2014-09-05 128 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