【问题标题】:Open a JOptionPane with a button from other class使用其他类的按钮打开 JOptionPane
【发布时间】:2018-01-05 23:34:42
【问题描述】:

我想知道如何在JPanel 内的主类按钮中创建一个链接,该按钮关闭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);
}

我要链接的按钮是面板中的ButtomNewGmeSidePanel

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 请学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。 3)见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是的。)..
  • .. 4) public class StartingScreen extends JFrame{ JFrame StartingScreen = new JFrame(); .. 该代码既扩展了框架又保留了对标准框架的引用。做后者,而不是前者。 5) StartingScreen.add(..,BorderLayout.WEST); .. StartingScreen.add(..,BorderLayout.PAGE_END); 这使用了罗盘和逻辑约束的组合。最好始终如一地使用逻辑约束(例如,适应语言文本的默认方向),所以StartingScreen.add(..,BorderLayout.LINE_START); .. StartingScreen.add(..,BorderLayout.PAGE_END);
  • 谢谢,但是如果没有preferredsize,如何设置jpanel的大小?,如果我在一个jframe中有多个jpanel,setsize不起作用
  • “如何在没有preferredsize的情况下设置jpanel的大小?” 它们是自定义绘制的面板,还是包含组件?您是否阅读了链接问答中的前两个投票答案?我认为他们至少回答了你的部分问题。看完这些答案你有什么不明白的?顺便说一句 - MCVE / SSCCE 在哪里?投票结束,因为缺少一个。

标签: java swing jbutton joptionpane


【解决方案1】:

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

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

【讨论】:

  • 忘了说,这个最小的代码只适用于 Java 8 +
【解决方案2】:

我认为这段代码不会编译,因为你有 ButtonNewGame.addActionListener(this); 但你的类没有实现 ActionListener

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2012-06-02
    相关资源
    最近更新 更多