处理另一个类中的JButton点击事件 [英] Handle JButton click event in another class

查看:104
本文介绍了处理另一个类中的JButton点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从C#开始使用java,所以我不熟悉java的最佳做法。



我有一个主类打开一个JFrame来获取几个从用户输入字符串。当用户点击提交时,GUI应该关闭,主类继续使用输入进行处理。


这是主要的类:

  public class Main {
FInput fInput;

public void main(String [] args){
if(args.length == 0)
{
fInput = new FInput();
fInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fInput.pack();
fInput.setVisible(true);
}
else
startProcess(args);
}

public void startProcess(String [] args){
// Do stuff
}

主类将使用此框架从用户获取输入:

  public class FInput extends JFrame {
private JTextField txtSourceDirectory;
private JTextField txtTargetDirectory;
private JTextField txtDefectNumber;
private JTextField txtSliceTokens;
私人JButton btnStart;

public FInput(){
//初始化文本字段和按钮
JButton.addActionListener(something);
}
}

在我可以找到的所有例子中,成为一个FMain本身。但是在这种情况下,我想要主要听和使用方法startProcess中的输入。


将主要实现ActionListener,并将其传递给FMain构造函数是否要走?

解决方案

是的,这是正确的想法。你必须做两件事才能做到这一点:


  1. 把它放在code> FINput class:

      Main m = new Main(this); 


  2. 然后,将这些行放在 Main class ...

      FINput gui; 

    public Main(FInput in){gui = in;


现在您可以参考任何组件通过执行这样的操作,从 Main 类中的 FInput 类。

  gui.someComponent ... 

设置收听者只需写入 someComponent.addItemListener(m); 或类似的东西。



希望这有帮助! / p>




@Yoav针对您最新的评论...


您不必将该类与GUI类分开;你可以将这两个组合成一个类...




  import javax.swing。*; 
import java.awt。*;
import java.awt.event。*;

public class Main extends JFrame implements ActionListener {

private JTextField txtSourceDirectory;
private JTextField txtTargetDirectory;
private JTextField txtDefectNumber;
private JTextField txtSliceTokens;
私人JButton btnStart;

public Main(){
txtSourceDirectory = new JTextField(40); //将此更改为您需要的字符数量
txtTargetDirectory = new JTextField(40);
txtDefectNumber = new JTextField(40);
txtSliceTokens = new JTextField(40);
btnStart = new JButton(Start);
add(txtSourceDirectory);
add(txtTargetDirectory);
add(txtDefectNumber);
add(txtSliceTokens);
add(btnStart);
btnStart.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}

public void actionPerformed(ActionEvent event){
// do stuff
}

static void startProcess(String [] ARGS ){
// do stuff
}

public static void main(String [] args){
if(args.length == 0){
Main frame = new Main();
} else {
startProcess(args);
}
}
}


I'm new to java coming from C# so I'm not familiar with java best practices.

I have a main class that opens a JFrame to get several input strings from a user. When the user clicks submit the GUI should close and the main class continue processing using the input.

This is the main class:

public class Main {
    FInput fInput;

    public void main(String[] args) {
        if(args.length==0)
        {
            fInput = new FInput();
            fInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fInput.pack();
            fInput.setVisible(true);
        }
    else
        startProcess(args);
    }

    public void startProcess(String[] args) {
// Do stuff
}

The main class will use this frame to get input from the user:

public class FInput extends JFrame{
    private JTextField txtSourceDirectory;
    private JTextField txtTargetDirectory;
    private JTextField txtDefectNumber;
    private JTextField txtSliceTokens;
    private JButton btnStart;

    public FInput() {
        // Initialize text fields and button
        JButton.addActionListener(something);
    }
}

In all the examples I could find, the listener would be a FMain itself. However in this case I want Main to listen and use the input in method startProcess.

Would having Main implement ActionListener, and passing it to FMain constructor is the way to go?

解决方案

Yes, that is the right idea. You must do two things in order to be able to do that, though:

  1. Put this at the beginning of the FInput class:

    Main m = new Main(this);
    

  2. Then, put these lines somewhere in the Main class...

    FInput gui;
    
    public Main(FInput in) { gui = in; }
    

Now you can refer to any component in the FInput class from the Main class by doing something like this.

gui.someComponent ...

To set up listeners just write someComponent.addItemListener(m); or something of the sort.

Hope this helps!


@Yoav In response to your latest comment...

You don't have to separate the listening class from the GUI class; you can combine the two into one class...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener {

    private JTextField txtSourceDirectory;
    private JTextField txtTargetDirectory;
    private JTextField txtDefectNumber;
    private JTextField txtSliceTokens;
    private JButton btnStart;

    public Main() {
        txtSourceDirectory = new JTextField(40); //change this to the amount of characters you need
        txtTargetDirectory = new JTextField(40);
        txtDefectNumber = new JTextField(40);
        txtSliceTokens = new JTextField(40);
        btnStart = new JButton("Start");
        add(txtSourceDirectory);
        add(txtTargetDirectory);
        add(txtDefectNumber);
        add(txtSliceTokens);
        add(btnStart);
        btnStart.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {
        //do stuff
    }

    static void startProcess(String[] ARGS) {
        //do stuff
    }

    public static void main(String[] args) {
        if (args.length == 0) {
            Main frame = new Main();
        } else {
            startProcess(args);
        }
    }
}

这篇关于处理另一个类中的JButton点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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