void setBounds(x, y, width, height)

设置窗体坐标,窗体大小

import java.awt.Frame;

public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
Frame f = new Frame(); // 设置窗体标题
f.setTitle("HelloWorld"); // // 设置窗体大小
// f.setSize(400, 300);
//
// // 设置窗体位置
// f.setLocation(400, 200); f.setBounds(400, 200, 400, 300); // 设置窗体可见
f.setVisible(true);
}
}

设置窗体可以关闭

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
Frame frame = new Frame("窗体"); // 设置窗体属性
frame.setBounds(400, 200, 400, 300); // 适配器,设置窗体可以关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // 设置窗体可见
frame.setVisible(true);
}
}

对按钮添加事件

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; /*
* 需求:把按钮添加到窗体,并对按钮添加一个点击事件
* A:创建窗体对象
* B:创建按钮对象
* C:把按钮添加到窗体
* D:窗体显示
* */ public class IntegerDemo {
public static void main(String[] args) {
// A:创建窗体对象
Frame frame = new Frame("添加按钮"); // 设置窗体属性
frame.setBounds(400, 200, 400, 300); // 设置布局为流式布局
frame.setLayout(new FlowLayout()); // B:创建按钮对象
Button button = new Button("快点我啊");
button.setSize(20, 10); // C:把按钮添加到窗体
frame.add(button); // 适配器,设置窗体可以关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // 对按钮添加事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("你再点试试");
}
}); // D:窗体显示
frame.setVisible(true);
}
}

文本框、按钮、文本区

对按钮添加事件

数据转移,在文本框输入,并保存在文本区

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
Frame frame = new Frame("数据转移");
// 设置窗体属性
frame.setBounds(400, 200, 400, 300);
// 设置布局为流式布局
frame.setLayout(new FlowLayout()); // 创建文本框
final TextField textfield = new TextField(20); // 创建按钮
Button button = new Button("数据转移"); // 创建文本区
final TextArea textarea = new TextArea(10, 40); // 把按钮添加到窗体
frame.add(textfield);
frame.add(button);
frame.add(textarea); // 适配器,设置窗体关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // 对按钮添加事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取文本框的值
String string = textfield.getText().trim();
// 清空数据
textfield.setText("");
// 设置文本框,追加和换行
textarea.append(string + "\r\n");
// 获取光标
textfield.requestFocus();
}
}); // 窗体显示
frame.setVisible(true);
}
}

对按钮添加鼠标的进入事件

对按钮添加鼠标的离开事件

通过鼠标移动到按钮上更改背景颜色

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
Frame frame = new Frame("数据转移");
// 设置窗体属性
frame.setBounds(400, 200, 400, 300);
// 设置布局为流式布局
frame.setLayout(new FlowLayout()); // 创建3个按钮
Button redButton = new Button("红色");
Button greenButton = new Button("绿色");
Button blueButton = new Button("蓝色"); // 添加按钮
frame.add(redButton);
frame.add(greenButton);
frame.add(blueButton); // 适配器,设置窗体关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // 对按钮添加动作事件
// redButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// frame.setBackground(Color.RED);
// }
// }); // 对按钮添加点击事件
// redButton.addMouseListener(new MouseAdapter() {
// public void mouseClicked(MouseEvent e) {
// frame.setBackground(Color.RED);
// }
// }); // 对按钮添加鼠标的进入事件
redButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
frame.setBackground(Color.RED);
}
}); greenButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
frame.setBackground(Color.GREEN);
}
}); blueButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
frame.setBackground(Color.BLUE);
}
}); // 对按钮添加鼠标的离开事件
redButton.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
frame.setBackground(Color.WHITE);
}
}); greenButton.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
frame.setBackground(Color.WHITE);
}
}); blueButton.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
frame.setBackground(Color.WHITE);
}
}); // 窗体显示
frame.setVisible(true);
}
}

控制在文本框里面只能输入数字字符

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
Frame frame = new Frame("不能输入非数字字符");
// 设置窗体属性
frame.setBounds(400, 200, 400, 300);
// 设置布局为流式布局
frame.setLayout(new FlowLayout()); // 创建Label
Label label = new Label("请输入你的Q号,不能是非数字"); TextField textfield = new TextField(40); // 添加按钮
frame.add(label);
frame.add(textfield); // 适配器,设置窗体关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); textfield.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
// 思路:1获取字符,2判断字符
char ch = e.getKeyChar(); if (ch >= '0' && ch <= '9') { } else {
e.consume();
}
}
}); // 窗体显示
frame.setVisible(true);
}
}

多级菜单

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException; public class IntegerDemo {
public static void main(String[] args) {
// 创建窗体对象
final Frame frame = new Frame("多级菜单");
final String name = frame.getTitle();
// 设置窗体属性
frame.setBounds(400, 200, 400, 300);
// 设置布局为流式布局
frame.setLayout(new FlowLayout()); // 创建菜单栏
MenuBar menuBar = new MenuBar(); // 创建菜单
Menu menu1 = new Menu("文件");
Menu menu1_1 = new Menu("更改名称"); // 创建菜单项
MenuItem menuItem1 = new MenuItem("好好学习");
MenuItem menuItem2 = new MenuItem("天天向上");
MenuItem menuItem3 = new MenuItem("恢复标题");
MenuItem menuItem4 = new MenuItem("打开记事本");
MenuItem menuItem5 = new MenuItem("退出系统"); // 添加菜单
menu1_1.add(menuItem1);
menu1_1.add(menuItem2);
menu1_1.add(menuItem3); menu1.add(menu1_1);
menu1.add(menuItem4);
menu1.add(menuItem5); menuBar.add(menu1); // 设置菜单项
frame.setMenuBar(menuBar); // 适配器,设置窗体关闭
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); menuItem1.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setTitle(menuItem1.getLabel());
}
}); menuItem2.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setTitle(menuItem1.getLabel());
}
}); menuItem3.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.setTitle(name);
}
}); menuItem4.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Runtime runTime = Runtime.getRuntime();
try {
runTime.exec("notepad");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}); menuItem5.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
} }); // 窗体显示
frame.setVisible(true);
}
}

GUI_FlowLayout的更多相关文章

随机推荐

  1. Sql 字符串自增列的实现

    ALTER FUNCTION [dbo].[f_NextID](@tabname VARCHAR()) RETURNS ) AS BEGIN DECLARE @charval CHAR() IF LO ...

  2. JavaScript生成简单数字验证码

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  3. MySQL索引的分类、结构、使用场景

    MySQL索引分类 1.主键索引:设定为主键后数据库会自动建立索引,innodb为聚簇索引 语法: 随表一起建索引: CREATE TABLE customer (id INT(10) UNSIGNE ...

  4. Nginx1.3.15导致Wordpress,Drupal等框架无限重定向的解决方案

    Wordpress建立的站点出现无限循环重定向问题.很多人遇到这个问题,并不是单纯Wordpress,Drupal, PHPCake等框架也都遇到同样的问题. 新版本的Nginx在收到 http:// ...

  5. The Complex Inversion Formula. Bromwich contour.

    网址:http://www.solitaryroad.com/c916.html

  6. JavaScript设计模式 样例二 —— 策略模式

    策略模式(Strategy Pattern): 定义:定义了一族算法: 封装了每个算法: 这族的算法可互换代替. 目的:将算法的使用与算法的实现分离开来. 场景:可用来消除大量的条件分支语句. 例:J ...

  7. DevExpress v19.1新版亮点——WinForms篇(二)

    行业领先的.NET界面控件DevExpress v19.1终于正式发布,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WinForms v19.1中新增的一些控 ...

  8. 字符编码 python2与python3的区别

    目录 1. 字符编码 2. 文本编辑器存储信息的过程 3. 编码: 1. 编码的历史 2. gb2312和gbk的区别 3. 编码和解码 4. python解释器 解释代码的流程 1. 读取文本到解释 ...

  9. 【05】Python 标准模块:random、os、time、hashlib 第三方模块:excel、数据库 列表生成式

    1 模块分类 标准模块,不需要你单独安装,python自带的模块 第三方模块 自己写的python 一个python文件就是一个模块 2 random模块 2.1 随机取元素 import rando ...

  10. Python---进阶---文件操作---搜索文件和保存搜索结果

    ### 编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在,如果遇到文件夹,则进入该文件夹继续搜索 - input 去接受用户输入的文件名和开始搜索的路径 - os.path.isdi ...