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)使用字符,对字符串进行一些处理 ...
随机推荐
- 乾颐堂7月HCIE、CCIE通过名单
拼多多都上市了,现在很多培训机构也流行公用一张PASS了,山寨总是山寨的,不脚踏实地总是欺骗自己7月(自然月)乾颐堂通过22名学员,每个考试日通过一名HCIE.CCIE 转载于:https://blo ...
- 运用shell脚本 执行sftp,ftp命令
sftp文件上传(从本地上传到远程) #!/bin/bash #远程上传文件测试 if [ $# -ne 2 ] then echo "miss arguments" echo & ...
- 面向对象第四单元(UML)总结
OO第四单元 一.总结本单元两次作业的架构设计 第一次作业 架构 第一次作业只有类图,所以全部的UmlElement都可以放在MyUmlInteraction中进行存储.计算和查找.对于类图来说,可以 ...
- 设置 Linux 支持中文
1.首先在 command 输入 locale,可以看到 Linux 下默认的系统语言的是英文 2.vim ~/.bashrc 打开这个文件,该文件夹相当于系统配置文件 3.打开后,将后三行命令输入到 ...
- Jmeter 结构体系及运行顺序
一.Jmeter 运行原理: Jmeter 时以线程的方式来运行的(由于Jmeter 是 java 开发的所以是运行在 JVM 虚拟机上的,java 也是支持多线程的) 二.Jmeter 结构体系 1 ...
- OpenCV 4下darknet修改
darknet的安装使用直接在官网上获取.https://pjreddie.com/darknet/ 但我用的是OpenCV4.1.1,make时会在image_opencv.cpp中有两个错误. 1 ...
- 使用EF Code First生成模型,如何让时间字段由数据库自动生成
场景:保存记录时需要时间字段,该时间如果由前台通过DateTime.Now产生,存在风险,比如修改客户端的系统时间,就会伪造该记录的生成时间.因此,需要在保存记录时,由后台自动赋予具体的时间. 实现方 ...
- Web(4)servlet
一.servlet.GenericServlet.HttpServlet 1.servlet具有四个生命周期方法 特性:单例模式,线程不安全,效率高 2.servletConfig接口对应根元素对应的 ...
- 1005 Spell It Right (20分)
1005 Spell It Right (20分) 题目: Given a non-negative integer N, your task is to compute the sum of all ...
- Codeforces 832D(Misha, Grisha and Underground,LCA)
题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...