Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

java - JFrame in separate class, what about the ActionListener?

I'm trying to separate my Swing GUI from my actual code. In short, I want the user to kick off a process (based on the user's selections); in this case, the JFrame will no longer be needed.

What I couldn't figure out is how to share the user's selection from the GUI.class with the Main.class.

Do you have any advice for me?

Here's my code:

public class Main {
  public static void main(String[] args) {
    // Show GUI
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        GUI gui = new GUI(templates);
        gui.setVisible(true);
      }
    });

    // Kick off a process based on the user's selection
  }
}

public class GUI extends JFrame {
  private static final long serialVersionUID = 1L;

  public GUI(Object[] objects) {
    setTitle("GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 350, 100);
    setLocationRelativeTo(null);

    JPanel cp = new JPanel();
    cp.setBorder(new EmptyBorder(10, 10, 10, 10));
    setContentPane(cp);

    JLabel lbl = new JLabel("Selection:");
    cp.add(lbl);

    final JComboBox<String> comboBox = new JComboBox<String>(new String[] { "One", "Two", "Three" });
    comboBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        setVisible(false);
        dispose();
        // Share the selected item with Main.class
      }
    });
    cp.add(comboBox);
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could create an object to store the selection result and pass it in to the constructor of the GUI class. Set the selection result in that object before closing the UI and then your Main class could access the value:

public class SelectionResult {
    private String selectionResult;

    public void setSelectionResult(final String selectionResult) {
        this.selectionResult = selectionResult;
    }

    public String getSelectionResult() {
        return this.selectionResult;
    }
}

Then, you could modify the GUI constructor like this:

private final SelectionResult selectionResult;

public GUI(Object[] objects, SelectionResult selectionResult) {
    this.selectionResult = selectionResult;
    ...

Create a SelectionResult object in your Main class, and pass it to the constructor of the GUI class. In you GUI class ActionListener, you can then call the setSelectionResult() method with the selected value and that value will be available from the Main class.

You would need to add code to make your main method wait while you are waiting for the value to be set in the UI and then proceed with your logic based on the selection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...