GUI编程1
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具
1、简介
Gui的核心技术:Swing AWT
1.因为界面不美观。
2需要jre环境!
为什么我们要学习?
- 可以写出自己心中想要的一些小工具
- 工作时候,也可能需要维护到swing界面,概率极小!
- 了解MVC架构,了解监听!
2、AWT
2.1、Awt介绍
1.包含了很多类和接口!GUI !
2.元素:窗口,按钮,文本框
3 java.awt
2.2、组件和容器
1、Frame
//GUI的第一个界而
public class TestFrame {
public static void main(String[] args) {
//Frame.JDK,看源码!
Frame frame = new Frame("我的第一个java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置颜色背景
frame.setBackground(new Color(71, 234, 106));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
问题:发现窗口关闭不掉,停止java程序!
尝试回顾封装:
package demo01;
import java.awt.*;
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口new
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.black);
MyFrame myFrame3 = new MyFrame(300, 100, 200, 200, Color.darkGray);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.cyan);
}
}
class MyFrame extends Frame{
static int id=0;//可能存在多个窗口,我们需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame+"+(++id));
setVisible(true);
setBounds(x,y,w,h);
setBackground(color);
//setResizable(false);
}
}
2、面板Panel
解决了关闭事件!
package demo01;
// PaneL可以看成是一个空间,但是不能单独存在
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel1 {
public static void main(String[] args) {
Frame frame = new Frame();
//布局的概念
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(Color.cyan);
//panel设置坐标,相对于frame
panel.setBounds(100,100,400,400);
panel.setBackground(new Color(231, 25, 25));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件system.exit(e)
// 适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
3、布局管理器
-
流式布局
public static void main(String[] args) { Frame frame = new Frame(); //组件-按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 //frame.setLayout(new FlowLayout()); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); frame.setBounds(500,500,500,500); frame.setVisible(true); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); }
-
东西南北中
package demo01; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setVisible(true); frame.setBounds(500,500,500,500); } }
-
表格布局
package demo01;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//自动优化布局
frame.setVisible(true);
frame.setBounds(500,500,500,500);
}
}
package demo01;
import java.awt.*;
public class EXDay91600 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(500,500);
frame.setLocation(400,400);
frame.setBackground(Color.CYAN);
frame.setVisible(true);
//上下一分为二
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel panel1 = new Panel(new BorderLayout());//边界,包边
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));
//上方ok
panel1.add(new Button("east-1"),BorderLayout.EAST);
panel1.add(new Button("west-1"),BorderLayout.WEST);
panel2.add(new Button("p2-btn-1"));
panel2.add(new Button("p2-btn-2"));
panel1.add(panel2,BorderLayout.CENTER);
//下方ok
panel3.add(new Button("east-1"),BorderLayout.EAST);
panel3.add(new Button("west-1"),BorderLayout.WEST);
panel4.add(new Button("p2-btn-1"));
panel4.add(new Button("p2-btn-2"));
panel4.add(new Button("p2-btn-3"));
panel4.add(new Button("p2-btn-4"));
panel3.add(panel4,BorderLayout.CENTER);
//把面板放入框架
frame.add(panel1);
frame.add(panel3);
}
}
总结:
1.Frame是一个顶级窗口
2.Panel无法单独显示,必须添加到某个容器中。
3.布局管理器
- 流式
- 东西南北中
- 表格
4.大小,定位,背景颜色,可见性,监听!
I
4、事件监听
事件监听:当某个事情发生的时候,干什么?
package demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
//按下按钮,触发一些事件
//因为,addActionListener()需要一个ActionListener,所以我们需要构造一个 ActionListener
Button button = new Button("btn");
MyActionListenter myActionListenter = new MyActionListenter();
button.addActionListener(myActionListenter);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setBounds(500,500,500,500);
windowClose(frame);
}
//关闭窗体的事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class MyActionListenter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaaaaa");
}
}
多个按钮,共享一个事件
package demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent2 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.pack();
frame.setVisible(true);
frame.setBounds(500,500,500,500);
//两个按钮,实现同一个监听
// 开始 停止
Button start = new Button("start");
Button stop = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值!
//可以多个按钮只写一个监听类
stop.setActionCommand("000");
MyMonitor myMonitor = new MyMonitor();
start.addActionListener(myMonitor);
stop.addActionListener(myMonitor);
frame.add(start,BorderLayout.NORTH);
frame.add(stop,BorderLayout.SOUTH);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击:msg--"+e.getActionCommand());
}
}
2.5、输入框TextField 监听
package demo03;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField= (TextField) e.getSource();//获得一些资源,返回的一个对象
System.out.println(textField.getText());//获得输入框的文本
textField.setText("");
}
}
======
package demo03;
import java.awt.*;
public class MyFrame extends Frame {
public MyFrame() {
TextField textField = new TextField();
add(textField);
///监听这个文本框输入的文字
MyActionListener myActionListener = new MyActionListener();
//按下enter就会触发这个输入框的事件
textField.addActionListener(myActionListener);
//设置替换编码
textField.setEchoChar('*');
}
{
setBounds(300,300,500,500);
setBackground(new Color(100, 209, 179));
setVisible(true);
//pack();
}
}
=======
package demo03;
public class Day91700 {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}
2.6、简易计算器,组合+内部类回顾复习!
oop原则:组合,大于继承!
package demo03;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalcListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//布局
setLayout( new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalcListener implements ActionListener {
//获取3个变量
private TextField num1,num2,num3;
public MyCalcListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1。获得加数和被加数
int n1=Integer.parseInt(num1.getText());
int n2=Integer.parseInt(num2.getText());
//2。将这个值+法运算后,放到第三个框
num3.setText(""+(n1+n2));
// 3.清除前两个框
num1.setText("");
num2.setText("");
}
}
2.7、画笔
package demo04;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
MyPaint myPaint = new MyPaint();
myPaint.loadFrame();
//myPaint.paint();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(300,300,500,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {//paint应该是在父类的构造方法里被调用了,所以只要创建实例就会被调用
//画笔,需要有颜色,画笔可以画画
g.setColor(Color.cyan);
g.drawOval(100,100,100,50);//空心椭圆
g.fillOval(100,100,100,50);//实心椭圆
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
2.8、鼠标监听
目的:想要实现鼠标画画!
步骤
-
监听
-
addMouseListener(new MyMouseListener());
-
new 一个监听类
private class MyMouseListener extends MouseAdapter
-
重写鼠标的操作方法
-
将鼠标的操作传到框架中
MyFrame myFrame=(MyFrame) e.getSource();
-
找到点的位置坐标
-
存入数组
-
myFrame.repaint();//刷新
-
-
-
存点
- ArrayList points;
-
显示
-
框架面板
- 尺寸,大小,可见性,new数组
-
数组的点显示出去
- 使用迭代器将数组的点画出来
-
package demo04;
//鼠标监听事件
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseLintener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,500,500);
//存鼠标点击的点
points=new ArrayList();
//鼠标监听器,正对这个窗口
addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point=(Point) iterator.next();
g.setColor(Color.cyan);
g.fillOval(point.x,point.y,5,5);
}
}
//适配器模式
private class MyMouseListener extends MouseAdapter{
//鼠标按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame=(MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
// /这个点就是鼠标的点;
Point point = new Point(e.getX(), e.getY());
//添加一个点到界面上
points.add(point);
///每次点击鼠标都需要重新画一遍
myFrame.repaint();//刷新
}
}
}
2.9、窗口监听
2.10、键盘监听
package demo04;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(100,100,300,300);
frame.setVisible(true);
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//获得键盘下的键是哪一个,当前的码
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode==KeyEvent.VK_UP){
System.out.println("wwwwwwwww");
}
}
});
}
}
3、Swing
3.1、窗口、面板
package demo05;
import javax.swing.*;
import javax.swing.text.PasswordView;
import java.awt.*;
public class JFrameDay91800 {
//init();初始化
public void init(){
JFrame frame = new JFrame("JFrame00");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
//设置文字JLable
JLabel label = new JLabel("jyvcvjyfvj");
frame.add(label);
//让文本标签居中设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
///获得一个容器
Container container = frame.getContentPane();
container.setBackground(Color.green);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDay91800().init();
}
}
3.2、弹窗
JDialog,用来被弹出,默认就有关闭事件!
package demo05;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JFrameDay91801 {
public static void main(String[] args) {
new DialogDemo();
}
}
//主窗口
class DialogDemo extends JFrame{
public DialogDemo() {
setBounds(200,200,500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//容器
Container container = getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹出一个对话框");
jButton.setBounds(100,100,50,50);
///点击这个按钮的时候,弹出一个弹窗
jButton.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(jButton);
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
setVisible(true);
setBounds(300,300,300,300);
// setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);多余的
Container container = getContentPane();
container.setLayout(null);
container.add(new JLabel("5555555555"));
}
}
3.3、标签
package demo05;
import javax.swing.*;
import java.awt.*;
//图标,需要实现类,Frame继承
public class IconDemo1 extends JFrame implements Icon {
public static void main(String[] args) {
new IconDemo1().init();
}
private int weight;
private int height;
public IconDemo1() {
}
public IconDemo1(int weight, int height) {
this.weight = weight;
this.height = height;
}
public void init(){
IconDemo1 iconDemo1 = new IconDemo1(15, 15);
//图标放在标签,也可以放在按钮上!
JLabel label = new JLabel("icontest", iconDemo1, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,weight,height);
}
@Override
public int getIconWidth() {
return weight;
}
@Override
public int getIconHeight() {
return height;
}
}
3.4、面板
package demo05;
import javax.swing.*;
import java.awt.*;
public class JPanelTest extends JFrame {
public JPanelTest() throws HeadlessException {
Container container = getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面的参数的意思,间隔距
JPanel jPanel = new JPanel(new GridLayout(1,3));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("1"));
container.add(jPanel);
setVisible(true);
setBounds(300,300,300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelTest();
}
}
/文本域
TextArea textArea = new JTextArea( rows: 20,columns: 50);textArea.setText("欢迎学习狂神说Java"');
/ /scroLL面板
JscrollPane scrollPane = new JScrollPane(textArea);container.add (scrollPane);
3.5、按钮·
图片按钮
public JButtonDemoe1( i
Container container = this.getContentPane();/将一个图片变为图标
URL resource = JButtonDemo01.class.getResource(iname:"tx.jpg");Icon icon = new ImageIcon(resource);
//把这个图标放在按钮上
JButton button = new JButton();button. setIcon(icon);
button. setToolTipText("图片按钮");
//add
container.add( button);
this.setVisible(true);this.setsize(500, 300);
this.setDefaultcloseOperation (windowConstants.EXIT_ON_CLOSE);
单选按钮·
package demo06;
import javax.swing.*;
import java.awt.*;
public class Day92100 extends JFrame {
public Day92100() throws HeadlessException {
Container container = getContentPane();
//单选框
JRadioButton jRadioButton1 = new JRadioButton("aaaaaaaaa");
JRadioButton jRadioButton2 = new JRadioButton("bbbbbbbbb");
JRadioButton jRadioButton3 = new JRadioButton("ccccccccc");
// 由于单选框只能选样一个,分组,一个组中只能选择一个
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Day92100();
}
}
复选按钮
package demo06;
import javax.swing.*;
import java.awt.*;
public class Day92101 extends JFrame {
public Day92101() throws HeadlessException {
Container container = getContentPane();
//多选框
JCheckBox jCheckBox1 = new JCheckBox("11111");
JCheckBox jCheckBox2 = new JCheckBox("2222");
container.add(jCheckBox1,BorderLayout.NORTH);
container.add(jCheckBox2,BorderLayout.SOUTH);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Day92101();
}
}
3.6、列表
下拉框
package demo06;
import javax.swing.*;
import java.awt.*;
public class Day92102 extends JFrame {
public Day92102() throws HeadlessException {
Container container = getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("111");
jComboBox.addItem("222");
jComboBox.addItem("333");
container.add(jComboBox);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Day92102();
}
}
列表框
package demo06;
import javax.swing.*;
import java.awt.*;
public class Day92103 extends JFrame {
public Day92103() throws HeadlessException {
Container container = getContentPane();
//生成列表的内容
String[] contents ={"1","2","3"};
//列表中需要放入内容
JList jList = new JList(contents);
container.add(jList);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Day92103();
}
}
·应用场景
- 选择地区,或者一些单个选项。
- 列表,展示信息,一般是动态扩容!
3.7、文本框
·文本框
package demo06;
import javax.swing.*;
import java.awt.*;
public class Day92104 extends JFrame {
public Day92104() throws HeadlessException {
Container container = getContentPane();
JTextField jTextField1 = new JTextField("zzzzzzzzz");
JTextField jTextField2 = new JTextField("ffff");
container.add(jTextField1,BorderLayout.NORTH);
container.add(jTextField2,BorderLayout.SOUTH);
setVisible(true);
setBounds(300,300,400,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Day92104();
}
}
·密码框
JPasswordField passwordField = new JPasswordField): //**
passwordField.setEchochar( '*");
文本域
标签:GUI,编程,Button,add,frame,import,new,public From: https://www.cnblogs.com/xin-zhi-suo-xiang/p/17720606.html