Java GUI编程
----基础
// 创建一个窗体对象
JFrame frame = new JFrame();
// 设置窗口大小
frame.setSize(300, 200);
// 指定窗口的显示位置
frame.setLocation(300, 200);
// 指定窗口的标题栏
frame.setTitle("MY FRIST WINDOWS!");
// 获取窗口的内容面板
Container c = frame.getContentPane();
// 设置内容面板的背景色
Color color = new Color(10, 30, 20);
c.setBackground(color);
// 显示窗口
frame.setVisible(true);
// 设置当关闭窗口是同时结束程序。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
JButton btn = new JButton("按钮1");
// 将组件添加到窗体中。。。
this.add(btn, BorderLayout.NORTH);
this.add(btn1, BorderLayout.WEST);
//this.add(btn2, BorderLayout.SOUTH);
//this.add(btn3, BorderLayout.EAST);
this.add(btn4, BorderLayout.CENTER);
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式(流式布局)
this.setLayout(new FlowLayout(FlowLayout.LEADING));
this.setLayout(new GridLayout(4, 4));
-----小应用
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class Test07 extends JFrame {
JTextField txtNum;
JLabel label;
JButton btn1, btn2, btn3;
JPanel panel;
int num = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Test07 frame = new Test07();
}
private void InitControl() {
txtNum = new JTextField(20);
label = new JLabel("请随机输入一个数(1-100)");
btn1 = new JButton("猜数字。。。");
btn1.addActionListener(new BtnClick());
btn2 = new JButton("重新玩。。。");
btn3 = new JButton("退出。。。");
panel = new JPanel();
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
this.add(txtNum, BorderLayout.NORTH);
this.add(label, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);
}
public Test07() {
InitControl();
// 产生一个随机数。。。
Random random = new Random();
// 产生一个随机数。。。
num = random.nextInt();
this.setSize(300, 200);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class BtnClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 区分点击那个按钮,获取事件源。。。
JButton btnclick = (JButton) e.getSource();
// 获取按钮上的文字
String text = btnclick.getText();
if (text.equals("猜数字。。。")) {
int input = Integer.valueOf(txtNum.getText());
if (input == num) {
label.setText("恭喜你!表演节目!!!");
} else if (input < num) {
label.setText("猜小了!!!");
} else if (input > num) {
label.setText("猜大了!!!");
} else if (text.equals("重新玩。。。")) {
// 产生一个随机数。。。
Random random = new Random();
// 产生一个随机数。。。
num = random.nextInt() + 1;
} else {
System.exit(0);
}
}
}
}
}
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test08 extends JFrame {
JButton[][] btns = new JButton[10][10];
// 默认色
Color defaultcolor = null;
JButton btn = new JButton();
public static void main(String[] args) {
// TODO Auto-generated method stub
Test08 frame = new Test08();
}
// 初始化窗体中所需的组件
private void InitControl() {
for (int i = 0; i < btns.length; i++) {
for (int j = 0; j < btns[i].length; j++) {
JButton btn = new JButton();
// 给按钮添加所在的行和列的信息,存入command中。。。
btn.setActionCommand(i + "," + j);
// 注册按钮的单击事件
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取单击按钮
JButton btnclick = (JButton) e.getSource();
// 获取按钮的命令参数(就是这个按钮在矩阵中的行号和列号)
String command = btnclick.getActionCommand();
// 使用字符分割得到按钮的行号和列号
String[] strs = command.split(",");
// 获取整形的行号和列号
int row = Integer.parseInt(strs[0]);
int col = Integer.parseInt(strs[1]);
// 声明上下左右的按钮
JButton topbtn = null;
JButton downbtn = null;
JButton leftbtn = null;
JButton rigthbtn = null;
// 获取上方按钮
if (row - 1 >= 0) {
topbtn = btns[row - 1][col];
}
// 获取左边的按钮
if (col - 1 >= 0) {
leftbtn = btns[row][col - 1];
}
// 获取下方的按钮
if (row + 1 < btns.length) {
downbtn = btns[row + 1][col];
}
// 获取右边的按钮
if (col + 1 < btns.length) {
rigthbtn = btns[row][col + 1];
}
JButton[] arrbtns = new JButton[5];
arrbtns[0] = btnclick;
arrbtns[1] = topbtn;
arrbtns[2] = downbtn;
arrbtns[3] = leftbtn;
arrbtns[4] = rigthbtn;
changColor(arrbtns);
/*
* btnclick.setBackground(Color.red);
* topbtn.setBackground(Color.red);
* downbtn.setBackground(Color.red);
* leftbtn.setBackground(Color.red);
* rigthbtn.setBackground(Color.red);
*/
}
});
btns[i][j] = btn;
this.add(btn);
}
}
}
// 改变按钮和周围颜色
private void changColor(JButton[] nearbtns) {
for (int i = 0; i < nearbtns.length; i++) {
if (nearbtns[i] != null) {
if (nearbtns[i].getBackground() == Color.red) {
// 变成原来的颜色
nearbtns[i].setBackground(defaultcolor);
} else {
nearbtns[i].setBackground(Color.red);
}
}
}
}
// 构造方法
public Test08() {
InitControl();
this.setLayout(new GridLayout(10, 10));
// 获取按钮的默认色
defaultcolor = btn.getBackground();
this.setSize(300, 350);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Java GUI编程的更多相关文章
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- Java GUI编程中AWT/swing/SWT的优缺点
http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...
- Java GUI编程4---标签组件JLabel
Java GUI编程4---标签组件JLabel 2018年06月11日 22:06:58 蓝蓝223 阅读数 12103更多 个人分类: Java书籍摘抄 所属专栏: Java Swing图形界面 ...
- java Gui编程 事件监听机制
1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式: 1. 命令交互方式 图书管理系统 ...
- java GUI编程二
java基础学习总结--GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import j ...
- Java GUI编程-(项目代码_扫雷_弹钢琴)
--扫雷 package com;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionLis ...
- [Java] GUI编程基础 绘图
库 swing awt 过程 创建窗口JFrame JFrame-->MenuBar-->Container 屏幕坐标系:左上角为原点 Graphics2D Main.java 1 imp ...
- Java GUI编程SwingUtilities.invokeLater作用
1 http://blog.micxp.com/index.php/archives/109/ 2
- 实验十五 GUI编程练习与应用程序部署
实验十五 GUI编程练习与应用程序部署 实验时间 2018-12-6 一:理论部分 1.Java 程序的打包:编译完成后,程序员将.class 文件压缩打包为 .jar 文件后,GUI 界面序就可以 ...
随机推荐
- leetcode 28
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- CSS/HTML 改变鼠标指针形状
改变鼠标指针形状的方法有两种:第一种:用的来改变鼠标指针形状.另一种是:利用第三方控件的方法,而我自己最常用的是第一种:用css样式表来改变鼠标指针形状 我们先来看第一种:用来改变鼠标指针形状. 有些 ...
- Python中递归的最大次数
实际应用中遇到了一个python递归调用的问题,报错如下: RuntimeError: maximum recursion depth exceeded while calling a Python ...
- Notepad++自动刷新文本
现在的日志信息往往都是打印在硬盘上,而不是保存到线上,所以我们常常会使用notepad++来查看硬盘上的文本文件 这时往往会出现两个问题 (1)在notepad++长时间最小化后,再次打开会提示是否下 ...
- 如何查看oracle数据库的所有的关键字
管理员账户登录后,执行以下命令: select * from v$reserved_words 附上参考: NOMONITORINGRECORDS_PER_BLOCKCASCADEDYNAMIC_S ...
- java线程池ThreadPoolExecutor理解
Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...
- VMWare虚拟机NAT上网方法 亲测可用
首先虚拟机的网卡要选择NAT 然后 在Virtual Network Editor中一定选上DHCP功能. 还要主机的服务必须开启.主机的VMnet8对应NAT模式,这个VMnet8的配置保持默认不要 ...
- 通过队列解决Lucene文件并发创建索引
public sealed class SearchIndexManager { private static readonly SearchIndexManager searchIndexManag ...
- 数据分析 - 开放街道地图(OpenStreetMap)
数据分析 - 开放街道地图(OpenStreetMap) Reinhard使用OpenStreetMap的开放地图数据作为本次数据分析的数据源,使用Python进行数据清洗,使用MongoDB进行数据 ...
- JS 获取url参数
<html> <body> 浏览器中输入 http://localhost/test/js_url.php?a1=aaa&a2=bbb&a3=ccc 会依次弹出 ...