本人上大二,正在学习java 图形用户界面的设计,遇到了一个困难,希望各路大神帮我看看
package HMB;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutDemo implements ActionListener{
JScrollPane listScroller;
JList list;
DefaultListModel listModel;
JButton button1;
JComboBox comboBox;
JTextField nameTextField,addressTextField;
JRadioButton manRadioButton,womanRadioButton;
JLabel label,nameLabel,addressLabel,label2,label3;
JFrame frame;
JPanel pane ,pane1,pane2,pane3,pane4;
public BorderLayoutDemo(){
frame = new JFrame(" *BorderLayout* ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = new JPanel();
pane1 = new JPanel();pane2 = new JPanel();pane3 = new JPanel();
frame.setContentPane(pane);
label3 = new JLabel("选择县:");
listModel = new DefaultListModel();
listModel.addElement("东乡县");
listModel.addElement("进贤县");
listModel.addElement("吉安县");
listModel.addElement("高安县");
listModel.addElement("萍乡县");
listModel.addElement("鹰潭县");
listModel.addElement("抚州市");
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(-1);
list.setSelectedIndex(1);
listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(150,80));
pane4.add(label3);
pane4.add(listScroller);
button1 = new JButton("确定");
button1.setForeground(Color.RED);
label= new JLabel("性别:");
manRadioButton = new JRadioButton("男");
womanRadioButton = new JRadioButton("女");
manRadioButton.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(manRadioButton);
group.add(womanRadioButton);
pane1.add(label);
pane1.add(manRadioButton);
pane1.add(womanRadioButton);
nameLabel = new JLabel("姓名:");
nameTextField = new JTextField(7);
nameTextField.setText("请输入姓名:");
nameTextField.setHorizontalAlignment(JTextField.RIGHT);
nameTextField.setBounds(30,4,32,10);
addressLabel = new JLabel("地址:");
addressTextField = new JTextField(12);
addressTextField.setText("请输入地址:");
addressTextField.setHorizontalAlignment(JTextField.LEFT);
addressTextField.setBounds(40,6,42,13);
pane2.add(nameLabel);
pane2.add(nameTextField);
pane2.add(addressLabel);
pane2.add(addressTextField);
label2 = new JLabel("选择省份:");
String[] province = {"江西","湖南","山西","安徽","北京","四川","广东","河南","福建"};
comboBox = new JComboBox(province);
comboBox.insertItemAt("香港",3);
comboBox.setSelectedIndex(4);
pane3.add(label2);
pane3.add(comboBox);
pane3.add(button1);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(" *红玫瑰* ");
Font font = new Font("SansSerif",Font.BOLD,20);
button1.setForeground(Color.BLUE);
button1.setFont(font);
}
});
}
public void creatAndShowGUI(){
//pane.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
/*pane.add(pane1,BorderLayout.NORTH);
pane.add(pane2,BorderLayout.WEST);
pane.add(pane3,BorderLayout.CENTER);*/
//pane.setLayout(new GridLayout(3,1,5,5));
pane.add(pane1);
pane.add(pane2);
pane.add(pane3);
pane.add(pane4);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new BorderLayoutDemo().creatAndShowGUI();
}
});
}
}
为什么加了第四个面板(加粗体了)后就运行有错误了呢?删除后就没问题了!真是没搞懂,希望哪位大神帮忙看看,指点一下小弟,谢谢了
浙公网安备 33010602011771号