我有一个 JFrame 打开另一个 JFrame,但是当我尝试在第二个 JFrame 弹出时单击它时,没有任何点击。问题是,如果我选择旧的 JFrame(单击它),然后选择新的,那么新的就会起作用。问题是当它弹出时我需要它工作,因为显然我不希望用户必须点击一百个地方才能让程序工作。
这是调用/创建新 JFrame 的代码:
public void displayData(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
DisplayWindow w = new DisplayWindow(novice, advanced);
w.setLocation(this.getLocation().x+20, this.getLocation().y+20);
w.setVisible(true);
w.setState(JFrame.NORMAL);
}
这是“DisplayWindow”的代码(我怀疑顶部的方法与它无关,因为它们根本不直接管理 JFrame,但我仍然发布它们):
public class DisplayWindow extends JFrame {
private JPanel contentPane;
private ArrayList<Judge> novice;
private ArrayList<Judge> advanced;
/**
* Create the frame.
*/
public static String getSingleText(ArrayList<Judge> d) {
StringBuilder product = new StringBuilder("");
for (Judge j : d) {
product.append(String.format("Judge: %s ", j.toString() + (j.getDebates().get(0).isAdvanced() ? 'A' : 'N')));
for (Debate de : j.getDebates()) {
product.append(String.format("Round: %s ", de != null ? de.toString() : "null"));
}
product.append("\n");
}
return product.toString();
}
public String getText(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
StringBuilder product = new StringBuilder("");
product.append("Advanced:\n");
product.append(getSingleText(advanced));
product.append("\nNovice:\n");
product.append(getSingleText(novice));
return product.toString();
}
public DisplayWindow(ArrayList<Judge> novice, ArrayList<Judge> advanced) {
this.novice = novice;
this.advanced = advanced;
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextArea textArea = new JTextArea(getText(novice, advanced));
textArea.setEditable(false);
JScrollPane scroll = new JScrollPane(textArea);
contentPane.add(scroll, BorderLayout.CENTER);
JLabel lblNewLabel = new JLabel("Debate Calculation:");
contentPane.add(lblNewLabel, BorderLayout.NORTH);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton btnSave = new JButton("Save");
panel.add(btnSave);
}
}
而且,以防万一,这里是旧 JFrame 的创建代码:
public DataWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
setLocationRelativeTo(null);
String[] columnNames = {"School",
"Advanced #",
"Novice #",
};
Object[][] data = {
};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
table = new JTable(model){
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return Integer.class;
case 2:
return Integer.class;
case 3:
return Integer.class;
default:
return Integer.class;
}
}
};
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new WrapLayout(FlowLayout.CENTER, 5, 5));
chckbxCantJudgeOwn.setSelected(true);
panel.add(chckbxCantJudgeOwn);
chckbxCantDebateOwn.setSelected(true);
panel.add(chckbxCantDebateOwn);
chckbxCantJudgeSchool.setSelected(true);
panel.add(chckbxCantJudgeSchool);
chckbxCantDebateSchool.setSelected(true);
panel.add(chckbxCantDebateSchool);
chckbxCantDebateSchool.setSelected(true);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new GridLayout(0, 2, 0, 0));
JButton btnNewButton = new JButton("+");
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"?????", 0, 0});
}
});
panel_1.add(btnNewButton);
JButton button = new JButton("-");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int[] selectedRows = table.getSelectedRows();
if (selectedRows.length > 0) {
for (int i = selectedRows.length - 1; i >= 0; i--) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.removeRow(selectedRows[i]);
}
}
}
});
panel_1.add(button);
btnCancel.setEnabled(false);
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processer.stop();
toggleButtons();
}
});
btnProcess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processer = new Thread(new Runnable() {
public void run() {
startProcess(getData(table));
}
});
processer.start();
toggleButtons();
}
});
panel_1.add(btnProcess);
panel_1.add(btnCancel);
}