首页 > 解决方案 > 将侦听器定义的字符串变量导入另一个包

问题描述

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

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

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 但在父类中永远无法识别变量“选择”,这意味着它不会被导入。我不确定如何正确导入变量,因为在线示例假定它是手头的唯一任务。

标签: javaswinglistener

解决方案


为了获得灵活性和更好的 gui 和逻辑解耦,考虑实现mvc 模式
简单地说,使用这个模式你有一个“愚蠢”的视图,它就是这样做的。
你有一个模型类,它负责保存视图使用的信息和逻辑。
您有一个控制器来控制:“连接”视图和模型。
以下是您的代码(1)的mcve版本,旨在展示一个可能的解决方案。为方便起见,可以将整个代码复制粘贴到一个文件中 ( ):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);
    }
} 

通过使用这个简单的结构,视图(Traitform在本例中由 表示)模型由视图更新。
Controller 拥有一个 的实例,Model因此它可以通过 访问其中的信息model.getChoice()
控制器还可以路径对model可能需要它的任何其他类的引用,或者实现一个监听器来监听Model.


(1)在问题中发布 mcve 可以让帮助更轻松、更快捷。


推荐阅读