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);
}