java中添加退出按钮

2024-12-12 17:52:35
推荐回答(1个)
回答1:

你都会编这么多的代码了,怎么就剩下这两步不会?
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Round extends Frame implements ActionListener {

TextField t1, t2, t3, t4;
Button b1;
Button btnExit;

public Round() {
setLayout(new FlowLayout());
t1 = new TextField(20);
t1.setBackground(Color.orange);

t2 = new TextField(20);
t2.setBackground(Color.orange);

t3 = new TextField(20);
t3.setBackground(Color.orange);

t4 = new TextField(20);
t4.setBackground(Color.orange);

b1 = new Button("计算");
btnExit = new Button("退出");

add(new Label("输入圆的半径:"));
add(t1);

add(new Label("得出圆的直径:"));
add(t2);

add(new Label("得出圆的面积:"));
add(t3);

add(new Label("得出圆的周长:"));
add(t4);

add(b1);
add(btnExit);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

b1.addActionListener(this);
btnExit.addActionListener(this);

setVisible(true);
setBounds(200, 200, 200, 300);
validate();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
double temp, r, a, c;
temp = Float.parseFloat(t1.getText());
r = 2 * temp;
a = 3.14 * temp * temp;
c = 2 * 3.14 * temp;
t2.setText(String.valueOf(r));
t3.setText(String.valueOf(a));
t4.setText(String.valueOf(c));
}
if(e.getSource()==btnExit){
System.exit(0);
}

}

public static void main(String args[]) {
new Round();
}
}