我正在尝试使用以下代码从现有屏幕打开一个新屏幕,
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();
}
});
}
}
请你建议我这样做吗?提前谢谢。
答案 0 :(得分:-1)
我通过添加JPanel和我的顶级容器(如JB Nizet建议)来做到这一点。我的代码在下面,
public class ConfigureSystemPage extends JFrame{
JTextField text;
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 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("Expeditors");
radioGroup.add(Male);
Female=new JRadioButton("Non Expeditors");
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 ");
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("EI Scanning");
// frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel(new GridBagLayout());
// content.setBackground(Color.GREEN);
content.setBorder(new EmptyBorder(20, 20, 20, 20));
frame.setContentPane(content);
frame.add(new CustomerConfigurationPane());
frame.pack();
frame.setSize(1000,700);
frame.setLocationRelativeTo(null);
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 CustomerConfigurationPane extends JPanel {
public CustomerConfigurationPane() {
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);
}
}
}
}
}
谢谢JB Nizet。快乐的编码...