CardLayout 无法正常工作
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看
ItemEvent
,您会发现一项正在DESELECTED
,而另一项正在SELECTED
。相反,监听ActionEvent
,如下所示 此处,然后相应地选择正确的卡。附录:如果您实施@Michael Brewer-Davis 的答案中的有用更改,那么合适的 ActionListener 就特别简单:
If you look at the
ItemEvent
, you'll see that one item is beingDESELECTED
and the other is beingSELECTED
. Instead, listen forActionEvent
, 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:在添加组件之前设置布局管理器。
有两个项目正在改变状态;一个被取消选择,另一个被选择。您可以通过以下方式改进调试输出:
System.out.println("Event Triggered: " + e);
您还需要考虑将选择切换回来的事件 - 并非全部组合框中的更改应选择
TEXTPANEL
。Set the layout manager before adding components.
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
.