编程语言
首页 > 编程语言> > java中GUI相关知识点

java中GUI相关知识点

作者:互联网

GUI:swing、awt

设置大小固定:frame.setResizable(false);

监听事件:添加监听事件可以关闭窗口

在这里插入图片描述

BorderLayout()流式布局

GridLayout()表格布局

frame.pack():自适应

在这里插入图片描述
flowlayout()是流布局管理器,居中对齐
borderlayout布局管理器,边框对齐
Gridlayout()表格的版面对象

计算机器

public class calculate {
public static void main(String[] args) {
new Calcualte();
}
}
class Calcualte extends Frame{
public Calcualte() {
TextField num1 = new TextField(10);//字符数
TextField num2 = new TextField(10);
TextField num3 = new TextField(30);
Button button = new Button("=");
Label label = new Label("+");
setLayout(new FlowLayout());
button.addActionListener(new ClaculateListener(num1,num2,num3));
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

}

}
class ClaculateListener implements ActionListener{
private TextField num1,num2,num3;
public ClaculateListener(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(String.valueOf(n1+n2));
}
}

标签:知识点,java,num1,num2,num3,GUI,new,TextField,public
来源: https://blog.csdn.net/m0_56242825/article/details/122266817