本文全文参考 https://www.bilibili.com/video/BV1DJ411B75F
/**
* GUI第一个界面
**/
public class TestFrame {
public static void main(String[] args) {
// Frame
Frame frame = new Frame("我的第一个Java图形界面窗口");
// 需要设置可见性,默认是隐藏的
frame.setVisible(true);
// 设置窗口大小
frame.setSize(400,400);
// 设置背景颜色
frame.setBackground(Color.lightGray);
// 弹出的初始位置
frame.setLocation(200,200);
// 设置窗口大小固定
frame.setResizable(false);
}
}
/**
* 展示多个Frame窗口
**/
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.lightGray);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.BLUE);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.CYAN);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.GREEN);
}
}
class MyFrame extends Frame{
/**
* 可能存在多个窗口,我们需要一个计数器
**/
static int id = 0;
public MyFrame(int x,int y, int w,int h,Color color) throws HeadlessException {
super("MyFrame:"+(++id));
setBounds(x,y,w,h);
setBackground(color);
setVisible(true);
setResizable(false);
}
}
// 监听事件,监听窗口关闭
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
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(Color.BLUE);
// 设置布局属性
// 坐标
panel.setBounds(50,50,100,100);
// panel.setBounds(50,160,100,100);
panel.setBackground(Color.RED);
// frame添加panel组件
frame.add(panel);
frame.setVisible(true);
// 监听事件,监听窗口关闭
// 适配器模式
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// 结束程序
System.exit(0);
}
});
}
}
/**
* 流式布局
**/
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
// 组件-按钮
Button btn1 = new Button("Button1");
Button btn2 = new Button("Button2");
Button btn3 = new Button("Button3");
// 设置为流式布局,默认是居中CENTER
// frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(300,300);
frame.setVisible(true);
// 添加按钮
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
// 窗口监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
/**
* 东西南北中
**/
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
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);
}
}
/**
* 表格布局
**/
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
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);
}
}
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame("TestActionEvent");
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(300,300);
frame.setVisible(true);
Button button = new Button();
button.setLabel("aaaaaa");
button.addActionListener(new MyActionListener());
windowClose(frame);
frame.add(button);
frame.pack();
}
/**
* 关闭窗口方法的封装
**/
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
/**
* 按钮事件监听
**/
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("hello");
}
}
public class jisuanqi {
public static void main(String[] args) {
new MyFrame2();
}
}
class MyFrame2 extends Frame{
public MyFrame2() throws HeadlessException {
TextField textField = new TextField();
add(textField);
MyAActionListener2 listener2 = new MyAActionListener2();
// 回车触发
textField.addActionListener(listener2);
textField.setEchoChar('*');
setSize(300,300);
setVisible(true);
}
}
class MyAActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource(); // 获得资源
String text = field.getText(); // 获得文本框内的文本内容
System.out.println(text);
field.setText("");
}
}
public class TestJiSuanQi {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame{
public Calculator() throws HeadlessException {
// 三个本文框
TextField field = new TextField(10);
TextField field2 = new TextField(10);
TextField field3 = new TextField(20);
// 一个按钮
Button sumBtn = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(field,field2,field3);
sumBtn.addActionListener(myCalculatorListener);
// 一个标签
Label label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(field);
add(label);
add(field2);
add(sumBtn);
add(field3);
pack();
setVisible(true);
windowClose(this);
}
// 关闭Frame监听事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener {
private TextField num1;
private TextField num2;
private TextField num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获得 加数 和 被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
// 将这两个值加法运算,放到第三个框
num3.setText(""+(n1+n2));
// 清除前两个框
num1.setText("");
num2.setText("");
}
}
public class TestJiSuanQi {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
// 属性
TextField num1,num2,num3;
Button sumBtn;
Label label;
// 方法
public void loadFrame(){
// 三个本文框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
// 一个按钮
sumBtn = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener(this);
sumBtn.addActionListener(myCalculatorListener);
// 一个标签
label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(sumBtn);
add(num3);
pack();
setVisible(true);
windowClose(this);
}
public Calculator() throws HeadlessException {
}
// 关闭Frame监听事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyCalculatorListener implements ActionListener {
Calculator calculator = null;
public MyCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
// 获得 加数 和 被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
// 将这两个值加法运算,放到第三个框
calculator.num3.setText(""+(n1+n2));
// 清除前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
public class TestJiSuanQi {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
class Calculator extends Frame{
// 属性
TextField num1,num2,num3;
Button sumBtn;
Label label;
public Calculator() throws HeadlessException {
}
// 方法
public void loadFrame(){
// 三个本文框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
// 一个按钮
sumBtn = new Button("=");
MyCalculatorListener myCalculatorListener = new MyCalculatorListener();
sumBtn.addActionListener(myCalculatorListener);
// 一个标签
label = new Label("+");
// 布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(sumBtn);
add(num3);
pack();
setVisible(true);
windowClose(this);
}
// 内部类,好处:可以直接访问外部类
private class MyCalculatorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获得 加数 和 被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
// 将这两个值加法运算,放到第三个框
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
} // 清除前两个框
}
// 关闭Frame监听事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}