[英]How to use CardLayout with multiple JButtons?
I need to make a GUI that asks the users for details and then save them in a linked list.我需要制作一个 GUI 来询问用户的详细信息,然后将它们保存在一个链接列表中。 I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time.
我想使用 CardLayout 从一个框架切换到另一个框架,这是我第一次做的事情。 I have done probably less half of what I need to do and here I am quite lost in this part.
我可能只完成了我需要做的事情的一半,而在这里我完全迷失了这一部分。 The code below compiles and executes but when I click the buttons, the desired change does not happen .
下面的代码编译并执行,但是当我单击按钮时,所需的更改不会发生。 What could be wrong?
可能有什么问题?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:主要问题是使用空布局和这些代码行:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout
to BorderLayout.CENTER
, then you "overlay" it with your buttonPanel
, which is using null-layout.首先使用
CardLayout
将面板添加到BorderLayout.CENTER
,然后使用使用空布局的buttonPanel
“覆盖”它。
I would go with a simple FlowLayout
(the default layout-manager for a JPanel
) for the buttonPanel
and add it to the BorderLayout.SOUTH
of the contentPane
.我会为
buttonPanel
使用一个简单的FlowLayout
( JPanel
的默认布局管理器)并将其添加到contentPane
的BorderLayout.SOUTH
中。 I would also strongly recommend reading this tutorial .我也强烈推荐阅读本教程。
So remove the following lines of code:所以删除以下代码行:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel);
并更改
frame.getContentPane().add(buttonPanel);
to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
到
frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
. .
Also forget about the null-layout / setBounds()
in your Window
-classes.还要忘记
Window
类中的 null-layout / setBounds()
。
(Note that you still won't see the text change if you press a button because you always add a JLabel
with the same text ("Memory Files") to your Windows
.) (请注意,如果您按下按钮,您仍然不会看到文本更改,因为您总是将具有相同文本(“内存文件”)的
JLabel
添加到您的Windows
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.