简体   繁体   English

等到按下按钮

[英]Waiting till button is pressed

There are a lot questions on this, but they weren't able to help me or I didn't understand it.. 对此有很多疑问,但是他们无法帮助我或我听不懂。

Basically I want user to press the button before system goes back to main method. 基本上,我希望用户在系统返回主要方法之前先按下按钮。 In this case if system goes back to main method, system will quit. 在这种情况下,如果系统返回主方法,系统将退出。

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

}
}

Sorry if it was just me lacking the understanding and thank you for help. 对不起,如果只是我缺乏理解,谢谢您的帮助。

EDIT: Maybe I explained this incorrectly or displayed it incorrectly. 编辑:也许我解释不正确或显示不正确。 Lets say if the system goes back to main system it will do something I don't want, therefor I want the user to press the button to go back to the main system or do "the thing". 可以说,如果系统返回到主系统,它将执行我不想执行的操作,因此,我希望用户按下按钮以返回到主系统或执行“操作”。 Sorry for bad explanation, including this one. 对不起,不好的解释,包括这个。

This class is separate from my work and I just used it to test stuff... In my project, user can choose from several buttons (lets say main method is the menu in this case). 该类与我的工作分开,我只是用它来测试东西。在我的项目中,用户可以从几个按钮中进行选择(在这种情况下,主要方法是菜单)。 The user presses a button goes to a new window/frame, if the program doesn't pause or waits for button to be pressed, it will go back to main method. 用户按下按钮将转到新的窗口/框架,如果程序不暂停或等待按钮被按下,它将返回到主方法。

The quick answer is what Andrew Thompson wrote in his comment: 快速的答案是安德鲁·汤普森(Andrew Thompson)在评论中写道:

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

}

but Java class name should start with upper case test123 -> Test123 (but you can find a lot better name for sure). 但是Java类名称应以大写的test123-> Test123开头(但是您肯定会找到更好的名称)。

Why not to extend JFrame also? 为什么不还要扩展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();
    }

}

Read more in Concurrency in Swing tutorial to find out how to deal with long running tasks out of dispatch thread... 在Swing并发教程中阅读更多内容,以了解如何在调度线程之外处理长时间运行的任务...


From your question it seems, that you do not know how swing application behaves. 从您的问题看来,您不知道swing应用程序的行为。 You create a GUI and the you are waiting for user's input. 您创建一个GUI,并且正在等待用户输入。 So basically you do not care what was your program executing, when user pressed the button... (and therefore it doesn't matter where it returns to) 因此,基本上,您无需关心程序在用户按下按钮时执行的操作...(因此,返回到什么位置都没有关系)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM