等到按钮pressed [英] Waiting till button is pressed

查看:220
本文介绍了等到按钮pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有对这个有很多问题,但他们没有能够帮助我,否则我不明白它..

There are a lot questions on this, but they weren't able to help me or I didn't understand it..

基本上我想用户preSS按钮系统返回到主方法之前。如果在系统中可以追溯到主要方法这种情况下,系统会退出。

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.

编辑:
也许我错误地解释了这或显示不正确的。如果系统返回到主系统会做一些我不愿意,我为此希望用户preSS的按钮,返回到主系统或做的事让说。对不起,我不好解释,包括这一个。

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.

这个类是我的工作分开,我只是用它来测试的东西...在我的项目,用户可以从几个按钮选择(可以说主要方法是在这种情况下,菜单)。用户presses按钮进入一个新的窗口/帧,如果该程序不会暂停或按钮等待是pressed,它会返回主方法。

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:

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(但你可以找到肯定有不少好名字)

but Java class name should start with upper case test123 -> Test123 (but you can find a lot better name for sure).

为什么不还扩展JFrame的?

Why not to extend JFrame also?

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

}

在Swing指南更多的并发找出如何应对长时间运行的任务,出来分派线程...

Read more in Concurrency in Swing tutorial to find out how to deal with long running tasks out of dispatch thread...

从你的问题看来,你不知道摆应用程序的行为。您创建一个GUI和你在等待用户的输入。所以基本上你不关心什么是你的程序执行,当用户pressed按钮...
(因此它并不重要,其中返回到)

From your question it seems, that you do not know how swing application behaves. You create a GUI and the you are waiting for user's input. 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)

这篇关于等到按钮pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆