0

基本上我正在编写摇摆程序,其中我通过另一个类调用我的摇摆程序类。每当我运行我的主类时,它都会执行并显示摇摆类,但即使在我按下 OK 按钮(Jbutton)之前,我的主类也会完全执行。请提出一些简单的方法来解决这个问题,因为我是新手。

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


public class select_common_attr1 {

    public JFrame frame;
    JCheckBox checkBoxArray[];
    public String returnstring[];
    public int set_n;
    public boolean test;
    public JButton btnOk;
    public JButton btnReset; 
    /**
     * Launch the application.
     */
    /*public static void main(String[] args) {
        //EventQueue.invokeLater(new Runnable() {
            String t[]={"one","two","three"};
            //public void run() {
                //try {
            select_common_attr1 window = new select_common_attr1(t,t.length);
                    //window.print();
                    window.frame.setVisible(true);

                //} catch (Exception e) {
                    //e.printStackTrace();
                //}
            //}
        //});

    }*/

    public select_common_attr1(String common_attr[],int len) {
        initialize(common_attr,len);
        frame.setVisible(true);
    }

    private void initialize(String common_attr[],int len) {
        frame = new JFrame();
        test=false;
        frame.setBounds(100, 100, 452, 263);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        checkBoxArray=new JCheckBox[len];
        for(int j=0;j<len;j++)
        {
            checkBoxArray[j]=new JCheckBox(common_attr[j]);
        }
        for(int i = 0; i<len; i++)
        {
           panel.add(checkBoxArray[i]);
        }
        btnOk = new JButton("OK");
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int count=0;
                for(int i=0; i<checkBoxArray.length; i++)
                {
                 if(checkBoxArray[i].isSelected())
                 {
                     count++;
                 }
                }
                set_n=count;
                returnstring=new String[set_n];
                count=0;
                for(int i=0; i<checkBoxArray.length; i++)
                {
                 if(checkBoxArray[i].isSelected())
                 {

                     returnstring[count++]=checkBoxArray[i].getText();
                 }
                }
                test=true;
                print();
            }
        });

        btnReset = new JButton("RESET");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                for(int i=0; i<checkBoxArray.length; i++)
                {
                 if(checkBoxArray[i].isSelected())
                 {
                     checkBoxArray[i].setSelected(false);
                 }
                }
                test=false;
            }
        });
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(158)
                            .addComponent(btnOk)
                            .addGap(18)
                            .addComponent(btnReset))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(panel, GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)))
                    .addContainerGap())
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
                    .addGap(18)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(btnOk)
                        .addComponent(btnReset))
                    .addContainerGap(66, Short.MAX_VALUE))
        );
        frame.getContentPane().setLayout(groupLayout);
    }
}
4

3 回答 3

1

使用模态JDialog而不是JFrame.

这将停止程序执行,直到对话框关闭

查看如何使用对话框如何在对话框中使用模态以获取更多信息

于 2013-03-11T07:27:18.940 回答
0

您可以添加一个锁来处理您的逻辑,请参见以下代码:

static Object lock = new Object();

public static void main(String[] args) throws Exception {
    synchronized (lock) {
        String t[] = { "one", "two", "three" };
        select_common_attr1 window = new select_common_attr1(t, t.length);
        System.out.println("???????");
        try {
            lock.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("#####################");
    }
}

private void next(){
    synchronized (lock) {
        frame.setVisible(false);
        lock.notify();
                    // You can process your business at here.

    }
}
private void initialize(String common_attr[], int len) {
    frame = new JFrame();
    test = false;
    frame.setBounds(100, 100, 452, 263);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    checkBoxArray = new JCheckBox[len];
    for (int j = 0; j < len; j++) {
        checkBoxArray[j] = new JCheckBox(common_attr[j]);
    }
    for (int i = 0; i < len; i++) {
        panel.add(checkBoxArray[i]);
    }
    btnOk = new JButton("OK");
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int count = 0;
            for (int i = 0; i < checkBoxArray.length; i++) {
                if (checkBoxArray[i].isSelected()) {
                    count++;
                }
            }
            set_n = count;
            returnstring = new String[set_n];
            count = 0;
            for (int i = 0; i < checkBoxArray.length; i++) {
                if (checkBoxArray[i].isSelected()) {

                    returnstring[count++] = checkBoxArray[i].getText();
                }
            }
            test = true;
            next();
        }
    });

    btnReset = new JButton("RESET");
    btnReset.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < checkBoxArray.length; i++) {
                if (checkBoxArray[i].isSelected()) {
                    checkBoxArray[i].setSelected(false);
                }
            }
            test = false;

            next();
        }
    });

...
于 2013-03-11T08:07:33.150 回答
0

如果您希望用户按下 OK 按钮后发生某些事情,则必须将代码放入其 actionListener 方法中。这就是事件监听器的用途。在此处查看一些背景文档:http: //docs.oracle.com/javase/tutorial/uiswing/events/intro.html

准确地说,Java 命名约定希望类名使用驼峰式表示法大写。也就是说,“select_common_attr1”实际上应该是“SelectCommmonAttr1”。希望能帮助到你。

于 2013-03-11T07:43:37.727 回答