2014-09-05 125 views
0

上有這個有很多問題,但他們沒能幫助我,否則我不明白它..等到按鈕被按下

基本上我想用戶按下按鈕,系統返回前以主要方法。在這種情況下,如果系統返回主方法,系統將退出。

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class test123 implements ActionListener{ 


JTable table; 
JButton button; 
JFrame frame; 

public test123(){ 

    frame = new JFrame(); 
    frame.setLayout(null); 

    button = new JButton("Finish"); 
    button.setBounds(200, 10, 70, 40); 
    button.addActionListener(this); 

    frame.add(button); 


    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
    frame.setSize(600, 200); 
    frame.setVisible(true); 
    frame.setTitle("TEst123"); 
} 


public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == button){ 
     System.out.println("message...."); 
    } 
} 

public static void main(String arg[]){ 
    test123 gui = new test123(); 

    System.exit(0); 

} 
} 

對不起,如果只是我缺乏理解,並感謝您的幫助。

編輯: 也許我錯誤地解釋了這或者顯示不正確的。比方說,如果系統回到主系統,它會做我不想要的東西,因此我希望用戶按下按鈕返回到主系統或做「事情」。對不起,不好的解釋,包括這一個。

這個類是從我的工作和我分開只是用它來測試的東西...在我的項目,用戶可以從幾個按鈕選擇(可以說主要方法是在這種情況下,菜單)。用戶按下一個按鈕會進入一個新的窗口/框架,如果程序沒有暫停或等待按鈕被按下,它將回到主方法。

+0

因此,你希望程序在按下按鈕時關閉? – ortis 2014-09-05 16:47:05

+1

*「但他們無法幫助我,或者我不理解它。」*那麼,有什麼可以相信我們可以用你能理解的方式解釋它?也許從一開始 - 有什麼問題,以及這些答案如何不能幫助你 - 或者你對這些答案有什麼不瞭解? – 2014-09-05 16:48:14

+2

將'System.exit(0);'移到'actionPerformed(ActionEvent)'方法中。順便說一句 - 你用什麼資源來學習Swing?它似乎給你許多壞習慣(AKA - '代碼是廢話')。 – 2014-09-05 16:50:24

回答

1

簡單的回答就是安德魯·湯普森在他的評論中寫道:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTable; 

public class test123 implements ActionListener { 

    JTable table; 
    JButton button; 
    JFrame frame; 

    public test123() { 

     frame = new JFrame(); 
     frame.setLayout(null); 

     button = new JButton("Finish"); 
     button.setBounds(200, 10, 70, 40); 
     button.addActionListener(this); 

     frame.add(button); 

     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
     frame.setSize(600, 200); 
     frame.setVisible(true); 
     frame.setTitle("TEst123"); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == button) { 
      System.out.println("message...."); 
//   System.exit(0); 
      frame.dispose(); // better than exit 
     } 
    } 

    public static void main(String arg[]) { 
     test123 gui = new test123(); 
    } 

} 

但Java類名稱應以大寫字母test123開始 - > Test123(但你可以找到肯定有很多更好的名字)。

爲什麼不擴展JFrame呢?

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class Test123 extends JFrame implements ActionListener { 

    private static final long serialVersionUID = 3378774311250822914L; 

// private JTable table; 
    private JButton button; 
// JFrame frame; 

    public Test123() { 

//  frame = new JFrame(); 
     this.setLayout(null); 

     button = new JButton("Finish"); 
     button.setBounds(200, 10, 70, 40); 
     button.addActionListener(this); 

     this.add(button); 

     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setSize(600, 200); 
     this.setTitle("Test123"); 
     this.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == button) { 
      System.out.println("message...."); 
//   System.exit(0); 
      this.dispose(); 
     } 
    } 

    public static void main(String arg[]) { 
     Test123 gui = new Test123(); 
    } 

} 

詳情請閱讀Concurrency in Swing tutorial找出如何處理長時間運行的任務進行調度線程的...


從你的問題看來,你不知道Swing應用程序如何運行。您創建一個GUI並且您正在等待用戶的輸入。因此,基本上你不在意你的程序執行的是什麼,當用戶按下按鈕時... (因此它返回的位置並不重要)

+1

將System.exit(0)'更改爲'dispose()'的良好調用爲以及許多其他技巧。代碼中仍有一些問題值得關注,但它們超出了問題的範圍。 – 2014-09-05 17:30:11

+0

@AndrewThompson隨意分享,在更大的框架中,我會使用佈局(但在這個簡單的例子中不需要),並且設置大小不是我更喜歡的(但是這與佈局的東西也有關)。當然,更大範圍的面板用法更好,字符串應該在生產代碼中進行本地化,但是對於這個例子來說,它太多了...... – Betlista 2014-09-05 17:38:59

+1

我同意。我認爲你已經覆蓋了比OP更多的內容,額外提示僅僅是「蛋糕上的精華」。我所指的'其他問題'幾乎是可怕的缺乏佈局管理器的使用,但是如果OP(在一個專門的問題上)詢問,我只會進入這一點。僅僅回答這個問題太多了。 – 2014-09-05 17:42:51