以下代码是否正确并且有任何错误?我必须创建一个音乐类型的下拉框。
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxWindow extends JFrame
{
private JPanel musicPanel;
private Jpanel selectMusicPanel;
private JComboBox MusicBox;
private JLabel label;
private JTextField selectedMusic;
private String[] music = {"Rock","Metal","Electro","Pop","Country","Jazz","Indie"};
public ComboBoxWindow()
{
super("Combo Box Demo");
setDefultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildMusicPanel();
buildSelectedMusicPanel;
add(musicPanel,BorderLayout.CENTER);
add(selectedMusicPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildMusicPanel()
{
musicPanel = new JPanel();
musicBox = new JComboBox(music);
musicBox.addActionListener(new ComboBoxListener());
musicPanel.add(MusicBox);
}
private void buildSelectedCoffeePanel()
{
selectedMusicPanel = new JPanel();
label = new JLabel("You selected: ");
selectedMusic = new JTextField(10);
selectedMusic.setEditable(flase);
selectedMusicPanel.add(label);
selectedMusicPanel.add(selectedMusic);
}
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = (String) MusicBox.getSelectedItem();
selectedMusic.setText(selection);
public static void main(String[] args)
{
new ComboBoxWindow();
}
}