gpt4 book ai didi

java - CardLayout 带有更改卡片的按钮

转载 作者:行者123 更新时间:2023-12-02 05:44:53 28 4
gpt4 key购买 nike

我正在尝试编写一个简短的程序,它有一个带有标题的主页和 4 个按钮,其中 3 个按钮将离开主屏幕并转到一个新页面,您可以在其中相应地输入信息。我开始使用拖放编辑器,但通过论坛发现我应该使用卡片布局,而不是我查看了 oracle 教程,但我想使用按钮来更改卡片而不是组合框。那可能吗。

这是教程中的代码,我需要更改哪些部分才能使按钮成为更改屏幕的操作按钮。

谢谢

package layout;

/*
* CardLayoutDemo.java
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
JPanel cards; //a panel that uses CardLayout
final static String BUTTONPANEL = "Card with JButtons";
final static String TEXTPANEL = "Card with JTextField";

public void addComponentToPane(Container pane) {
//Put the JComboBox in a JPanel to get a nicer look.
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);

//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));

JPanel card2 = new JPanel();
card2.add(new JTextField("TextField", 20));

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("CardLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
CardLayoutDemo demo = new CardLayoutDemo();
demo.addComponentToPane(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);

//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

but i want to use buttons to change cards not a combobox. Is that possible.

本教程使用组合框的 ItemListener。

如果您想使用按钮,那么您可以使用 ActionListener。

ActionListener 中的代码基本上是相同的。也就是说,您需要识别您要更改的卡。

cl.show(cards, BUTTONPANEL); // for example

虽然该按钮可能会显示您想要切换到的面板的名称,但您可以执行以下操作:

cl.show(cards, event.getActionCommand());

单击按钮时生成的 ActionEvent 的操作命令包含按钮上显示的文本。

关于java - CardLayout 带有更改卡片的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167805/

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