2012-11-25 61 views
0

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

//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编程的新手

+2

如果这是一个问题,你最好有一些基本的Java教程开始。只是从互联网复制代码并试图做出一些修改而不理解它的作用会导致你从一个问题到另一个问题 – Robin

+0

是的,我理解你的观点,我正在学习Java基础。 – Karol

回答