在一个JFrame中放了一个JPanel,其中有一个按钮,想要单击他时关闭当前面板?
2011-03-22
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFrameTest {
public static void main(String[] args) {
final JFrame f=new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(300,200);
final JPanel p=new JPanel(new GridLayout(3,1,2,2));
final JButton a=new JButton("Close Frame");
final JButton b=new JButton("Hide JPanel");
final JButton c=new JButton("Remove JPanel");
p.add(a);
p.add(b);
p.add(c);
f.add(p);
f.setVisible(true);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==a){
f.dispose();
}
else if(e.getSource()==b){
p.setVisible(false);
}
else if(e.getSource()==c){
f.remove(p);
f.repaint();
}
}
};
a.addActionListener(al);
b.addActionListener(al);
c.addActionListener(al);
}
}
import java.awt.event.*;
import javax.swing.*;
public class JFrameTest {
public static void main(String[] args) {
final JFrame f=new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(300,200);
final JPanel p=new JPanel(new GridLayout(3,1,2,2));
final JButton a=new JButton("Close Frame");
final JButton b=new JButton("Hide JPanel");
final JButton c=new JButton("Remove JPanel");
p.add(a);
p.add(b);
p.add(c);
f.add(p);
f.setVisible(true);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==a){
f.dispose();
}
else if(e.getSource()==b){
p.setVisible(false);
}
else if(e.getSource()==c){
f.remove(p);
f.repaint();
}
}
};
a.addActionListener(al);
b.addActionListener(al);
c.addActionListener(al);
}
}
追问
dispose();是FRAME类的方法,看样子我需要在Frame中加一个子Frame再采用dispose();
了,谢谢了,
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询