大家好我打算创建登录面板。在该面板中应该是用户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编程的新手
答案 0 :(得分:3)
JButton
没有带String
数组的构造函数。打电话就足够了:
JButton loginButton = new JButton("Login");