繁体   English   中英

swing-如何使用按钮切换JPanel

[英]swing - how to switch JPanel using button

大家好,我打算创建登录面板。 在该面板中应该是用户JLabel,密码JLabel,用户JTextField,密码JTextField和JButon。 我想使用该按钮切换到新的JPanel。 我读过最好的方法是CardLayout,我试图修改该代码:

//Where the GUI is assembled:
//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);
...
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
...

//Method came from the ItemListener class implementation,
//contains functionality to process the combo box item selecting
public void itemStateChanged(ItemEvent evt) {
    CardLayout cl = (CardLayout)(cards.getLayout());
    cl.show(cards, (String)evt.getItem());
}

我正在尝试修改那部分代码

JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);

并将其更改为:

JButton loginButton = new JButton();
loginButton.addItemListener(this);
comboBoxPane.add(loginButton);
pane.add(loginButton, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);

我不能使用:

JButton loginButton = new JButton(comboBoxItems);

因为编译器返回错误:构造函数JButton(String [])未定义

有谁能帮助我解决我的问题。 我是Java编程的新手

JButton没有采用String数组的构造函数。 只需调用:

JButton loginButton = new JButton("Login");

请参阅: 使用JFC / Swing创建GUI

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM