簡體   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