java中GUI相关知识点

GUI:swing、awt

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

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

java中GUI相关知识点

BorderLayout()流式布局

GridLayout()表格布局

frame.pack():自适应

java中GUI相关知识点
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));
}
}

上一篇:vector push_back普通指针的感想


下一篇:机器学习算法-随机森林(Random Forest)