
正文
java下一关按钮代码,java里面如何使用按钮开始程序
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA如何用按钮关闭窗体
很久没有用过界面编程了,就当复习一下了,哈哈
如一楼所说的,给按钮加一个监听器ActionListener,写一个实现方法
actionPerformed.此时当按钮点击时会调用actionPerformed方法,代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Close extends JFrame implements ActionListener{
JButton close;
public Close(){
close = new JButton("close");//增加一个按钮
add(close);
close.addActionListener(this);//给按钮增加一个监听器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按钮点击时的事件处理方法
//按钮点击时一定会自动执行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//关闭整个应用程序.如果只是是想关闭当前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
相关问答
Q1: java设置关闭窗口按钮
package org.t20110219.test;
/**
* 修改后的代码
* 能关闭,
* 希望能帮助你
*/
import java.awt.*;
import java.awt.event.*;
public class MainProgram extends Frame implements ActionListener,WindowListener
{
int i;
Button b1 = new Button("确定");
Button b2 = new Button("关闭");
void FlowLayoutEX()
{
this.setTitle("布局管理器");
this.setLayout(new FlowLayout());
this.add(b1);
b1.addActionListener(this);
this.add(b2);
b2.addActionListener(this);
this.addWindowListener(this);
this.setBounds(100, 100, 250, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
/**------------修改代码--------------*/
/**
* 修改 合理就可以了
* e.getSource() 可以得到 你点击了哪个 按钮
* 判断之后就可以 设定 对应的 操作了
*/
if(e.getSource()==b2){
System.exit(0);
}else{
i++;
Button bi = new Button("按钮"+i);
this.add(bi);
this.show(true);
}
}
public void windowClosing(WindowEvent f)
{
System.exit(0);
}
public void windowOpened(WindowEvent f){}
public void windowClosed(WindowEvent f){}
public void windowIconified(WindowEvent f){}
public void windowDeiconified(WindowEvent f){}
public void windowActivated(WindowEvent f){}
public void windowDeactivated(WindowEvent f){}
public static void main(String args[])
{
MainProgram window = new MainProgram();
window.FlowLayoutEX();
}
}
Q2: 在java swing中如何实现通过代码按下一个按钮?
给事件队列发一个点击事件即可,按钮(JButton)本身就有一个doClick方法,或者Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent一个MouseEvent即可



![[转] android自动化之MonkeyRunner测试环境配置(一) [转] android自动化之MonkeyRunner测试环境配置(一)](https://www.04ip.com/template/qe/style/noimg/8.jpg)


