Java-第15章图形用户界面设计例题
Example15_1.java JFrame常用方法
import javax.swing.*;
import static javax.swing.JFrame.*;
public class Example15_1 {
public static void main(String args[]) {
JFrame window1=new JFrame("撤销窗口");
JFrame window2=new JFrame("退出程序");
window1.setBounds(60,100,188,108);
window2.setBounds(260,100,188,108);
window1.setVisible(true);
window1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window2.setVisible(true);
window2.setDefaultCloseOperation(EXIT_ON_CLOSE); }
}

Example 15_2.java菜单条、菜单、菜单项
public class Example15_2 {
public static void main(String args[]) {
WindowMenu win=new WindowMenu("带菜单的窗口",20,30,200,190);
}
}
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static javax.swing.JFrame.*;
public class WindowMenu extends JFrame{
JMenuBar menubar ;
JMenu menu,subMenu;
JMenuItem item1,item2;
public WindowMenu() {}
public WindowMenu(String s,int x,int y,int w,int h) {
init(s);
setLocation(x,y);
setSize(w,h);
setVisible (true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(String s) {
setTitle(s);
menubar =new JMenuBar();
menu=new JMenu("菜单");
subMenu=new JMenu("软件项目");
item1=new JMenuItem("JAVA话题",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\a.gif"));//图片所在位置
item2=new JMenuItem("动画话题",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\b.gif"));//图片所在位置
item1.setAccelerator(KeyStroke.getKeyStroke('A'));
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menu.add(item1);
menu.addSeparator();
menu.add(item2);
menu.add(subMenu);
subMenu.add(new JMenuItem("汽车销售系统",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\c.gif")));
subMenu.add(new JMenuItem("农场信息系统",new ImageIcon("D:\\java\\eclipse\\5.24作业\\src\\d.gif")));
menubar.add(menu);
setJMenuBar(menubar);
}
}





Example15_3 常用组件
public class Example15_3 {
public static void main (String args[]) {
ComponentInWindow win =new ComponentInWindow();
win.setBounds(100,100,310,260);
win.setTitle("常用组件");
}
}
import java.awt.*;
import javax.swing.*;
import static javax.swing.JFrame.*;
public class ComponentInWindow extends JFrame{
JTextField text;
JButton button;
JCheckBox checkBox1,checkBox2,checkBox3 ;
JRadioButton radio1,radio2;
ButtonGroup group;
JComboBox comBox;
JTextArea area ;
public ComponentInWindow() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());
add(new JLabel("文本框"));
text=new JTextField(10);
add(text);
add(new JLabel("按钮:"));
button =new JButton("确定");
add(button);
add(new JLabel("选择框:")) ;
checkBox1=new JCheckBox("喜欢音乐");
checkBox2=new JCheckBox("喜欢旅游");
checkBox3=new JCheckBox("喜欢篮球");
add(checkBox1);
add(checkBox2);
add(checkBox3);
add(new JLabel("单选按钮:"));
group=new ButtonGroup();
radio1=new JRadioButton("男");
radio2=new JRadioButton("女");
group.add(radio1);
group.add(radio2);
add(radio1);
add(radio2);
add(new JLabel("下拉列表:"));
comBox=new JComboBox();
comBox.addItem("音乐天地");
comBox.addItem("武术天地");
comBox.addItem("象棋乐园");
add(comBox);
add(new JLabel("文本区:"));
area=new JTextArea(6,12);
add(new JScrollPane(area)); }
}

Example15_4常用布局
public class Example15_4{
public static void main(String args[])
{
WindowBoxLayout win=new WindowBoxLayout () ;
win. setBounds (100, 100, 310,260);
win. setTitle("嵌套盒式布局容器");
}
}
import javax.swing.*;
public class WindowBoxLayout extends JFrame{
Box baseBox,boxV1,boxV2;
public WindowBoxLayout() {
setLayout (new java.awt. FlowLayout()) ;
init() ;
setVisible (true) ;
setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE) ;
}
void init () {
boxV1=Box.createVerticalBox() ;
boxV1. add (new JLabel ("姓名")) ;
boxV1.add (Box.createVerticalStrut(8)) ;
boxV1.add (new JLabel ("email")) ;
boxV1. add (Box.createVerticalStrut(8)) ;
boxV1.add (new JLabel ("职业")) ;
boxV2=Box. createVerticalBox() ;
boxV2. add (new JTextField(10) ) ;
boxV2.add (Box.createVerticalStrut(8)) ;
boxV2.add(new JTextField(10)) ;
boxV2. add (Box. createVerticalStrut(8));
boxV2. add (new JTextField(10));
baseBox=Box.createHorizontalBox() ;
baseBox.add (boxV1) ;
baseBox.add (Box.createVerticalStrut(10));
baseBox.add(boxV2) ;
add (baseBox) ; }
}

Example15_6 ActionEvent事件
public class Example15_6 {
public static void main(String args[]) {
WindowActionEvent win=new WindowActionEvent();
win.setBounds(100, 100, 460,360);
win. setTitle ("处理ActionEvent事件");
}
}
import java.awt. *;
import javax.swing.*;
public class WindowActionEvent extends JFrame{
JTextField inputText;
JTextArea textShow;
JButton button;
PoliceListen listener;
public WindowActionEvent() {
init() ;
setVisible(true) ;
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout (new FlowLayout()) ;
inputText = new JTextField(10);
button = new JButton("读取");
textShow = new JTextArea(9, 30) ;
listener= new PoliceListen();
listener.setJTextField(inputText);
listener.setJTextArea (textShow) ;
inputText.addActionListener (listener);
add (inputText) ;
add (button) ;
add (new JScrollPane (textShow)) ;
}
}
import java.awt.event.*;
import java.io.*;
import javax. swing.*; public class PoliceListen implements ActionListener{
JTextField textInput;
JTextArea textShow;
public void setJTextField (JTextField text) {
textInput = text;
}
public void setJTextArea (JTextArea area) {
textShow = area;
}
public void actionPerformed (ActionEvent e) {
textShow.setText (null);
try { File file = new File (textInput.getText());
FileReader inOne = new FileReader (file) ;
BufferedReader inTwo = new BufferedReader (inOne) ;String s=null;
while((s=inTwo. readLine()) !=null)
textShow.append(s+"n") ;
inOne.close();
inTwo.close();
}
catch (Exception ee) {
textShow. append (ee.toString());
}
}
}

注意:如果出现无法解析的情况,可将错误代码重新键盘输入。
Java-第15章图形用户界面设计例题的更多相关文章
- MATLAB学习笔记(十一)——MATLAB图形用户界面设计
(一)菜单设计 一.建立用户菜单 1.概况: 用户菜单一般含有一级菜单和二级菜单,乃至多级菜单.每一级菜单又包含多个菜单项.建立菜单可以使用uimenu函数. 2.uimenu函数调用: %建立一级菜 ...
- Java基础学习总结 -- 图形用户界面GUI
虽然目前Java算不上前端开发的主力,但是作为Java入门基础的一部分,学习Java的GUI编程还是有必要的,而且可以做出一些小且有趣的图形程序来提高学习热情.本篇学习总结均为一个Beginner的笔 ...
- java第八节 GUI/图形用户界面
/* *第8讲 GUI/图形用户界面 * AWT的基础知识 * GUI全称是Graphical User Interface,即图形用户界面 * JDK中提供了AWT和Swing两个包,用于GUI程序 ...
- 201671010127 2016-2017-11 Java图形用户界面设计技术
一.事件处理器 1.什么是事件处理 一个事件要求特定的动作被执行,它被作为消息由外界或系统自身发送给GUI系统.这些事件包括来自计算机设备如鼠标键盘和网络端口的I/O中断,以及GUI系统的逻辑事件触发 ...
- 《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记
一.为什么GUI是单线程化 传统的GUI应用程序通常都是单线程的. 1. 在代码的各个位置都须要调用poll方法来获得输入事件(这样的方式将给代码带来极大的混乱) 2. 通过一个"主事件循环 ...
- 2014.04.16,读书,读书笔记-《Matlab R2014a完全自学一本通》-第17章 图形用户界面
界面对象分三类: 用户控件对象(uicontrol) 下拉式菜单对象(uimenu) 内容式菜单对象(uicontextmenu) 创建用户界面: 1.命令行方式 采用uicontrol来创建控件对象 ...
- <<Python基础教程>>学习笔记 | 第12章 | 图形用户界面
Python支持的工具包非常多.但没有一个被觉得标准的工具包.用户选择的自由度大些.本章主要介绍最成熟的跨平台工具包wxPython.官方文档: http://wxpython.org/ ------ ...
- java进制转换器 图形用户界面 十进制及其相反数分别转化为二,四,八,十六进制
package com.rgy.Test; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; ...
- Java第15章笔记
字符串的概述 1.什么是字符串:零个或多个字符组成的有限序列 2.如何使用字符串:(使用字符串分为两步) 1)定义并初始化字符串 2)使用字符,对字符串进行一些处理 ...
随机推荐
- vue中 $refs的基本用法
骚年,我看你骨骼惊奇,有撸代码的潜质,这里有324.57GB的前端学习资料传授于你!什么,你不信??? 先随便看几个图: 肯定没看够.再来个GIF图热个身??? 那么问题来了,如果你也想入坑前端或者学 ...
- 小老板,我学的计算机组成原理告诉我半导体存储器都是断电后丢失的,为什么U盘SSD(固态硬盘)没事呢?
什么是闪存: 快闪存储器(英语:flash memory),是一种电子式可清除程序化只读存储器的形式,允许在操作中被多次擦或写的存储器 存储原理 要讲解闪存的存储原理,还是要从EPROM和EEPROM ...
- CodeForces - 140A New Year Table (几何题)当时没想出来-----补题
A. New Year Table time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- Jenkins 构建 Jmeter 项目之源代码管理(SVN)
1.查看项目创建中是否又 svn 插件,没有的话下载插件 subversion 2.配置 svn 源代码管理,如下图(testcases 目录下包含 build.xml 和脚本文件) 3.查看 Jen ...
- Https双向验证与Springboot整合测试-人来人往我只认你
1 简介 不知不觉Https相关的文章已经写了6篇了,本文将是这个专题的最后一篇,起码近期是最后一篇.前面6篇讲的全都是单向的Https验证,本文将重点介绍一下双向验证.有兴趣的同学可以了解一下之前的 ...
- MAC使用Scrapy遇到的坑
MAC版本:EI Captain Python版本: 2.7.10 MAC默认没有安装pip,所以首先 sudo easy_install pip 然后安装Scrapy: sudo pip insta ...
- Python3 迭代器与生成器 - 学习笔记
可迭代对象(Iterable) 迭代器(Iterator) 定义 迭代器和可迭代对象的区别 创建一个迭代器 创建一个迭代器类 使用内置iter()函数 StopIteration异常 生成器(gene ...
- (技能篇)Mysql在linux下的全量热备份
相关命令: #创建备份目录 mkdir -p /mysqlbackup #进入创建的备份目录中 cd /mysqlbackup #如果mysql运行在mysql用户和用户组下面,root表示用户,my ...
- redux中间件的理解
redux的中间件就是用来处理reducer和actions之间应用,常用的中间件有redux-thunk,redux-sage.在redux中通过applyMiddleware方法使用中间件 使用例 ...
- 【T-SQL】基础——表别名
Som有时候我们需要为表设置别名,这样就可以方便的修改表. 如果在SSMS中,可以点击 Query-> SQL CMD mode --Set Alisa for the table:setvar ...