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