为什么点击后我的第二个框架没有出现?

时间:2015-02-17 20:50:33

标签: java swing jframe jpanel

当我点击打开第二帧时,它不会出现。但是,如果我选择只运行相同的帧,它会出现(在eclipse中)。我确实有一个mntmSetup的听众,并确认它正在被击中。难道我做错了什么?我很困惑为什么GUI不会显示。

代码:

在我的主要电话中呼叫:

 else if (obj == Main.mntmSetup) 
  {
      TestSetup setup = null;
    try {
        setup = new TestSetup();
    } catch (ParserConfigurationException
            | SAXException | IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      setup.setVisible(true);
   }

TestSetup.java:

public class TestSetup extends JPanel {
 /**
 * 
 */
private static final long serialVersionUID = 1L;
private boolean debug = true,  // Prints out debug messages
                 booleanValue;   // Used to convert String[] execute to a boolean value
XMLparser xml = new XMLparser();
public String[] test_case = xml.CreateTestCasesArray("testcase", "filename");    // Get the test case name from the CLA testsuite.xml
public String[] iterations = xml.CreateTestCasesArray("testcase", "iterations"); // Get the amount of iterations from the CLA testsuite.xml
public String[] execute = xml.CreateTestCasesArray("testcase", "execute");       // Get the execute status (true or false) from the CLA testsuite.xml
public String[] parameter = xml.CreateTestCasesArray("testcase", "parameter");       // Get the execute status (true or false) from the CLA testsuite.xml
private SaveToXml save = new SaveToXml();

 public TestSetup() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {

      super(new GridLayout(1, 0));

      ArrayList<MyObject> list = new ArrayList<MyObject>();


      for(int i = 0; i < test_case.length;i++){
        if(debug){
            System.out.println("---------------------------------------------");
            System.out.println("test_case [" + i + "] = '" + test_case[i] + "'");
            System.out.println("iterations [" + i + "] = '" + iterations[i] + "'");
            System.out.println("execute [" + i + "] = '" + execute[i] + "'");
            System.out.println("parameter [" + i + "] = '" + parameter[i] + "'");
            System.out.println("---------------------------------------------");
            System.out.println("\n");
            //SaveToXml xml = new SaveToXml();
            //xml.WriteProductsXml();
        }
        if(execute[i].equals("true"))
            booleanValue = true;
        else
            booleanValue = false;
        // uppercase for nice formatting
        char temp = parameter[i].charAt(0);
        String sTemp = "" + temp;
        sTemp = sTemp.toUpperCase();
        parameter[i] = sTemp + parameter[i].substring(1, parameter[i].length());
        list.add(new MyObject(test_case[i], new Integer(iterations[i]), new Boolean(booleanValue), parameter[i]));
     }          
      JTable table = new JTable(new MyTableModel(list));
      table.setPreferredScrollableViewportSize(new Dimension(600, 500));
      table.setFillsViewportHeight(true);
      // Create the scroll pane and add the table to it.
      JScrollPane scrollPane = new JScrollPane(table);

      //Fiddle with the Sport column's cell editors/renderers.
      setInterleavedColumn(table, table.getColumnModel().getColumn(3));

      // Add the scroll pane to this panel.
      add(scrollPane);
 }

 class MyObject {
      String testCase;
      int iterations;
      boolean isExecute;
      String parameter;

      MyObject(String testCase, int iterations, boolean isExecute, String parameter) {
           this.testCase = testCase;
           this.iterations = iterations;
           this.isExecute = isExecute;
           this.parameter = parameter;
      }
 }

 public void setInterleavedColumn(JTable table,
         TableColumn executionSytle) {
    //Set up the editor for the sport cells.
    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.addItem("Parallel");
    comboBox.addItem("Interleaved");
    executionSytle.setCellEditor(new DefaultCellEditor(comboBox));

    //Set up tool tips for the sport cells.
    DefaultTableCellRenderer renderer =
     new DefaultTableCellRenderer();
    renderer.setToolTipText("Click for combo box");
    executionSytle.setCellRenderer(renderer);
}

 class MyTableModel extends AbstractTableModel {
      /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String[] columnNames = { "Test Name", "Iterations", "Execute", "Execution Style"};
      ArrayList<MyObject> list = null;

      MyTableModel(ArrayList<MyObject> list) {
           this.list = list;
      }

      public int getColumnCount() {
           return columnNames.length;
      }

      public int getRowCount() {
           return list.size();
      }

      public String getColumnName(int col) {
           return columnNames[col];
      }

      public Object getValueAt(int row, int col) {

           MyObject object = list.get(row);

           switch (col) {
           case 0:
                return object.testCase;
           case 1:
                return object.iterations;
           case 2:
                return object.isExecute;
           case 3:
               return object.parameter;
           default:
                return "unknown";
           }
      }
      @Override
      public boolean isCellEditable(int row, int col) {
          return true;
      }
      @SuppressWarnings("unchecked")
      public Class getColumnClass(int c) {
           return getValueAt(0, c).getClass();
      }
      @Override
      public void setValueAt(Object value, int row, int col) {
          MyObject obj = list.get(row);
          if(col == 0){
              obj.testCase = (String)value;
          } else if(col == 1){
              obj.iterations = (int)value;
          } else if (col == 2) {
              obj.isExecute = (Boolean)value;
          } else if (col == 3) {
              obj.parameter = (String)value;
          }
          fireTableCellUpdated(row, col);              
          try {
            SaveDataEntered(value, row, col);
        } catch (ParserConfigurationException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

      }

      // col = 0 / Test Case Name
      // col = 1 / Num of Iterations
      // col = 2 / Execute status
      // col = 3 / Execute Style
      private void SaveDataEntered(Object value, int row, int col) throws ParserConfigurationException, IOException {
          MyObject obj = list.get(row);
          if(col == 0){
              System.out.println("test case column");
              test_case[row] = (String)value;
          } else if(col == 1){
              System.out.println("iterations column");
              iterations[row] = Integer.toString((int)value);
          } else if(col == 2){
              System.out.println("execute column");
              execute[row] = Boolean.toString((Boolean)value);
          } else if(col == 3){
              obj.parameter = (String)value;
              System.out.println("before parameter parameter[" + row + "] = " + parameter[row]);                  
              System.out.println("(String)value = " + (String)value);                 
              parameter[row] = (String)value;
              System.out.println("after parameter parameter[" + row + "] = " + parameter[row]);    
          }

          save.WriteProductsXml(test_case, test_case, iterations, execute, parameter); 

          if(debug){
              System.out.println("    row " + row + ":");
              System.out.println("--------------------------");
              System.out.println("test_case selection\t= '" + test_case[row] + "'");
              System.out.println("value\t\t\t= '" + value + "'");
              System.out.println("row\t\t\t= '" + row + "'");
              System.out.println("col\t\t\t= '" + col + "'");
              System.out.println("--------------------------");
          }

      }
 }

 /**
  * Create the GUI and show it. For thread safety, this method should be
  * invoked from the event-dispatching thread.
 * @throws IOException 
 * @throws SAXException 
 * @throws ParserConfigurationException 
 * @throws FileNotFoundException 
  */
 private static void createAndShowGUI() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
      // Create and set up the window.
      JFrame frame = new JFrame("Test Setup");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create and set up the content pane.
      TestSetup newContentPane = new TestSetup();
      newContentPane.setOpaque(true); // content panes must be opaque
      frame.setContentPane(newContentPane);

      // Display the window.
      frame.pack();
      frame.setVisible(true);
 }

 public static void main(String[] args) {
      // Schedule a job for the event-dispatching thread:
      // creating and showing this application's GUI.
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
                try {
                    createAndShowGUI();
                } catch (ParserConfigurationException | SAXException
                        | IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
           }
      });
 }

}

1 个答案:

答案 0 :(得分:0)

要获得像Eclipse一样的第二帧,请将TestSetup#createAndShowGUI公开并在第一个片段中调用它:

else if (obj == Main.mntmSetup)
{
    try{
        TestSetup.createAndShowGUI();
    } catch (ParserConfigurationException | SAXException
                    | IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
    }
}