【发布时间】:2017-10-27 06:24:30
【问题描述】:
我正在尝试使用以下代码从现有屏幕打开一个新屏幕,
CheckABunch frame = new CheckABunch();
frame.setVisible(true);
但是窗口没有打开。每当我尝试执行第二类时,它都会打开窗口。但是当我试图提供到下一个屏幕的链接时,它不会打开屏幕。我的第一堂课扩展了 JFrame,我的下一堂课扩展了 JPanel。
我的第一堂课在下面,
public class ConfigureSystemPage extends JFrame{
JTextField text;
public static void main(String[] args) throws Exception{
new ConfigureSystemPage();
}
public ConfigureSystemPage(){
JFrame f = new JFrame("Configure System");
f.getContentPane().setLayout(null);
JLabel lab = new JLabel("System:");
final JRadioButton Male,Female;
ButtonGroup radioGroup=new ButtonGroup();
Male=new JRadioButton("CPU");
radioGroup.add(Male);
Female=new JRadioButton("Monitor");
radioGroup.add(Female);
Female.setSelected(true);
JButton cancelbutton=new JButton("Cancel");
JButton button=new JButton("Next");
lab.setBounds(50,20,70,20);
Male.setBounds(110,20,100,20);
Female.setBounds(210,20,150,20);
button.setBounds(50,50,80,15);
cancelbutton.setBounds(50,50,80,15);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if((Male.isSelected()==false)&&(Female.isSelected()==false)){
System.out.println("if part ");
JOptionPane.showMessageDialog(null,"Please select system settings");
}else {
System.out.println("else part ");
CheckABunch frame = new CheckABunch();
frame.setVisible(true);
}
}
});
f.add(lab);
f.add(Male);
f.add(Female);
f.add(button);
f.setSize(1000,700);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我使用下面的类作为第二类,
public class CheckABunch extends JPanel {
private static final int CHECK_COL = 1;
private static final Object[][] DATA = {
{"One", Boolean.TRUE}, {"Two", Boolean.FALSE},
{"Three", Boolean.TRUE}, {"Four", Boolean.FALSE},
{"Five", Boolean.TRUE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.TRUE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.TRUE}, {"Ten", Boolean.FALSE}, {"One", Boolean.TRUE}, {"Two", Boolean.FALSE},
{"Three", Boolean.TRUE}, {"Four", Boolean.FALSE},
{"Five", Boolean.TRUE}, {"Six", Boolean.FALSE},
{"Seven", Boolean.TRUE}, {"Eight", Boolean.FALSE},
{"Nine", Boolean.TRUE}, {"Ten", Boolean.FALSE}};
private static final String[] COLUMNS = {"Number", "CheckBox"};
private DataModel dataModel = new DataModel(DATA, COLUMNS);
private JTable table = new JTable(dataModel);
private DefaultListSelectionModel selectionModel;
public CheckABunch() {
super(new BorderLayout());
this.add(new JScrollPane(table));
this.add(new ControlPanel(), BorderLayout.SOUTH);
table.setPreferredScrollableViewportSize(new Dimension(250, 175));
selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
}
private class DataModel extends DefaultTableModel {
public DataModel(Object[][] data, Object[] columnNames) {
super(data, columnNames);
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == CHECK_COL) {
return getValueAt(0, CHECK_COL).getClass();
}
return super.getColumnClass(columnIndex);
}
@Override
public boolean isCellEditable(int row, int column) {
return column == CHECK_COL;
}
}
private class ControlPanel extends JPanel {
public ControlPanel() {
this.add(new JLabel("Selection:"));
this.add(new JButton(new SelectionAction("Clear", false)));
this.add(new JButton(new SelectionAction("Finish", true)));
}
}
private class SelectionAction extends AbstractAction {
boolean value;
String name;
public SelectionAction(String name, boolean value) {
super(name);
this.name = name;
this.value = value;
}
@Override
public void actionPerformed(ActionEvent e) {
if(name.equalsIgnoreCase("Finish")) {
}else {
for (int i = 0; i < dataModel.getRowCount(); i++) {
// if (selectionModel.isSelectedIndex(i)) {
System.out.println("actionPerformed value : " + value+" "+i+" "+CHECK_COL+" "+name);
dataModel.setValueAt(value, i, CHECK_COL);
// }
}
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Configure Customer");
frame.add(new CheckABunch());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setSize(1000,700);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
}
您能建议我这样做吗?提前致谢。
【问题讨论】:
-
发布一个完整的最小示例来重现问题。
-
@JBNizet 我已经发布了两个类。请检查一下
-
CheckABunch 不是 JFrame。这是一个JPanel。您不能只使 JPanel 可见并希望它在新窗口中打开。只有 JFrame、JWindow 和 JDialog 等顶级容器才能像这样可见。 JPanel 应该被添加到顶级容器中。你可能想要一个 JDialog 在这里。
-
Whenever i try to execute second class its open the window.这是什么意思?你在哪里叫它,所以会打开一个窗口?第二次执行是什么意思?创建该类型的对象? -
@ThomasBöhm 我的意思是,我正在单独执行该类。那个时候它打开了新窗口