怎么让摇摆程序等待

时间:2013-03-11 07:20:50

标签: java swing

基本上我正在编写swing程序,其中我通过另一个类调用我的swing程序类。每当我运行我的主类时它会执行并显示swing类,但即使在我按下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);
    }
}

3 个答案:

答案 0 :(得分:1)

使用模式JDialog代替JFrame

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

请查看How to use DialogsHow to Use Modality in Dialogs以获取更多信息

答案 1 :(得分:0)

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

准确地说,Java命名约定希望类名是camel case表示法的大写字母。也就是说,“select_common_attr1”应该是“SelectCommmonAttr1”。 希望它有所帮助。

答案 2 :(得分: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();
        }
    });

...