CardLayout 无法正常工作

发布于 2024-11-17 23:31:32 字数 2989 浏览 3 评论 0原文

我用 Splitpane、Combobox 和其他一些包含按钮和标签的面板编写了这个简单的 Cardlayout 示例。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class splitpane_test extends JFrame implements ItemListener {

    private JPanel contentPane;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    JPanel cards;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    splitpane_test frame = new splitpane_test();
                    //frame.addComponentToPane(frame.getContentPane());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public splitpane_test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JSplitPane splitPane = new JSplitPane();
        contentPane.add(splitPane, BorderLayout.CENTER);


        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        splitPane.setLeftComponent(comboBoxPane);

        //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();
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        splitPane.setRightComponent(cards);
        cards.setLayout(new CardLayout(0, 0));
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        System.out.print("Event Triggered \n");
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, TEXTPANEL);
    }
}

我可以看到左侧带有组合框的分割面板,右侧带有其他卡片布局面板。 当我更改组合框项目时,没有任何事情发生在正确的尺寸上。 为了验证我是否击中了卡片,我使用了 System.out.print("事件触发\n"); 但我看到的令人惊讶的事情是,它为每个组合框项目显示两次,就像它调用两次一样

Event Triggered
Event Triggered

,您可以建议我我在这里做错了什么以及为什么触发的事件被击中两次。 感谢您的所有时间和帮助。

I have written this simple Cardlayout example with Splitpane, Combobox and few other panels containing buttons and label.

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class splitpane_test extends JFrame implements ItemListener {

    private JPanel contentPane;
    final static String BUTTONPANEL = "Card with JButtons";
    final static String TEXTPANEL = "Card with JTextField";
    JPanel cards;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    splitpane_test frame = new splitpane_test();
                    //frame.addComponentToPane(frame.getContentPane());
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public splitpane_test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JSplitPane splitPane = new JSplitPane();
        contentPane.add(splitPane, BorderLayout.CENTER);


        JPanel comboBoxPane = new JPanel();
        String comboBoxItems[] = {BUTTONPANEL, TEXTPANEL};
        JComboBox cb = new JComboBox(comboBoxItems);
        cb.setEditable(false);
        cb.addItemListener(this);
        comboBoxPane.add(cb);
        splitPane.setLeftComponent(comboBoxPane);

        //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();
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);
        splitPane.setRightComponent(cards);
        cards.setLayout(new CardLayout(0, 0));
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        System.out.print("Event Triggered \n");
        CardLayout cl = (CardLayout) (cards.getLayout());
        cl.show(cards, TEXTPANEL);
    }
}

I can see the splitpane with combobox on left and other cardlayout panels on right.
when i change the combobox items nothing is happening on right size.
In order to verify if iam hitting the cardout i used the
System.out.print("Event Triggered \n");
but the surprising thing i have seen is that its displaying twice for each combobox item change as if its calling twice

Event Triggered
Event Triggered

Can you please suggest me what iam doing wrong here and why event triggered is getting hit twice.
Thanks for all your time and help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏日浅笑〃 2024-11-24 23:31:32

您能否建议我在这里做错了什么以及为什么触发的事件被击中两次。

如果您查看 ItemEvent,您会发现一项正在DESELECTED,而另一项正在SELECTED。相反,监听 ActionEvent,如下所示 此处,然后相应地选择正确的卡。

附录:如果您实施@Michael Brewer-Davis 的答案中的有用更改,那么合适的 ActionListener 就特别简单:

@Override
public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();
    CardLayout cl = (CardLayout) cards.getLayout();
    cl.show(cards, jcb.getSelectedItem().toString());
}

Can you please suggest me what I am doing wrong here and why event triggered is getting hit twice.

If you look at the ItemEvent, you'll see that one item is being DESELECTED and the other is being SELECTED. Instead, listen for ActionEvent, as shown here, and select the correct card accordingly.

Addendum: If you implement the helpful changes in @Michael Brewer-Davis's answer, then a suitable ActionListener is particularly straightforward:

@Override
public void actionPerformed(ActionEvent e) {
    JComboBox jcb = (JComboBox) e.getSource();
    CardLayout cl = (CardLayout) cards.getLayout();
    cl.show(cards, jcb.getSelectedItem().toString());
}
梦途 2024-11-24 23:31:32
  1. 在添加组件之前设置布局管理器。

  2. 有两个项目正在改变状态;一个被取消选择,另一个被选择。您可以通过以下方式改进调试输出:

    System.out.println("Event Triggered: " + e);

您还需要考虑将选择切换回来的事件 - 并非全部组合框中的更改应选择 TEXTPANEL

  1. Set the layout manager before adding components.

  2. Two items are changing state; one is being deselected, the other selected. You would improve your debugging output with the following:

    System.out.println("Event Triggered: " + e);

You'll also need to account for the event switching the selection back--not all changes in the combo box should select TEXTPANEL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文