JavaGUI程序打开一个新的JPanel,而不是替换当前面板
我正在学习JavaGUI,正在编写一个非常简单的程序 我有4个类,一个初始化gui,一个有一些随机方法,最后2个与gui相关,这是我感兴趣的。下面是我的GUI类:JavaGUI程序打开一个新的JPanel,而不是替换当前面板,java,swing,Java,Swing,我正在学习JavaGUI,正在编写一个非常简单的程序 我有4个类,一个初始化gui,一个有一些随机方法,最后2个与gui相关,这是我感兴趣的。下面是我的GUI类: public class GUI extends JFrame{ Container container; CardLayout cl; GridBagLayout gl; GridBagConstraints gbc; public GUI(){ super("Game Finder")
public class GUI extends JFrame{
Container container;
CardLayout cl;
GridBagLayout gl;
GridBagConstraints gbc;
public GUI(){
super("Game Finder");
cl = new CardLayout();
gl = new GridBagLayout();
gbc = new GridBagConstraints();
setLayout(cl);
container = this.getContentPane();
//File>New
JPanel newPanel = new JPanel();
newPanel.setLayout(gl);
newPanel.setBackground(Color.YELLOW);
JLabel newLabel = new JLabel("Time to find the game!");
JButton newButton = new JButton("Begin!");
newButton.setPreferredSize(new Dimension(200,75));
gbc.weightx=1;
gbc.weighty=1;
gbc.gridx=0;
gbc.gridy=1;
gbc.anchor = GridBagConstraints.PAGE_START;
gl.setConstraints(newButton, gbc);
newPanel.add(newButton, gbc);
newLabel.setFont(new Font("Serif", Font.BOLD, 24));
gbc.gridx=0;
gbc.gridy=0;
gbc.anchor = GridBagConstraints.CENTER;
gl.setConstraints(newLabel, gbc);
newPanel.add(newLabel, gbc);
container.add("New", newPanel);
//File>Load
JPanel loadPanel = new JPanel();
loadPanel.setBackground(Color.BLUE);
JLabel loadLabel = new JLabel("Feature yet to be implemented");
loadPanel.add(loadLabel);
container.add("Load", loadPanel);
//File>Save
JPanel savePanel = new JPanel();
savePanel.setBackground(Color.RED);
JLabel saveLabel = new JLabel("Feature yet to be implemented");
savePanel.add(saveLabel);
container.add("Save", savePanel);
//Help>About
JPanel aboutPanel = new JPanel();
JLabel aboutLabel = new JLabel();
String s = "Lorem iasd asd .";
aboutLabel.setText("<html><body style='width:230px'>" + s);
aboutPanel.setBackground(Color.WHITE);
aboutLabel.setBorder(new EmptyBorder(0,20,0,20));
aboutPanel.add(aboutLabel);
container.add("About", aboutPanel);
// Construct objects
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File"); // File menu
JMenu menuHelp = new JMenu("Help"); // Help menu
JMenuItem menuFileNew = new JMenuItem("New Game"); // New Game
JMenuItem menuFileLoad = new JMenuItem("Load Game"); // Load Game
JMenuItem menuFileSave = new JMenuItem("Save Game"); // Save Game
JMenuItem menuFileExit = new JMenuItem("Exit"); // Exit
JMenuItem menuHelpAbout = new JMenuItem("About"); // About
// Add action listener.for the menu button
menuFileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Methods().windowClosed();
}
}
);
menuFileNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "New");
}
}
);
menuFileLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "Load");
}
}
);
menuFileSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "Save");
}
}
);
menuHelpAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cl.show(container, "About");
}
}
);
/*HERE in particular*/
newButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
GamePanels g = new GamePanels();
g.begin();
container.add("Begin", g.getPanel());
cl.show(container, "Begin");
}
});
// Add menu items to menu bar
menuFile.add(menuFileNew);
menuFile.add(menuFileLoad);
menuFile.add(menuFileSave);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuHelp.add(menuHelpAbout);
menuBar.add(menuHelp);
// Create window
setJMenuBar(menuBar);
setSize(new Dimension(350, 350));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Container getContainer(){
return this.container;
}
对于GUI类中的监听器,如果我单击类似于file>about的内容,它将非常有效,即内容将与选定的容器面板切换。但是,当我尝试使用“外部”面板时,它会打开一个全新的GUI界面,因此我最终得到两个类似的界面:
假设我的实现有问题 我认为你的问题在于:
GamePanels g = new GamePanels(); //your gamePanel has a new GUI
public class GamePanels {
GUI gui = new GUI(); // your GUI is a new container
//...
因此,在actionPerformed中,不应新建游戏面板,而应新建最后一个游戏面板
在您的情况下,您可能想使用或我认为您在这方面有所进展,这是有道理的!如果我查看初始化GUI的主类,如果我以某种方式再次使用该GUI或返回GUI:
publicstaticvoid…{guiinitialize=new GUI()
那么也许我可以使用一种方法返回现有的gui并使用该gui?在这种情况下,您可以将其设置为静态的,或者将其发送到您需要的地方作为参数,或者使用上面的组件(如果您只想使用一个框架)解决了,我不知道为什么在GamePanels类中有GUI GUI=new GUI
。它实际上一直在工作,但它正在创建一个新的类实例。无论如何,谢谢Tony,你让我看得更清楚了!
GamePanels g = new GamePanels(); //your gamePanel has a new GUI
public class GamePanels {
GUI gui = new GUI(); // your GUI is a new container
//...