-1
以下代碼看起來是否正確,是否有錯誤?我必須創建一個音樂流派的下拉框。組合框代碼是否正確?
我的代碼:
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();
}
}