我的java怎样给按钮添加一个当按钮被点击时窗口关闭?请举个例子。swing界面编程的
展开全部
系统不关闭,只是隐藏窗体!在按钮的监听事件中获取到窗体,然后设置窗体的visible属性为false既可以
例如:
public class window {
public static void main(String[] arg0){
myWindow mw=new myWindow();
mw.setVisible(true);
}
}
class myWindow extends JFrame{
private JButton jb=null;
public myWindow() {
jb=new JButton("test");
jb.addActionListener(new mylistener(this));
this.add(jb);
}
}
class mylistener implements ActionListener{
private myWindow mw=null;
public mylistener(myWindow mw){
this.mw=mw;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
mw.setVisible(false);
}
}
例如:
public class window {
public static void main(String[] arg0){
myWindow mw=new myWindow();
mw.setVisible(true);
}
}
class myWindow extends JFrame{
private JButton jb=null;
public myWindow() {
jb=new JButton("test");
jb.addActionListener(new mylistener(this));
this.add(jb);
}
}
class mylistener implements ActionListener{
private myWindow mw=null;
public mylistener(myWindow mw){
this.mw=mw;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
mw.setVisible(false);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你一个我们当时的作业:
//源文件名:album.java
//在下载源程序中的文件夹:0709相册
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class album extends Applet implements ActionListener
{
Image img[];
int j=0;
String f;
Label a1=new Label("文件名:");
Label a2=new Label(" ");
Button btn1,btn2;
public void init()
{
img = new Image[22];
setLayout(null);
setBackground(Color.cyan);
add(a1);
add(a2);
btn1= new Button("下一页");
btn2= new Button("上一页");
add(btn1);
add(btn2);
a1.setBounds(650,60,60,30);
a2.setBounds(650,80,110,60);
a2.setBounds(650,80,110,60);
a1.setBackground(Color.cyan);
a2.setBackground(Color.cyan);
a2.setForeground(Color.red);
Font ft = new Font("Times New Romon",1,20);
a2.setFont(ft);
btn1.setBounds(650,180,60,30);
btn2.setBounds(650,240,60,30);
btn1.addActionListener(this);
btn2.addActionListener(this);
for (int i=0;i<22;i++)
{
f="pic"+Integer.toString(i)+".jpg";
img[i]=getImage(getCodeBase(),f);
}
}
public void paint (Graphics g)
{
f="pic"+Integer.toString(j)+".jpg";
a2.setText(f);
int w=img[j].getWidth(this);
int h=img[j].getHeight(this);
g.drawImage(img[j],0,0,w,h,this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
j++;
if (j>21) j=0;
}
if(e.getSource()==btn2)
{
j--;
if(j<0) j=21;
}
repaint();
}
}
//源文件名:album.java
//在下载源程序中的文件夹:0709相册
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class album extends Applet implements ActionListener
{
Image img[];
int j=0;
String f;
Label a1=new Label("文件名:");
Label a2=new Label(" ");
Button btn1,btn2;
public void init()
{
img = new Image[22];
setLayout(null);
setBackground(Color.cyan);
add(a1);
add(a2);
btn1= new Button("下一页");
btn2= new Button("上一页");
add(btn1);
add(btn2);
a1.setBounds(650,60,60,30);
a2.setBounds(650,80,110,60);
a2.setBounds(650,80,110,60);
a1.setBackground(Color.cyan);
a2.setBackground(Color.cyan);
a2.setForeground(Color.red);
Font ft = new Font("Times New Romon",1,20);
a2.setFont(ft);
btn1.setBounds(650,180,60,30);
btn2.setBounds(650,240,60,30);
btn1.addActionListener(this);
btn2.addActionListener(this);
for (int i=0;i<22;i++)
{
f="pic"+Integer.toString(i)+".jpg";
img[i]=getImage(getCodeBase(),f);
}
}
public void paint (Graphics g)
{
f="pic"+Integer.toString(j)+".jpg";
a2.setText(f);
int w=img[j].getWidth(this);
int h=img[j].getHeight(this);
g.drawImage(img[j],0,0,w,h,this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
{
j++;
if (j>21) j=0;
}
if(e.getSource()==btn2)
{
j--;
if(j<0) j=21;
}
repaint();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.addActionListener(new actionListener()
public void actionPerformed(ActionEvent e) {
System.exit(0);
});
public void actionPerformed(ActionEvent e) {
System.exit(0);
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JButton b = new JButton("退出");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
试试可以不?
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
试试可以不?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |