㈠ java做一个窗口怎么设置一个退出按钮
如果是点击上面的那个叉号退出的话就加上这样一句setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如果是通过按钮退出就用监听器实现如:
class MyListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
一般情况下这两种都有。
㈡ JAVA 的GUI 如何实现按钮退出程序
用JFrame写的java小应用是直接带有窗口的,在main()中加上setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
如果想写点击事件来实现关闭窗口,试试 System.exit(0);
㈢ java 用按钮关闭窗口
这个不对,这个是设置标题栏上的按钮退出方式的
你可以按钮上添加事件把窗口2设置为隐藏。
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jFrame1.setVisible(false);
}
});
另外 按钮退出的命令是在按钮的事件中写
System.exit(0);
㈣ java中添加退出按钮
你都会编这么多的代码了,怎么就剩下这两步不会?
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Round extends Frame implements ActionListener {
TextField t1, t2, t3, t4;
Button b1;
Button btnExit;
public Round() {
setLayout(new FlowLayout());
t1 = new TextField(20);
t1.setBackground(Color.orange);
t2 = new TextField(20);
t2.setBackground(Color.orange);
t3 = new TextField(20);
t3.setBackground(Color.orange);
t4 = new TextField(20);
t4.setBackground(Color.orange);
b1 = new Button("计算");
btnExit = new Button("退出");
add(new Label("输入圆的半径:"));
add(t1);
add(new Label("得出圆的直径:"));
add(t2);
add(new Label("得出圆的面积:"));
add(t3);
add(new Label("得出圆的周长:"));
add(t4);
add(b1);
add(btnExit);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b1.addActionListener(this);
btnExit.addActionListener(this);
setVisible(true);
setBounds(200, 200, 200, 300);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
double temp, r, a, c;
temp = Float.parseFloat(t1.getText());
r = 2 * temp;
a = 3.14 * temp * temp;
c = 2 * 3.14 * temp;
t2.setText(String.valueOf(r));
t3.setText(String.valueOf(a));
t4.setText(String.valueOf(c));
}
if(e.getSource()==btnExit){
System.exit(0);
}
}
public static void main(String args[]) {
new Round();
}
}
㈤ JAVA怎么实现点击按钮关闭窗口
你可以按钮上添加事件把窗口2设置为隐藏。
jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jFrame1.setVisible(false);
}
});
另外 按钮退出的命令是在按钮的事件中写
System.exit(0);
㈥ JAVA 菜单栏 下面的退出按钮怎么实行
以下代码已经运行过。你可以参照以下代码修改你自己的代码。
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JMenuBarmenuBar=newJMenuBar();
frame.setJMenuBar(menuBar);
JMenufileMenu=newJMenu("文件");
menuBar.add(fileMenu);
JMenuItemexitItem=newJMenuItem("退出");
fileMenu.add(exitItem);
exitItem.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
frame.dispose();
System.exit(0);
}
});
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SwingUtilities.invokeLater(()->frame.setVisible(true));
}
㈦ java中通过按钮退出用户界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class login extends JFrame implements ActionListener,ItemListener
{
JButton buttonone,buttontwo;
login(String s)
{
super(s);
//欢迎界面
JLabel labelone=new JLabel(new ImageIcon("login.jpg")),
labeltwo=new JLabel("UID"),
labelthree=new JLabel("Password");
JPanel panelone=new JPanel(),
paneltwo=new JPanel();
//panelone中为登陆信息
panelone.add(labeltwo);
JTextField textone=new JTextField(16);
//textone.addActionListener(this);
panelone.add(textone);
panelone.add(labelthree);
JPasswordField passone=new JPasswordField(16);
//passone.addActionListener(this);
panelone.add(passone);
//paneltwo中为权限选择
JRadioButton radioone=new JRadioButton("学生"),
radiotwo=new JRadioButton("教师"),
radiothree=new JRadioButton("学院");
ButtonGroup group=new ButtonGroup();
radioone.setSelected(true);//默认该单选按钮被选中
/*radioone.addItemListener(this);
radiotwo.addItemListener(this);
radiothree.addItemListener(this);*/
group.add(radioone);
group.add(radiotwo);
group.add(radiothree);
buttonone=new JButton("登陆");
buttontwo=new JButton("退出");
buttonone.addActionListener(this);
buttontwo.addActionListener(this);
paneltwo.add(radioone);
paneltwo.add(radiotwo);
paneltwo.add(radiothree);
paneltwo.add(buttonone);
paneltwo.add(buttontwo);
Container con=getContentPane();
con.add(labelone,BorderLayout.NORTH);
con.add(panelone,BorderLayout.CENTER);
con.add(paneltwo,BorderLayout.SOUTH);
validate();
setVisible(true);
setSize(500,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttontwo)
{
System.exit(0);//?????????问题出现的地方.类似的题目做过许多都没问题,但是本次实验就是无法退出!!望各位指点!!
System.out.println("sssss");
}
}
public void itemStateChanged(ItemEvent e2)
{
}
}
public class Window
{
public static void main(String args[])
{
new login("login");
}
}
你前面申明了Button buttonone,buttontwo; 改为JButton buttonone,buttontwo;
JButton buttonone=new JButton("登陆"),
buttontwo=new JButton("退出");
改为
buttonone=new JButton("登陆");
buttontwo=new JButton("退出");
㈧ java 怎样实现点击按钮,关闭程序
给按钮添加 ActionPerform 事件 内容写System.exit(0);
package com.lx;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test implements ActionListener {
Frame f = new Frame();
public static void main(String[] args) {
Test t = new Test();
t.init();
}
private void init() {
Button b = new Button("exit");
b.addActionListener(this);
f.add(b);
f.setLayout(new FlowLayout());
f.setSize(100,100);
f.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
f.setVisible(false);
f.dispose();
System.exit(0);
}
}
㈨ 用java编写的自动按键如何退出
问题在你的keypress处理中
while (true) {
// System.out.println(1);
if (e.getKeyCode() == KeyEvent.VK_F2) {
jfFrame.dispose();
}
if (e.getKeyCode() == KeyEvent.VK_F1)
try {
flash();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
当按下F1时,while循环将一直执行if(....= KeyEvent.VK_F1)内的代码,此时查看系统CPU一定会发现占用较高。
同时,新的按键F2事件无法得到调用,因为CPU在忙着执行while的代码。
改动的办法很简单,F5的触发就相当于是一个自动机,F1是触发F5的自动运行,F2是终止运行
只需要将F5触发的事件监听跟F2,F1都绑定到keypress函数中去,然后当检查到按键是F5时,则重新创建robot对象,延迟5秒触发keypress。
改动代码如下:
public void keyPressed(KeyEvent e) {
//System.out.println(e.getKeyCode());
//while (true) { //删除
// System.out.println(1);
//追加F5的处理
if(e.getKeyCode() == KeyEvent.VK_F5){
//重新创建robot执行计划
flash();
return; //结束事件处理,优化性能
}
if (e.getKeyCode() == KeyEvent.VK_F2) {
jfFrame.dispose();
return; //阻止代码继续执行
}
if (e.getKeyCode() == KeyEvent.VK_F1)
try {
flash();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//} //删除end while
}
㈩ java事件处理button如何单击按钮退出
如果是要实现单击按钮退出,建议删掉这行代码:
if(e.getActionCommand().equals("Eixt"))
或者,将实现接口的两个类变为public的内部类,同时稍微修改下,如下
import java.awt.*;
import java.awt.event.*;
public class lesson1 {
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame() {
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b, BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400, 300);
}
public static void main(String args[]) {
lesson1 aa = new lesson1();
aa.launchFrame();
}
class ButtonHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == b) {
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}