1. 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();
}
}
2. java做一个窗口怎么设置一个退出按钮
1、打开eclipse,并且建立java一个工程,具体如下代码:
addActionListene
(new ActionListene
()
{
pu
lic void actionPe
fo
med(ActionEvent e)
{
dispose();
}
});
2、执行该程序查看结果,如图所示。
3. java做一个窗口怎么设置一个退出按钮
如果是点击上面的那个叉号退出的话就加上这样一句setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如果是通过按钮退出就用监听器实现如:
class MyListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
一般情况下这两种都有。
4. java 作出窗口,点击关闭按钮时可以退出当前程序。
button.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
setVisible(false);//隐藏窗体
System.exit(0);//退出程序
}
});
5. 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);
}
}
6. 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();
}
}
7. 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("退出");
8. java中有一个按钮“退出”,如何编写代码,使当点击这个按钮时退出程序
if(JOptionPane.showConfirmDialog(this,"您确定要退出吗?","提示",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
{
System.exit(0);
}