简体   繁体   English

swing-如何使用按钮切换JPanel

[英]swing - how to switch JPanel using button

Hi guys I'm planning to create login panel. 大家好,我打算创建登录面板。 In that panel should be user JLabel, password JLabel, user JTextField, password JTextField, and JButon. 在该面板中应该是用户JLabel,密码JLabel,用户JTextField,密码JTextField和JButon。 I would like to use that button to switch to new JPanel. 我想使用该按钮切换到新的JPanel。 I've read the best way is CardLayout and I'm trying to modify that code: 我读过最好的方法是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());
}

I'm trying to modify that part of code 我正在尝试修改那部分代码

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

and change it in to: 并将其更改为:

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

I can't use: 我不能使用:

JButton loginButton = new JButton(comboBoxItems);

because compiler return error: The constructor JButton(String[]) is undefined 因为编译器返回错误:构造函数JButton(String [])未定义

is any one can help me with my problem. 有谁能帮助我解决我的问题。 I'm newbie in Java programming 我是Java编程的新手

JButton does not have a constructor which takes a String array. JButton没有采用String数组的构造函数。 It is sufficient to call: 只需调用:

JButton loginButton = new JButton("Login");

See: Creating a GUI With JFC/Swing 请参阅: 使用JFC / Swing创建GUI

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

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