Amir Abdurazak
Amir Abdurazak

Reputation: 11

JButton that can make a .JOptionPane.showInputDialog appear

I am making a jeoprady game and need players to be able to click on a box and a question appear. I do not understand how i would be able too could someone help me with my actionlistener

package jeopardy;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class Jeopardy {

public static JButton[][] t = new JButton[6][6];
public static JFrame frame = new JFrame("Jeopardy");

public static void main(String[] args) {
    //Variables
    String Answer;

When I get to my action listener I am unsure what code to use to be able to get the pressed button to give me a InputDialog to ask users the question.

    ActionListener listener;
    listener = (ActionEvent e) -> {
        if (e.getSource() instanceof JButton) {
            JButton pressedbutton = (JButton) e.getSource();
            pressedbutton.JOptionPane.showInputDialog(null, "What time is it?");
        }
    };
    //JFrame frame = new JFrame("Jeopardy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(
            new GridLayout(6, 6));
    frame.setVisible(
            true);
    frame.setSize(
            1920, 950);

    for (int x = 0;
            x < 6; x++) {
        for (int i = 0; i < 6; i++) {
            t[i][x] = new JButton();
            //  t[i][x] = new JButton("1");
            t[i][x].setBackground(Color.BLUE);
            t[i][x].setForeground(Color.BLACK);
            //t[i][x].addActionListener(listener);
            frame.add(t[i][x]);

        }
    }

    for (int s = 0;
            s < 6; s++) {
        //Column 0
        t[0][1].setText("200");
        t[0][1].addActionListener(listener);
        t[0][2].setText("400");
        t[0][3].setText("600");
        t[0][4].setText("800");
        t[0][5].setText("1000");

        //Column 1
        t[1][1].setText("200");
        t[1][2].setText("400");
        t[1][3].setText("600");
        t[1][4].setText("800");
        t[1][5].setText("1000");

        //Column 2
        t[2][1].setText("200");
        t[2][2].setText("400");
        t[2][3].setText("600");
        t[2][4].setText("800");
        t[2][5].setText("1000");

        //Column 3
        t[3][1].setText("200");
        t[3][2].setText("400");
        t[3][3].setText("600");
        t[3][4].setText("800");
        t[3][5].setText("1000");

        //Column 4
        t[4][1].setText("200");
        t[4][2].setText("400");
        t[4][3].setText("600");
        t[4][4].setText("800");
        t[4][5].setText("1000");

        //Column 5
        t[5][1].setText("200");
        t[5][2].setText("400");
        t[5][3].setText("600");
        t[5][4].setText("800");
        t[5][5].setText("1000");
    }

    frame.repaint();

    frame.revalidate();

}

}

Upvotes: 1

Views: 58

Answers (1)

AtherSajjad
AtherSajjad

Reputation: 119

Just remove 'pressedButton.' From pressedButton. JOptionPane. JOptionPane is a separate class. It doesn't belong to button

Upvotes: 1

Related Questions