java - 将监听器定义的字符串变量导入到另一个包中

标签 java swing listener

我有一个包含我的输入表单的包。我的想法是,输入表单将记录答案并将其存储为变量,然后我将这些变量导入到另一个包中,算法将在其中执行计算。

我一直在阅读有关导入变量的内容,但是我不确定如何将代码正确地实现到我现有的代码中。

package inputform;

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

public class Traitform extends JFrame {

        JPanel set1 = new JPanel();
        //add labels
        JRadioButton momEye1 = new JRadioButton("brown");
        JRadioButton momEye2 = new JRadioButton("blue");

        JPanel set2 = new JPanel();
        JRadioButton momHair1 = new JRadioButton("brown");
        JRadioButton momHair2 = new JRadioButton("blonde");

        JPanel set3 = new JPanel();
        JRadioButton dadEye1 = new JRadioButton("brown");
        JRadioButton dadEye2 = new JRadioButton("blue");

        JPanel set4 = new JPanel();
        JRadioButton dadHair1 = new JRadioButton("brown");
        JRadioButton dadHair2 = new JRadioButton("blonde");

        public Traitform () {
            super("Parent Trait Form");
            setSize(1000, 1000);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout layout = new GridLayout(0,1);
            setLayout(layout);

            ButtonGroup group1 =  new ButtonGroup();
            group1.add(momEye1);
            group1.add(momEye2);

            class geneActionListener implements ActionListener {
            @Override
                public void actionPerformed(ActionEvent ex) {
                    String choice = group1.getSelection().getActionCommand();
                    System.out.println("trait selected" + choice);
            } 
        }
            ActionListener al = new geneActionListener();
            momEye1.addActionListener(al);
            momEye2.addActionListener(al);
            momEye1.setActionCommand("brown");
            momEye2.setActionCommand("blue");

            ButtonGroup group2 =  new ButtonGroup();
            group2.add(momHair1);
            group2.add(momHair2);

            ButtonGroup group3 =  new ButtonGroup();
            group3.add(dadEye1);
            group3.add(dadEye2);

            ButtonGroup group4 =  new ButtonGroup();
            group4.add(dadHair1);
            group4.add(dadHair2);

            set1.add(momEye1);
            set1.add(momEye2);

            set2.add(momHair1);
            set2.add(momHair2);

            set3.add(dadEye1);
            set3.add(dadEye2);

            set4.add(dadHair1);
            set4.add(dadHair2);

            add(set1);
            add(set2);
            add(set3);
            add(set4);

            setVisible(true);

            group1.getSelection().getActionCommand();         
        }

    private static void setLookAndFeel() {
        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception exc){

            }
    }
        public static void main(String[] arguments){
            Traitform.setLookAndFeel();
            Traitform frame = new Traitform();
        }       
}   

我要导入的类:

package parents;

import inputform.Traitform;

public class parents {

    public static void main(String[] arguments){

        // algorithm goes here to predict the odds of the child having blue or brown eyes
        // and blonde or brown hair

    }    
}

我以为我可以使用 import 导入变量 choice,但变量“choice”在父类中永远不会被识别,这意味着它不会被导入。我不确定如何正确导入变量,因为在线示例假设它是手头的唯一任务。

最佳答案

为了获得灵 active 以及更好地解耦 GUI 和逻辑,请考虑实现 mvc pattern
简而言之,使用此模式您将获得一个“哑” View ,它就是这样做的。
您有一个模型类,它负责保存 View 使用的信息和逻辑。
您有一个 Controller 可以控制:“连接” View 和模型。
以下为mcve您的代码版本(1),旨在展示可能的解决方案。 为了方便起见,可以将整个代码复制粘贴到一个文件中 (Traitform.java):

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

//better practice is to have a `JFrame instance rather than extending it 
public class Traitform extends JFrame { 

    public Traitform (Model model) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridLayout layout = new GridLayout(0,1);
        setLayout(layout);

        JRadioButton momEye1 = new JRadioButton("brown");
        JRadioButton momEye2 = new JRadioButton("blue");
        ButtonGroup group1 =  new ButtonGroup();
        group1.add(momEye1);
        group1.add(momEye2);

        class geneActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent ex) {
                String choice = group1.getSelection().getActionCommand();
                model.setChoice(choice);
            }
        }

        ActionListener al = new geneActionListener();
        momEye1.addActionListener(al);
        momEye2.addActionListener(al);
        momEye1.setActionCommand("brown");
        momEye2.setActionCommand("blue");

        JPanel set1 = new JPanel();
        set1.add(momEye1);
        set1.add(momEye2);

        add(set1);
        pack();
        setVisible(true);
    }

    public static void main(String[] arguments){
        new Controller();
    }
}

//controls and "wires" view and model
class Controller{
    Model model = new Model();   //construct a model
    Traitform view = new Traitform(model);   //construct a view 
}

//holds information and logic used by view
class Model{
    private String choice;

    String getChoice() {
        return choice;
    }

    void setChoice(String choice) {
        this.choice = choice;
        System.out.println("choice in model changed to "+ choice);
    }
} 

通过使用这个简单的结构, View (在此示例中由 Traitform 表示)、模型由 View 更新。
Controller 持有 Model 的实例,因此可以通过 model.getChoice() 访问其中的信息。
Controller 还可以将 model 的引用传递给可能需要它的任何其他类,或者实现监听 Model 中的更改的监听器。

<小时/> (1) 在问题中发布 mcve 可以让帮助变得更轻松、更快捷。

关于java - 将监听器定义的字符串变量导入到另一个包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57226837/

相关文章:

java - 按下按钮时执行的 Action

android - ListView 项目的自定义监听器

java - Java中如何根据日期高效触发事件?

java - EntityManager 生命周期和持续的客户端-服务器-通信

java - 使用quartz调度程序调度jasper报告

java - 这是java中的类型推断吗?

java - 更改单个 JOptionPane 大小

java - JSpinner 在 Action 监听器中不起作用

java - 不同文件系统JVM在同一台服务器上工作

java - 如何检测组合选择是否是第一次选择