繁体   English   中英

可以使 .JOptionPane.showInputDialog 出现的 JButton

[英]JButton that can make a .JOptionPane.showInputDialog appear

我正在制作一个危险游戏,需要玩家能够点击一个框并出现一个问题。 我不明白我怎么能也有人可以帮助我使用我的 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;

当我到达我的动作侦听器时,我不确定使用什么代码才能让按下的按钮给我一个 InputDialog 来询问用户这个问题。

    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();

}

}

只需删除“pressedButton”。 来自pressedButton。 JOptionPane。 JOptionPane 是一个单独的类。 它不属于按钮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM