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