使用其他类中的按钮打开JOptionPane

时间:2018-01-05 23:34:43

标签: java swing jbutton joptionpane

我想知道如何在关闭JPanel的{​​{1}}内的主要类的按钮中创建一个链接,并从另一个类中打开JFrame,欢迎任何帮助

这是我的主要课程:

JOptionPane

这是我的二级类,我存储了我要打开的选项窗格:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")

public class StartingScreen extends JFrame{
    JFrame StartingScreen = new JFrame();
    JPanel SidePanel = new JPanel();
    JPanel CenterPanel = new JPanel();
    JPanel BottomPanel = new JPanel();
    JButton ButtonNewGame = new JButton("New Game");
    JButton ButtonLoadGame = new JButton("Load Game");
    JButton ButtonDeleteGame = new JButton("Delete Game");

    public static void main(String[] args) {
        new StartingScreen();
        FileLoader FL = new FileLoader();
    }

    StartingScreen(){
        //Configuration of the JPanel
        super("StartingScreen");
        StartingScreen.setSize(880,520);
        StartingScreen.setResizable(false);
        StartingScreen.setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Configuration of the panels
        SidePanel.setBackground(Color.YELLOW);
        SidePanel.setPreferredSize(new Dimension(300,520));
        BottomPanel.setPreferredSize(new Dimension(880,80));
        BottomPanel.setBackground(Color.RED);
        CenterPanel.setBackground(Color.BLUE);
        //Configuration of the buttons
        ButtonNewGame.setBackground(Color.GREEN);
        ButtonNewGame.setPreferredSize(new Dimension(280,130));
        ButtonNewGame.setActionCommand("Add Credits");
        ButtonNewGame.addActionListener(this);
        ButtonLoadGame.setBackground(Color.GREEN);
        ButtonLoadGame.setPreferredSize(new Dimension(280,130));
        ButtonDeleteGame.setBackground(Color.GREEN);
        ButtonDeleteGame.setPreferredSize(new Dimension(280,130));

        //Adding components
        StartingScreen.add(SidePanel,BorderLayout.WEST);
        StartingScreen.add(CenterPanel,BorderLayout.CENTER);
        StartingScreen.add(BottomPanel,BorderLayout.PAGE_END);

        SidePanel.add(ButtonNewGame);
        SidePanel.add(ButtonLoadGame);
        SidePanel.add(ButtonDeleteGame);

        //Making it visible(Important to be at the end)
        StartingScreen.setVisible(true);
    }
}

我想要关联的按钮是import javax.swing.JOptionPane; public class NewGame { String[] classes ={ "Paladin", "Mage", "Warlock", }; int classes_index=JOptionPane.showOptionDialog(null, "Choose a class", "Classes", 0, JOptionPane.INFORMATION_MESSAGE, null, classes, 0); } 面板中的ButtomNewGme

2 个答案:

答案 0 :(得分:0)

您最好在NewGame类的构造函数(或其他一些最佳实践方法)中执行showOptionDialog。要打开这个JOptionPane,你必须:

ButtonNewGame.addActionListener((ActionEvent e) -> {
     new NewGame();
});

答案 1 :(得分:0)

我不认为此代码会编译,因为您有ButtonNewGame.addActionListener(this);,但您的课程没有实现ActionListener

要解决这个问题,您需要做的一件事就是编写actionPerformed(...)方法。按下按钮时会调用该按钮,我认为您要在其中创建NewGame的实例,此时您的选择器对话框将会出现。