GUI编程
作者:互联网
GUI编程
1.简介
Gui的核心技术:Swing AWT
//Fram对象
Frame frame = new Frame("我的第一个java图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(52, 73, 179));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
2.面板Panel
package com.lm.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成一个空间。但是不可以单独存在。
public class TestPanel {
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(new Color(118, 186, 17));
//设置坐标相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(24, 57, 156));
//
frame.add(panel);
frame.setVisible(true);
//监听
frame.addWindowListener(new WindowAdapter() {
//窗口关闭需要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
3.流式布局
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("Sout");
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.setSize(200,200);
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.setVisible(true);
frame.pack();
}
4.事件监听
public class TestActionTwo {
public static void main(String[] args) {
//两个按钮实现同一个监听
Frame frame = new Frame("开始-停止");
Button button1 = new Button("开始");
Button button2 = new Button("停止");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮点击了=>"+e.getActionCommand());
}
5.输入框TextFiled 监听
public class TestText01 {
public static void main(String[] args) {
//main只管启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听文本输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下回车输入
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField) e.getSource();//获得一些资源
System.out.println(field.getText());//获得输入框的文本
field.setText("");
6.画笔
class MyPaint extends Frame{
public void loadFrame(){
setVisible(true);
setBounds(200,200,400,400);
}
@Override
public void paint(Graphics g){
//画笔的颜色
g.setColor(new Color(145, 198, 13));
g.drawOval(100,100,100,100);
g.fillOval(200,200,200,200);
g.setColor(Color.red);
g.fillRect(150,150,200,200);
7.鼠标监听
目的:实现鼠标画画
8.窗口监听
9.键盘监听
标签:Frame,Button,编程,public,add,frame,new,GUI 来源: https://www.cnblogs.com/lyxyyds/p/16438665.html