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. 内存可见性,指令重排序,JIT。。。。。。从一个知乎问题谈起

    在知乎上看到一个问题<java中volatile关键字的疑惑?>,引起了我的兴趣 问题是这样的: package com.cc.test.volatileTest; public clas ...

  2. (15)python 数据库连接

    python连接mysql两种方法 一.python官网提供的 MySQL-python 软件 下载地址 https://pypi.python.org/pypi/MySQL-python/1.2.5 ...

  3. 并查集&线段树&树状数组&排序二叉树

    超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11 ...

  4. next_permutatio

    next_permutation的函数声明:#include  <algorithm> bool next_permutation( iterator start, iterator en ...

  5. 28、Django实战第28天:个人信息展示

    从今天开始,我来完成个人中心部分,前端页面如下 1.浏览这些页面可以发现,它们和base.html是有区别的,因此,它们需要新建一个模板usercenter-base.html 2.把usercent ...

  6. 自定义编写jmeter的Java测试代码

    我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","JavaV ...

  7. [BZOJ 2743] 采花

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2743 Algorithm: 此题询问区间内出现次数超过1个的数字 明显在线做无从下手,无 ...

  8. 1.3(Spring MVC学习笔记)数据绑定

    一.数据绑定介绍 用户发送过来的数据,只有传递到服务器端的参数上才会起作用. 比如用户输入的用户名和密码要和后台方法中代表用户名和密码的变量关联起来, 从而才能使用用户传递的数据进行一些操作,这样数据 ...

  9. Problem Y: 零起点学算法21——摄氏温度转换

    #include<stdio.h> int main() { float f,c; while(scanf("%f",&f)!=EOF) c=*(f-); pr ...

  10. Android Studio 首次安装取消自动下载SDK

    一.第一次安装: Android Studio安装完成后,第一次启动AS前,为了避免重新下载新版本的SDK,需要做如下操作: AS启动前,打开安装目录,请先将bin目录的idea.properties ...