java中 点击一个按钮,让这个按钮所在的框关掉,怎么实现
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class practice1 extends JFrame
{
private JTextField txt1;
private JTextField txt2;
private JLabel But1;
private JLabel But2;
private JButton but1,but2;
public practice1 jav;
public practice1()
{
super("测试");
Container c=getContentPane();
But1 = new JLabel("登陆");
But2 = new JLabel("注册");
but1 = new JButton();
but2 = new JButton();
but1.addActionListener(new Handler1());
but2.addActionListener(new Handler1());
c.setLayout(new FlowLayout());
txt1 = new JTextField(10);
txt2 = new JTextField(10);
but1.add(But1);
but2.add(But2);
c.add(txt1);
c.add(txt2);
c.add(but1);
c.add(but2);
setSize(300,300);
setVisible(true);
}
public void shutdown( )
{
this.dispose() ;
}
public static void main(String args[])
{
practice1 jav = new practice1();
}
class Handler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
jav.shutdown();
}
else
{
jav.shutdown();
}
}
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class practice1 extends JFrame
{
private JTextField txt1;
private JTextField txt2;
private JLabel But1;
private JLabel But2;
private JButton but1,but2;
public practice1 jav;
public practice1()
{
super("测试");
Container c=getContentPane();
But1 = new JLabel("登陆");
But2 = new JLabel("注册");
but1 = new JButton();
but2 = new JButton();
but1.addActionListener(new Handler1());
but2.addActionListener(new Handler1());
c.setLayout(new FlowLayout());
txt1 = new JTextField(10);
txt2 = new JTextField(10);
but1.add(But1);
but2.add(But2);
c.add(txt1);
c.add(txt2);
c.add(but1);
c.add(but2);
setSize(300,300);
setVisible(true);
}
public void shutdown( )
{
this.dispose() ;
}
public static void main(String args[])
{
practice1 jav = new practice1();
}
class Handler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
jav.shutdown();
}
else
{
jav.shutdown();
}
}
}
} 展开
3个回答
展开全部
package zhidao;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class practice1 extends JFrame implements ActionListener
{
private JTextField txt1;
private JTextField txt2;
private JLabel But1;
private JLabel But2;
private JButton but1, but2;
public practice1 jav;
public practice1 ()
{
super ("测试");
Container c = getContentPane ();
But1 = new JLabel ("登陆");
But2 = new JLabel ("注册");
but1 = new JButton ();
but2 = new JButton ();
but1.addActionListener (this);
but2.addActionListener (this);
c.setLayout (new FlowLayout ());
txt1 = new JTextField (10);
txt2 = new JTextField (10);
but1.add (But1);
but2.add (But2);
c.add (txt1);
c.add (txt2);
c.add (but1);
c.add (but2);
setSize (300, 300);
setVisible (true);
this.addWindowListener (new Handler1());
}
public void shutdown ()
{
this.dispose ();
}
public static void main ( String args[] )
{
practice1 jav = new practice1 ();
}
class Handler1 extends WindowAdapter
{
@Override
public void windowClosing ( WindowEvent e )
{
System.exit (0);
}
}
@Override
public void actionPerformed ( ActionEvent e )
{
dispose ();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dispose()
public class Test extends JFrame{
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(400, 600);
JButton button = new JButton("关闭");
button.setSize(60, 25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
frame.add(button);
frame.show();
}
}
public class Test extends JFrame{
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(400, 600);
JButton button = new JButton("关闭");
button.setSize(60, 25);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
}
});
frame.add(button);
frame.show();
}
}
追问
关不掉的~~!
追答
public class Test extends JFrame {
private JTextField txt1;
private JTextField txt2;
private JLabel But1;
private JLabel But2;
private JButton but1, but2;
public static Test jav; //更改
public Test() {
super("测试");
Container c = getContentPane();
But1 = new JLabel("登陆");
But2 = new JLabel("注册");
but1 = new JButton();
but2 = new JButton();
but1.addActionListener(new Handler1());
but2.addActionListener(new Handler1());
c.setLayout(new FlowLayout());
txt1 = new JTextField(10);
txt2 = new JTextField(10);
but1.add(But1);
but2.add(But2);
c.add(txt1);
c.add(txt2);
c.add(but1);
c.add(but2);
setSize(300, 300);
setVisible(true);
}
public void shutdown() {
this.dispose();
}
public static void main(String args[]) {
jav = new Test(); //更改 }
class Handler1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but1) {
jav.shutdown();
} else {
jav.shutdown();
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.setVisible(false)
追问
也试过,关不掉~~!
追答
你的jav初始化了吗?我觉得是个null,我帮你改了一下,你看可以了吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class practice1 extends JFrame
{
private JTextField txt1;
private JTextField txt2;
private JLabel But1;
private JLabel But2;
private JButton but1,but2;
public practice1()
{
super("测试");
Container c=getContentPane();
But1 = new JLabel("登陆");
But2 = new JLabel("注册");
but1 = new JButton();
but2 = new JButton();
but1.addActionListener(new Handler1());
but2.addActionListener(new Handler1());
c.setLayout(new FlowLayout());
txt1 = new JTextField(10);
txt2 = new JTextField(10);
but1.add(But1);
but2.add(But2);
c.add(txt1);
c.add(txt2);
c.add(but1);
c.add(but2);
setSize(300,300);
setVisible(true);
}
public void shutdown( )
{
this.setVisible(false); ;
}
public static void main(String args[])
{
practice1 jav = new practice1();
}
class Handler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but1)
{
practice1.this.shutdown();
}
else
{
practice1.this.shutdown();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询