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; //性别button
private Object cities[][]; //存储多省的城市
private JComboBox combox_province, combox_city; //省份、城市组合框
private JButton button_add; //加入button
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)); //单选button子面板,网格布局。1行2列
rightpanel.add(panel_rb);
ButtonGroup bgroup = new ButtonGroup(); //button组
radiob_male = new JRadioButton("男",true); //创建单选button。默认选中
bgroup.add(radiob_male); //单选button加入到button组
panel_rb.add(radiob_male); //单选button加入到子面板
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) //单击button
{
String aline=number+", "+text_name.getText();
if (radiob_male.isSelected()) //指定单选button选中时
aline += ", "+radiob_male.getText(); //获得单选button表示的性别字符串
if (radiob_female.isSelected())
aline += ", "+radiob_female.getText();
aline += ", "+combox_province.getSelectedItem(); //获得组合框选中项的字符串
aline += ", "+combox_city.getSelectedItem();
text_user.append(aline+"\n"); //文本区加入一行字符串
this.number++; //编号自己主动加1
text_number.setText(""+this.number);
text_name.setText("姓名");
}
} public static void main(String arg[])
{
Object provinces[]={"江苏省", "浙江省"};
Object cities[][]={{"南京市","苏州市","无锡市"}, {"杭州市","宁波市","温州市"}};
new UserJFrame(provinces, cities);
}
}

java之 ------ 图形界面(三)的更多相关文章

  1. Java的图形界面依然是跨平台的

    Awt:抽象窗口工具箱,它由三部分组成: ①组件:界面元素: ②容器:装载组件的容器(例如窗体): ③布局管理器:负责决定容器中组件的摆放位置. 图形界面的应用分四步: ① 选择一个容器: ⑴wind ...

  2. 【计项02组01号】Java版图形界面计算器

    Java版图形界面计算器1.0版本 项目分析[1.0] 组成部分 代码结构 (1)窗口的创建 在<JDK 核心 API>中我们提到,创建一个窗口需要使用 JFrame 类.在本实验中,我们 ...

  3. Java Swing 图形界面开发(目录)

    Java Swing 图形界面开发(目录) 2017年05月30日 23:50:42 阅读数:5228 本文链接: http://blog.csdn.net/xietansheng/article/d ...

  4. Java Swing图形界面开发

    本文转自xietansheng的CSDN博客内容,这是自己见过的最通俗易懂.最适合快速上手做Java GUI开发的教程了,这里整合一下作为自己以后复习的笔记: 原文地址:https://blog.cs ...

  5. Java自学-图形界面 容器

    Swing 的容器 JFrame和JDialog java的图形界面中,容器是用来存放 按钮,输入框等组件的. 窗体型容器有两个,一个是JFrame,一个是JDialog 步骤 1 : JFrame ...

  6. Java自学-图形界面 Swing中的线程

    Swing中的线程 步骤 1 : 三种线程 在Swing程序的开发中,需要建立3种线程的概念 初始化线程 初始化线程用于创建各种容器,组件并显示他们,一旦创建并显示,初始化线程的任务就结束了. 事件调 ...

  7. JAVA与图形界面开发(Applet应用程序、AWT库、Swing)

    Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...

  8. Java GUI图形界面开发工具

    Applet 应用程序     一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...

  9. java的图形界面初学惯用

    1.单一界面的创建 public void mainFrame() { HashMap<String, Component> views = new HashMap<String, ...

随机推荐

  1. javascript:入门笔记

    1:html注释: <html> <body> <script type="text/javascript"> <!-- document ...

  2. 搜索引擎--范例:新浪微博API获取最近的微博--statuses/public_timeline

    新浪微博平台跟新浪SAE一样,都是一个字“坑”,好了,不再吐槽,直入主题 1:直接登录新浪,添加网站,就直接添加我们在新浪SAE上创建的应用即可 2:填写信息,ICP备案信息号填写sinaapp.co ...

  3. Tkinter 小应用

    import tkinter as tk class APP: def __init__(self,master): frame = tk.Frame(master) frame.pack(side ...

  4. [loj#115] 无源汇有上下界可行流 网络流

    #115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据   题 ...

  5. 使用threadpool并发测试,报错HTTPConnectionPool Max retires exceeded

    解决方法:和以下答案一致 https://blog.csdn.net/qq_21405949/article/details/79363084 场景: 在做爬虫项目或者是在发送网络请求的时候,一般都会 ...

  6. Burp Suite的使用介绍

    在网上找了一篇关于Burp Suite的使用介绍,感觉写的基础的,下面就copy了,另外还有一篇<BurpSuite实战指南>的pdf是一位好心的“前辈”共享的https://www.gi ...

  7. scrapy生成csv文件空行、csv文件打开乱码(解决方案)

    一.scrapy生成csv文件会有多余的空行 当使用scrapy crawl testspider -o test.csv后,生成的默认csv文件每一行之间是有空行的,解决的方法是修改scrapy的源 ...

  8. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  9. POJ1300Door Man(欧拉回路)

                                                               Door Man Time Limit: 1000MS   Memory Limi ...

  10. MongoError: topology was destroyed解决方法

    MongoError: topology was destroyed 分析得出,出现这个问题是因为,当mongodb尝试写入某个数据的时候,连接被中断了! 解决方法:检查代码中是否存在操作数据的过程中 ...