gpt4 book ai didi

java - 如何将 CardLayout 与多个 JButton 一起使用?

转载 作者:行者123 更新时间:2023-11-30 08:44:23 24 4
gpt4 key购买 nike

我需要制作一个 GUI,询问用户详细信息,然后将它们保存在链表中。我想使用 CardLayout 从一个框架切换到另一个框架,这是我第一次做的事情。我完成的工作可能还不到我需要做的一半,而在这里我完全迷失了这一部分。下面的代码编译并执行,但是当我单击按钮时,没有发生所需的更改。有什么问题吗?

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);
}
}

最佳答案

主要问题是空布局的使用和这些代码行:

frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);

首先使用 CardLayout 添加面板至 BorderLayout.CENTER , 然后你用你的 buttonPanel “覆盖”它,它使用的是空布局。

我会选择一个简单的 FlowLayout (JPanel 的默认布局管理器)对于 buttonPanel并将其添加到 BorderLayout.SOUTHcontentPane .我也强烈建议阅读 this tutorial .

所以去掉下面几行代码:

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);

并更改frame.getContentPane().add(buttonPanel);frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); .

同时忘记空布局/setBounds()在你的Window -类。

(请注意,如果您按下按钮,您仍然不会看到文本发生变化,因为您总是将具有相同文本(“内存文件”)的 JLabel 添加到您的 Windows 中。)

关于java - 如何将 CardLayout 与多个 JButton 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com