在另一个类中获取JButton的结果

时间:2017-07-07 08:03:38

标签: java swing

我在另一个类中创建了一个类的实例。在课堂上,我创建了一个带有radiobuttons和按钮的JFrame。当用户点击按钮时,我希望选择的radiobutton文本返回另一个类。但它不起作用。换句话说,一个未返回的变量。我该怎么办?

我的第一堂课的代码:

public class SelectLayoutCreator {
public JFrame  f = new  JFrame("Choos Rule");
public String a;
public SelectLayoutCreator(ArrayList<String> choose) {
// TODO Auto-generated constructor stub
ArrayList<JRadioButton> ruleRadioButtions = new ArrayList<JRadioButton>();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
f.setLayout(null);
JTextField ruleText = new JTextField();
ruleText.setBounds(30, 67, 90, 10);
f.add(ruleText);
JLabel ruleLabel = new JLabel ("Which of the following rules should be execute?");
ruleLabel.setBounds(20, 20, 350, 20);
f.add(ruleLabel);
ButtonGroup group = new ButtonGroup();
 for (int i = 0; i < choose.size(); i++) {
  JRadioButton radio = new JRadioButton(choose.get(i));
  radio.setActionCommand(choose.get(i));
  ruleRadioButtions.add(radio);
  ruleRadioButtions.get(i).setBounds(20, 50+20*i, 200, 30);
  group.add(ruleRadioButtions.get(i));
  f.add(ruleRadioButtions.get(i));
  }
 JButton b = new JButton("Select");
 b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    **a** =  group.getSelection().getActionCommand();
       }   
    });
f.add(b);
b.setBounds(270,220,100, 40);    
e.setBounds(170,120,100, 40);
f.setVisible(true);
 }

另一堂课的鳕鱼:

public class interfaceRule {
public String call(ArrayList<String> choose)
{
  SelectLayoutCreator s = new SelectLayoutCreator(choose);
  return s.a;
}
}

1 个答案:

答案 0 :(得分:0)

您设计和实施当前解决方案的方式存在问题。当调用call()方法时,会发生以下情况:

1)创建JFrame RadioButtons并显示在屏幕上。

SelectLayoutCreator s = new SelectLayoutCreator(choose);

2)执行方法继续,因为GUI在单独的线程上运行。因此,执行return语句并返回null值,因为String尚未初始化。

return s.a;

您的代码现在不会等待用户进行选择并单击“选择”按钮。

这是一个基于您的解决方案的工作示例(由于编码风格不好,我重构了一些代码)。您可以将其放在 Main.java 文件中并运行它:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


class SelectLayoutCreator {

    private JFrame f = new JFrame("Choose Rule");
    private String a;

    public SelectLayoutCreator(ArrayList<String> choose, Object lock) {
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400, 300);
        f.setLayout(null);

        JTextField ruleText = new JTextField();
        ruleText.setBounds(30, 67, 90, 10);
        f.add(ruleText);

        JLabel ruleLabel = new JLabel("Which of the following rules should be executed?");
        ruleLabel.setBounds(20, 20, 350, 20);
        f.add(ruleLabel);

        ButtonGroup group = new ButtonGroup();
        ArrayList<JRadioButton> ruleRadioButtions = new ArrayList<JRadioButton>();
        for (int i = 0; i < choose.size(); i++) {
            JRadioButton radio = new JRadioButton(choose.get(i));
            radio.setActionCommand(choose.get(i));
            ruleRadioButtions.add(radio);
            ruleRadioButtions.get(i).setBounds(20, 50 + 20 * i, 200, 30);

            group.add(ruleRadioButtions.get(i));
            f.add(ruleRadioButtions.get(i));
        }

        JButton b = new JButton("Select");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                a = group.getSelection().getActionCommand();

                /* wake up the thread waiting inside InterfaceRule */
                synchronized (lock) {
                    lock.notify();
                }

                f.dispose();
            }
        });
        b.setBounds(270, 220, 100, 40);
        f.add(b);

        f.setVisible(true);
    }

    public String getA() {
        return a;
    }

}


class InterfaceRule {

    private static Object lock = new Object();

    public String call(ArrayList<String> choose) {
        SelectLayoutCreator s = new SelectLayoutCreator(choose, lock);

        /* wait for the String to be populated */
        synchronized (lock) {
            try {
                lock.wait();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return s.getA();
    }

}


public class Main {

    public static void main(String[] args) {
        ArrayList<String> choose = new ArrayList<String>();
        choose.add("1");
        choose.add("2");
        choose.add("3");

        String chosen = new InterfaceRule().call(choose);
        System.out.println(chosen);
    }

}