第二周学习笔记


学习的组件的使用方法:下拉框 JComBox;单选按钮 JRaidoButton;
单选按钮要二选一的话需要添加ButtonGroup!
下拉框前面省份和后面市不会设置一一对应。

程序运行截图

程序代码

package project;
import javax.swing.*;  

import java.awt.*;  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Interfacecomponents{  
private JFrame frame;  
private JButton bt1;  
private JRadioButton bt2;  
private JRadioButton bt3;  
private JLabel lab1;  
private JLabel lab2;  
private JLabel lab3;  
private JLabel lab4;  
private JLabel lab5;  
private JLabel lab6;  
private JTextField text1;  
private JTextField text2;  
private JTextField text3;  
private JTextField text4;  
private JComboBox comboBox1;  
private JComboBox comboBox2;  
private JList list;

public Interfacecomponents(){
makeFrame();        
}
private void makeFrame(){
frame = new JFrame("人口登记表");    
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panelcenter = new JPanel(new GridLayout(6,2));
JPanel center1 = new JPanel(new FlowLayout());
JPanel center2 = new JPanel(new FlowLayout());
JPanel center3 = new JPanel(new FlowLayout());
JPanel center4 = new JPanel(new FlowLayout());
JPanel center5 = new JPanel(new FlowLayout());
JPanel center6 = new JPanel(new FlowLayout());
JPanel radioButton = new JPanel(new FlowLayout());

lab1 = new JLabel("姓          名:");
lab2 = new JLabel("性          别:");
lab3 = new JLabel("出生日期:");
lab4 = new JLabel("联系方式:");
lab5 = new JLabel("身份证号:");
lab6 = new JLabel("地          址:");
bt2 = new JRadioButton("男");
bt3 = new JRadioButton("女");
text1 = new JTextField(16);
text2 = new JTextField(16);
text3 = new JTextField(16);
text4 = new JTextField(16);

bt1 = new JButton("登记");
bt1.addActionListener(new ActionListener() {             
    @Override
    public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
    	int t = JOptionPane.showConfirmDialog(frame, "请确认信息无误","确认对话框",JOptionPane.YES_OPTION);
    	if(t == JOptionPane.YES_OPTION)
    		JOptionPane.showMessageDialog(frame, "登记成功","提示", JOptionPane.PLAIN_MESSAGE);
    }
   });
String[] likes1 = {"山西省","陕西省","北京市","河北省","河南省","黑龙江省"};
String[] likes2 = {"太原市","西安市","","石家庄","洛阳","哈尔滨"};
list = new JList(likes1);
comboBox1 = new JComboBox(likes1);
comboBox1.setEditable(true);
comboBox1.setMaximumRowCount(16);
comboBox2 = new JComboBox(likes2);
comboBox2.setEditable(true);
comboBox2.setMaximumRowCount(16);
ButtonGroup bg = new ButtonGroup();
bg.add(bt2);
bg.add(bt3);
radioButton.add(bt2);
radioButton.add(bt3);

center1.add(lab1);
center1.add(text1);
center2.add(lab2);
center2.add(radioButton);
center3.add(lab3);
center3.add(text2);
center4.add(lab4);
center4.add(text3);
center5.add(lab5);
center5.add(text4);
center6.add(lab6);
center6.add(comboBox1);
center6.add(comboBox2);
contentPane.add(bt1);
panelcenter.add(center1);
panelcenter.add(center2);
panelcenter.add(center3);
panelcenter.add(center4);
panelcenter.add(center5);
panelcenter.add(center6);
contentPane.add(panelcenter,BorderLayout.NORTH);
contentPane.add(bt1,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Interfacecomponents();
 }
}    

                                                                                                                2016/3/18 18:02:13

返回顶部

原文地址:https://www.cnblogs.com/djz2016/p/5293147.html