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. HCharts的y轴保留一位和 两位小数

    保留一位小数,有一位小数的不变 yAxis : { labels : {  formatter : function () { var strVal = ''+this.value ; if (str ...

  2. .NET DataSet DataTable 导出excel

    public void CreateExcel(DataSet ds, string FileName) { HttpResponse resp; resp = Page.Response; resp ...

  3. python进程理论部分

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): sxx在一个时间段内有很多任务要做:python备课的任务,写书的任务 ...

  4. Win 32平台SDK中的文件操作

    读取文件: HANDLE hFile ; // 声明文件操作内核对象句柄 hFile = CreateFile(, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL ...

  5. jQuery验证控件jquery.validate.js使用说明+中文API(转)

    一导入js库<script src="../js/jquery.js" type="text/javascript"></script> ...

  6. React Native - 1 Windows下的环境配置(Windows+Android)

    参考:https://facebook.github.io/react-native/docs/getting-started.html(要FQ)     网站上建议使用Chocolatey去配环境, ...

  7. 跳转的两种实现方法setInterval和setTimeout

    setInterval方法: <html> <head> <meta http-equiv="Content-Type" content=" ...

  8. RPD Volume 168 Issue 4 March 2016 评论1

    GEANT4 calculations of neutron dose in radiation protection using a homogeneous phantom and a Chines ...

  9. cojs.tk(所有题目来源) 树状数组专练

    1.求和问题 ★   输入文件:sum.in   输出文件:sum.out   简单对比时间限制:1.2 s   内存限制:128 MB [问题描述]     在一个长度为n的整数数列中取出连续的若干 ...

  10. Java高级架构师(一)第14节:新增和列表页面和分页tag

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...