【发布时间】: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 example 或 Short, Self Contained, Correct Example。 2) 请学习常见的 Java 命名法(命名约定 - 例如
EachWordUpperCaseClass、firstWordLowerCaseMethod()、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