GUI
1 简介
GUI核心技术:Swing、AWT
说明:界面不美观,需要jre环境(不常用)
2 AWT技术
2.1AWT介绍
2.2组件和容器
窗口
public class FirstFrame {
public static void main(String[] args) {
Frame frame = new Frame("first gui");
// 需要设置可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400, 400);
//设置背景颜色 Color
frame.setBackground(Color.lightGray);
//弹出的初始位置
frame.setLocation(200, 200);
//设置大小固定
frame.setResizable(false);
}
}
窗口无法关闭;停止java程序运行
- 多个窗口
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口 bew
MyFrame MyFrame1 = new MyFrame (100,100,200,200,Color.lightGray);
MyFrame MyFrame2 = new MyFrame (100,100,200,200,Color.lightGray);
MyFrame MyFrame3 = new MyFrame (100,100,200,200,Color.lightGray);
MyFrame MyFrame4 = new MyFrame (100,100,200,200,Color.lightGray);
}
}
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);
}
}
面板
public class TestPanle {
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.green);
//设置坐标,先对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(15, 193, 101));
//frame.add(panel);添加panel
frame.add(panel);
frame.setVisible(true);
//监听实践,监听窗口关闭 System,exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
窗口关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
2.3布局管理器
流式布局
public class TestFlowLayoout {
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.setLayout(new FlowLayout(FlowLayout.RIGHT));//右
frame.setSize(200,200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
东西南北中
public class TasteBorderayout {
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.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
表格布局
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
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();//java函数 自动布局
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
作业
- 最终版
ublic class HomeWork {
public static void main(String[] args) {
//总窗
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setBounds(400,300,300,400);
frame.setBackground(Color.green);
frame.setVisible(true);
//四个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
//按钮
//上面
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
p4.add(new Button("p4-btn-1"));
p4.add(new Button("p4-btn-2"));
p4.add(new Button("p4-btn-3"));
p4.add(new Button("p4-btn-4"));
p3.add(p4,BorderLayout.CENTER);
//放入面板
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
总结:
1.Frame是一个顶级窗口2.Panel无法单独显示,必须添加到某个容器中。
3.布局管理器
流式
东西南北中
表格
4.大小,定位,背景颜色,可见性,监听!
2.4事件监听
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发一些事件。
Frame frame = new Frame();
frame.setSize(1000,1000);
frame.setLocation(400,400);
Button button = new Button("wdnmd");
//需要addActionListener()需要要给ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
//关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public class TestActionEvent2 {
public static void main(String[] args) {
//俩个按钮,实现同一个接口
///开始 停止
Frame frame = new Frame("开始-停止");
MyMonitor myMonitor = new MyMonitor();
Button b1 = new Button("start");
Button b2 = new Button("stop");
b1.addActionListener(myMonitor);
b2.addActionListener(myMonitor);
frame.add(b1,BorderLayout.NORTH);
frame.add(b2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了:nsg"+e.getActionCommand());
}
}
2.5输入框
public class TestText01 {
public static void main(String[] args) {
// 启动!
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField textField = new TextField();
add(textField);
setSize(400,400);
setLocation(100,100);
//监听文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下enter 就会触发这个输入框里面的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField) e.getSource();//获得一些资源,返回了一个对象。
System.out.println(field.getText());//获得输入文本框
field.setText(null);//设置回车就清空文本
}
}
2.6计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrrame();
}
}
//计算类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrrame(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener());
//1个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
//自动布局
pack();
//启用关闭按钮
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
setVisible(true);
}
//监听器类
//内部类最大的好处,就是可以畅通无阻的访问外部类的属性和方法。
private class MyCalculatorListener implements ActionListener{
@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(null);
num2.setText(null);
}
}
}
2.7画笔
public class TestPanint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔,需要有颜色,可以画画
g.setColor(Color.lightGray);
//圆
g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100);//实心的圆
//正方形
g.setColor(Color.green);
g.fillRect(150,150,200,200);
//画笔用完,还原到最初的颜色
}
}
2.8鼠标监听
目的:实现鼠标画画
//鼠标监听
public class TestMouse {
public static void main(String[] args) {
new MyFrame("画图");
}
//鼠标类
static class MyFrame extends Frame {
//画画需要画笔,需要监听鼠标当前的位置
// 需要集合来存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200, 200, 400, 400);
//存鼠标的点
points = new ArrayList<>();
// 鼠标监听器,针对这个窗口
this.addMouseListener(new MyMouse());
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x, point.y, 5, 5);
//画点时显示的其实是实心圆
}
}
// 添加一个点到界面上,写一个方法
public void addPoint(Point point) {
points.add(point);
}
//适配器模式
private class MyMouse extends MouseAdapter {
//鼠标 按下,弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
//当我们点击的时候就会在界面上产生一个点
// 这个点就是鼠标的点
myFrame.addPoint(new Point(e.getX(), e.getY()));
//每一次点击鼠标都需要重新画一遍
myFrame.repaint();
//刷新
}
}
}
}
2.9窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.lightGray);
setBounds(200,200,400,400);
setVisible(true);
addWindowListener(new MyWindowListenter());
}
}
class MyWindowListenter extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);//正常退出
//通过点击按钮隐藏setVisible(false);
}
}
匿名内部类
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.lightGray);
setBounds(200,200,400,400);
setVisible(true);
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
@Override
public void windowOpened(WindowEvent e) {
super.windowOpened(e);
}
@Override
public void windowIconified(WindowEvent e) {
super.windowIconified(e);
}
@Override
public void windowDeiconified(WindowEvent e) {
super.windowDeiconified(e);
}
@Override
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
}
@Override
public void windowDeactivated(WindowEvent e) {
super.windowDeactivated(e);
}
@Override
public void windowStateChanged(WindowEvent e) {
super.windowStateChanged(e);
}
@Override
public void windowGainedFocus(WindowEvent e) {
super.windowGainedFocus(e);
}
@Override
public void windowLostFocus(WindowEvent e) {
super.windowLostFocus(e);
}
}
);
}
}
2.10键盘监听
public class Lesson06 {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(100,200,300,400);
setVisible(true);
//键盘监听内部类
this.addKeyListener(
new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个
int KeyCode = e.getKeyCode();//获得键盘是哪个健
if(KeyCode == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
//窗口监听内部
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
}
);
}
}
3.Swing技术
3.1窗口
public class JFrameDemo {
//init();初始化
public void init(){
//顶级窗口
JFrame jf = new JFrame("这是一个JFram窗口");
jf.setVisible(true);
jf.setBounds(100,100,200,200);
jf.setBackground(Color.white);
//设置文字Jlabel
JLabel label = new JLabel("天哪");
label.setHorizontalAlignment(SwingConstants.CENTER);//居中
jf.add(label);
//关闭事件
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo().init();
}
}
3.2弹窗
//主窗口
public class DialogDemo extends JFrame{
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
//JFrame 放东西,容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,30,200,60);
//点击这个按钮的时候,弹出一个窗口
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("坏银"));
}
}
3.3标签
Label
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
//获取图片的地址
JLabel label = new JLabel();
URL url = ImageIconDemo.class.getResource("1.jpg");//图片链接
ImageIcon imageIcon = new ImageIcon(url);//图片对象
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
3.4面板
public class JScrollDemo extends JFrame {
public JScrollDemo(){
Container container = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20 ,50);
textArea.setText("坏银哦");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setBounds(100,100,400,400);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
JScrollPonel
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将图片编程图标
URL resource = JButtonDemo01.class.getResource("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);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
3.5按钮
public class Demo extends JFrame{
public void init(){
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(500,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
//单选框
JRadioButton radioButton1 = new JRadioButton("JB1");
JRadioButton radioButton2 = new JRadioButton("JB2");
JRadioButton radioButton3 = new JRadioButton("JB3");
//由于单选框只能选一个,分组,一个组中只能选一个(单选即不分组)
ButtonGroup g = new ButtonGroup();
g.add(radioButton1);
g.add(radioButton2);
g.add(radioButton3);
jf.add(radioButton1,BorderLayout.NORTH);
jf.add(radioButton2,BorderLayout.CENTER);
jf.add(radioButton3,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Demo().init();
}
}
-
单选
package com.lijin.demo05; import javax.swing.*; import java.awt.*; import java.net.URL; //多选 public class TestJButton1 extends JFrame { public TestJButton1() { Container container = this.getContentPane(); //单选框 JRadioButton jRadioButton1 = new JRadioButton("1"); JRadioButton jRadioButton2 = new JRadioButton("2"); //由于单选框只能选一个所以我们要分组,一个组中只能选一个 JRadioButton jRadioButton3 = new JRadioButton("3"); ButtonGroup group = new ButtonGroup(); group.add(jRadioButton1); group.add(jRadioButton2); group.add(jRadioButton3); container.add(jRadioButton1,BorderLayout.CENTER); container.add(jRadioButton2,BorderLayout.NORTH); container.add(jRadioButton3,BorderLayout.SOUTH); setVisible(true); setBounds(100,100,300,300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestJButton1(); } }
- 复选
package Lesson20;
import javax.swing.*;
import java.awt.*;
public class Demo extends JFrame{
public void init(){
JFrame jf = new JFrame();
jf.setVisible(true);
jf.setSize(500,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
//多选框,即不分组
JRadioButton radioButton1 = new JRadioButton("JB1");
JRadioButton radioButton2 = new JRadioButton("JB2");
JRadioButton radioButton3 = new JRadioButton("JB3");
jf.add(radioButton1,BorderLayout.NORTH);
jf.add(radioButton2,BorderLayout.CENTER);
jf.add(radioButton3,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Demo().init();
}
}
3.6 列表
- 下拉框
public class Demo1 extends JFrame {
public Demo1() throws HeadlessException {
Container container = this.getContentPane();
JComboBox status = new JComboBox();//下拉框方法
status.addItem(null);
status.addItem("12312312312");
status.addItem("123123123");
status.addItem("123123123");
container.add(status);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo1();
}
}
应用:选择地区,或一些单个选项
- 列表
public class Demo2 extends JFrame{
public Demo2() throws HeadlessException {
Container container = this.getContentPane();
//生成列表内容
//String[] contents = {"1","2","3"};
Vector contents = new Vector();
//列表中的内容
JList jList = new JList(contents);
contents.add("张三");
contents.add("李四");
contents.add("王五");
container.add(jList);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo2();
}
}
3.7文本框
- 文本框
public class Demo3 extends JFrame{
public Demo3() throws HeadlessException {
Container container = this.getContentPane();
JTextField textField = new JTextField("hello");
JTextField textField1 = new JTextField("world",20);
container.add(textField,BorderLayout.NORTH);
container.add(textField1,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo3();
}
}
- 密码框
public class Demo4 extends JFrame{
public Demo4() throws HeadlessException {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('+');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo4();
}
}
- 文本域
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container c = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("坏银的文本域");
//Scroll面板
JScrollPane sp = new JScrollPane(textArea);
c.add(sp);
this.setVisible(true);
this.setBounds(100,100,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('+');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo4();
}
}
[外链图片转存中...(img-adOpTqsC-1629445037380)]
- 文本域
```java
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container c = this.getContentPane();
//文本域
JTextArea textArea = new JTextArea(20,50);
textArea.setText("坏银的文本域");
//Scroll面板
JScrollPane sp = new JScrollPane(textArea);
c.add(sp);
this.setVisible(true);
this.setBounds(100,100,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();
}
}