GUI_FlowLayout
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的更多相关文章
随机推荐
- mongodb连接警告修复
问题 Node.js中mongoose模块连接MongoDB数据库时提示(node:12580) DeprecationWarning: current URL string parser is de ...
- 纯手写实现ajax分页功能
前言 最近用到了这个功能,百度大半天,网上的不是有各种问题就是需要引入其他的插件,无奈,只能自己写,大致功能已经完成.前端页面用bootstrap做样式,分页功能用ajax实现,没用其他插件哦,只引入 ...
- nmbd - 向客户端提供构造在IP之上的NetBIOS名字服务的NetBIOS名字服务器
总览 SYNOPSIS nmbd [-D] [-F] [-S] [-a] [-i] [-o] [-h] [-V][-d <debug level>] [-H <lmhosts fil ...
- 十一、Boostrap-X-editable
一.官网 http://vitalets.github.io/x-editable/index.html 二.实践 在jQuery中ajax配置项中的使用type与method的区别: type 和m ...
- Zabbix--06主动模式和被动模式、低级自动发现、性能优化、
目录 一. Zabbix主动模式和被动模式 1.克隆模版 2.修改克隆后的模版为主动模式 3.修改监控主机关联的模版为主动模式 4.修改客户端配置文件并重启 5.查看最新数据 二.Zabbix低级自动 ...
- 使ApacheBench支持multi-url
目录 1.下载Apache httpd相关源码包以及针对ab工具的patch包 2.编译安装apr 3.编译安装apr-util 4.替换httpd源码里面的ab.c文件 5.编译安装httpd 6. ...
- 01JAVA入门
1 Welcome to java public class ch01Welcome { public static void main(String[] args) { System.out.pri ...
- 10年前文章_eclipse下perl环境搭建
eclipse下perl环境搭建1.Eclipse下安装perl插件Help -Software Updates…- Available .- Add Site… :http://e-p-i-c ...
- CSP-J&S 游记以及总结
前言 以前的比赛没有写什么总结,都只注重结果,没有注重过程,不知道如何才能在比赛中拿到高分,因此这次CSP才会考得一塌糊涂. 从此吸取教训,每场比赛都要总结经验,每场比赛都要尽全力考,每个题目都要尽全 ...
- SOJ 一句话题解整理
#50 离线+按位考虑 #99 %6拆成%2和%3合并+将图定向为DAG的方案数为 (-1)^n P(-1) #123 储存分数最后求逆元 #124 $\binom{2n}{n}$大概在25的时候就已 ...