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章图形用户界面设计例题的更多相关文章

  1. MATLAB学习笔记(十一)——MATLAB图形用户界面设计

    (一)菜单设计 一.建立用户菜单 1.概况: 用户菜单一般含有一级菜单和二级菜单,乃至多级菜单.每一级菜单又包含多个菜单项.建立菜单可以使用uimenu函数. 2.uimenu函数调用: %建立一级菜 ...

  2. Java基础学习总结 -- 图形用户界面GUI

    虽然目前Java算不上前端开发的主力,但是作为Java入门基础的一部分,学习Java的GUI编程还是有必要的,而且可以做出一些小且有趣的图形程序来提高学习热情.本篇学习总结均为一个Beginner的笔 ...

  3. java第八节 GUI/图形用户界面

    /* *第8讲 GUI/图形用户界面 * AWT的基础知识 * GUI全称是Graphical User Interface,即图形用户界面 * JDK中提供了AWT和Swing两个包,用于GUI程序 ...

  4. 201671010127 2016-2017-11 Java图形用户界面设计技术

    一.事件处理器 1.什么是事件处理 一个事件要求特定的动作被执行,它被作为消息由外界或系统自身发送给GUI系统.这些事件包括来自计算机设备如鼠标键盘和网络端口的I/O中断,以及GUI系统的逻辑事件触发 ...

  5. 《Java并发编程实战》第九章 图形用户界面应用程序界面 读书笔记

    一.为什么GUI是单线程化 传统的GUI应用程序通常都是单线程的. 1. 在代码的各个位置都须要调用poll方法来获得输入事件(这样的方式将给代码带来极大的混乱) 2. 通过一个"主事件循环 ...

  6. 2014.04.16,读书,读书笔记-《Matlab R2014a完全自学一本通》-第17章 图形用户界面

    界面对象分三类: 用户控件对象(uicontrol) 下拉式菜单对象(uimenu) 内容式菜单对象(uicontextmenu) 创建用户界面: 1.命令行方式 采用uicontrol来创建控件对象 ...

  7. <<Python基础教程>>学习笔记 | 第12章 | 图形用户界面

    Python支持的工具包非常多.但没有一个被觉得标准的工具包.用户选择的自由度大些.本章主要介绍最成熟的跨平台工具包wxPython.官方文档: http://wxpython.org/ ------ ...

  8. java进制转换器 图形用户界面 十进制及其相反数分别转化为二,四,八,十六进制

    package com.rgy.Test; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; ...

  9. Java第15章笔记

    字符串的概述 1.什么是字符串:零个或多个字符组成的有限序列 2.如何使用字符串:(使用字符串分为两步)          1)定义并初始化字符串          2)使用字符,对字符串进行一些处理 ...

随机推荐

  1. Android Room SQLite持久层框架

    原文链接 前言 Android中提供了SQLite数据库进行数据的持久化 ,并提供了对应API访问数据库,而Room框架提供了SQLite数据访问抽象层,为高效的数据库访问层带来便捷 APP可以缓存用 ...

  2. codeforce 227D Naughty Stone Piles (贪心+递归+递推)

    Description There are n piles of stones of sizes a1, a2, -, an lying on the table in front of you. D ...

  3. CodeForces-259B]Little Elephant and Magic Square

      Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains ...

  4. SpringBoot + MybatisPlus3.x 代码生成

    主要参考另外两篇博文,这里表示感谢 参考一(mybatisplus3.x分页) : https://www.jianshu.com/p/2ec9337dc2b0 参考二(mybatisplus2.x升 ...

  5. Java采用反射技术创建对象后对目标类的成员变量和成员方法进行访问

    实现: package com.ljy; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * * @Class ...

  6. C/S程序设计范式

    在socket编程之并发回射服务器3篇文章中,提到了3种设计范式: 多进程 父进程阻塞于accept调用,然后为每个连接创建一个子进程. 多线程 主线程阻塞于accept调用,然后为每个连接创建一个子 ...

  7. Programming Languages_04 Deferred Substitution

    Deferred Substitution 在执行出现with时,利用"substitution",每次with的出现,它都绕着整个body置换.这一方式是由F1WAE到env再到 ...

  8. MongoDB JAVA开发

    简介 MongoDB是一个基于内存的NoSql(非关系型数据库).具有NoSql的特点,读写快(key-value),不适合持久化但都提供此功能. 用途 我用来存放页面模板 用法 依赖 <dep ...

  9. 聊聊算法——BFS和DFS

    如果面试字节跳动和腾讯,上来就是先撕算法,阿里就是会突然给你电话,而且不太在意是周末还是深夜, 别问我怎么知道的,想确认的可以亲自去试试.说到算法,直接力扣hard三百题也是可以的,但似乎会比较伤脑, ...

  10. LeetCode二分专题

    二分 二分模板 两个模板:1.最大值最小模板一,2.最小值最大用模板二 单调性.两段性的性质 版本1:二分绿色端点是答案,最大值最小 int bsearch_1(int l, int r){ whil ...