本文主要是介绍java之 ------ 图形界面(三),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;public class UserJFrame extends JFrame implements ActionListener
{private int number=1; //编号private JTextField text_number, text_name; //编号、姓名文本行private JRadioButton radiob_male, radiob_female; //性别按钮private Object cities[][]; //存储多省的城市private JComboBox combox_province, combox_city; //省份、城市组合框private JButton button_add; //添加按钮private JTextArea text_user; //文本区public UserJFrame(Object provinces[], Object cities[][])//参数指定省份和城市数组{super("输入用户信息");this.setSize(740, 300);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(EXIT_ON_CLOSE);JPanel rightpanel=new JPanel(new GridLayout(6,1));//右面板JPanel leftpanel=new JPanel(new BorderLayout());//左面板leftpanel.setBorder(new TitledBorder("Person"));JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,rightpanel,leftpanel);//水平分隔窗格,左右各添加一个面板split.setDividerLocation(140);//设置水平分隔条的位置split.setEnabled(false);//设置分隔条不能变动this.getContentPane().add(split);//框架内容窗格添加分隔窗格text_user = new JTextArea();text_user.setEditable(false);leftpanel.add(text_user);leftpanel.add(new JScrollPane(text_user));//设置文本编辑域可以滚动text_number = new JTextField("1"); //编号文本行text_number.setEditable(false); //不可编辑,编号自动生成rightpanel.add(text_number);text_name = new JTextField("姓名");rightpanel.add(text_name);JPanel panel_rb=new JPanel(new GridLayout(1,2)); //单选按钮子面板,网格布局,1行2列rightpanel.add(panel_rb);ButtonGroup bgroup = new ButtonGroup(); //按钮组radiob_male = new JRadioButton("男",true); //创建单选按钮,默认选中bgroup.add(radiob_male); //单选按钮添加到按钮组panel_rb.add(radiob_male); //单选按钮添加到子面板radiob_female = new JRadioButton("女");bgroup.add(radiob_female);panel_rb.add(radiob_female);this.cities = cities;combox_province = new JComboBox(provinces); //省份组合框combox_province.setEditable(false); //设置组合框可编辑 combox_province.addActionListener(this);rightpanel.add(combox_province);combox_city = new JComboBox(cities[0]); //城市组合框rightpanel.add(combox_city);button_add = new JButton("添加");button_add.addActionListener(this);rightpanel.add(button_add);this.setVisible(true);}public void actionPerformed(ActionEvent e) //单击事件处理方法{if (e.getSource()==combox_province) //在组合框的下拉列表中选择数据项时{int i=combox_province.getSelectedIndex(); //省份组合框当前选中项序号combox_city.removeAllItems(); //清除地区组合框中原所有内容for (int j=0; j<this.cities[i].length; j++)combox_city.addItem(this.cities[i][j]); //地区组合框添加数据项}if (e.getSource() == button_add) //单击按钮{String aline=number+", "+text_name.getText();if (radiob_male.isSelected()) //指定单选按钮选中时aline += ", "+radiob_male.getText(); //获得单选按钮表示的性别字符串 if (radiob_female.isSelected())aline += ", "+radiob_female.getText();aline += ", "+combox_province.getSelectedItem(); //获得组合框选中项的字符串aline += ", "+combox_city.getSelectedItem();text_user.append(aline+"\n"); //文本区添加一行字符串this.number++; //编号自动加1text_number.setText(""+this.number);text_name.setText("姓名");}}public static void main(String arg[]){Object provinces[]={"江苏省", "浙江省"};Object cities[][]={{"南京市","苏州市","无锡市"}, {"杭州市","宁波市","温州市"}};new UserJFrame(provinces, cities);}
}
这篇关于java之 ------ 图形界面(三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!