import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
class AFramee extends JFrame{
public AFramee(String title)
{
super (title);
JButton button1,button2,button3,button4;
JPanel panel1,panel2;
button1=new JButton("查询房屋信息");
button2=new JButton("租房");
button3=new JButton("退房");
button4=new JButton("退出");
panel1=new JPanel();
panel2=new JPanel();
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
panel2.add(button4);
panel2.setLayout(new FlowLayout());
panel1.setBackground(Color.CYAN);
Container cp=getContentPane();
cp.add(panel1,BorderLayout.CENTER);
cp.add(panel2,BorderLayout.SOUTH);
AL listener=new AL();
button4.addActionListener(listener);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
}
}
class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
if((JButton)e.getSource()==button4)
JFrame.EXIT_ON_CLOSE;
}
}
public class AFrame{
public static void main(String args[]){
new AFramee("123");
}
}
这代码我想实现按button4时 窗口关闭 但是按了没反应 求各位指教指教
private JButton getExitBtn() {
if (exitBtn == null) {
exitBtn = new JButton("退 出");
exitBtn.setBounds(new Rectangle(331, 229, 104, 23));
exitBtn.setName("退出按钮");
exitBtn.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.exit(0);
}
});
}
return exitBtn;
}
JFrame.EXIT_ON_CLOSE更本不是退出语句,只是一个常量
怎么可能退出与关闭UI,帮你改了actionPerformed的方法
更多发现看我的博客
Java codepublic void actionPerformed(ActionEvent e){
if((JButton)e.getSource()==button4) {
this.setVisible(true);
this.dispose();
}