「java按钮退出」java按钮关闭当前窗口

博主:adminadmin 2022-12-06 14:30:08 57

今天给各位分享java按钮退出的知识,其中也会对java按钮关闭当前窗口进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java做一个窗口怎么设置一个退出按钮

如果是点击上面的那个叉号退出的话就加上这样一句setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

如果是通过按钮退出就用监听器实现如:

class MyListener2 implements ActionListener {

public void actionPerformed(ActionEvent e) {

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 怎样实现点击按钮,关闭程序?

给按钮添加 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中通过按钮退出用户界面

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事件处理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);

}

}

}

java按钮退出的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java按钮关闭当前窗口、java按钮退出的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-06,除非注明,否则均为首码项目网原创文章,转载请注明出处。