2015-07-11 98 views
0

当你点击按钮“搜索”时,它应该打开类MISControlPanel.java,但它会打开一个窗口,就像你在图片中看到的一样。它不应该是那样的空白。 任何想法为什么这个班是空白?我认为这与初始化“newWindow”为null有关(否则它抛出了这个错误:“局部变量newWindow可能没有被初始化”),所以我不确定。 enter image description here在Jframe中打开新窗口

从代码相关片段下方

MISControlPanel newWindow = null; 
        try { 
         newWindow = new MISControlPanel(); 
        } catch (IOException e3) { 
         // TODO Auto-generated catch block 
         e3.printStackTrace(); 
        } 
         newWindow.setVisible(true); 

满级代码


 import java.awt.EventQueue; 

import javax.naming.InvalidNameException; 
import javax.naming.ldap.LdapName; 
import javax.swing.JFrame; 
import javax.swing.JButton; 

import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.GridLayout; 

import javax.naming.ldap.LdapName; 
import javax.swing.JTextField; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

import java.awt.Color; 
import java.awt.Font; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.Inet4Address; 
import java.net.UnknownHostException; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import javax.swing.JTextArea; 
import javax.swing.JTable; 

public class MISSearch { 

    JFrame frame; 
    static JTextField textField; 
    private JTextField textField_1; 
    JLabel selectedComputerFromAD = new JLabel("testing"); 
    String sCurrentLine = null; 
    String CN = null; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MISSearch window = new MISSearch(); 
       window.frame.setVisible(true); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    * 
    * @throws IOException 
    * 
    * @wbp.parser.entryPoint 
    */ 
    public MISSearch() throws IOException { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    * 
    * @throws IOException 
    */ 

    private void initialize() throws IOException { 
     frame = new JFrame(); 
     frame.getContentPane().setBackground(Color.LIGHT_GRAY); 
     frame.getContentPane().setForeground(Color.RED); 
     frame.setBounds(100, 100, 394, 111); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 
     frame.setTitle("MIS Control Panel"); 
     frame.setResizable(false); 
     FileWriter fw = new FileWriter("C:\\Users\\anoc5f\\Desktop\\Output.txt"); 
     File tempFile = new File("myTempFile.txt"); 
     JButton searchComputerButton = new JButton("Search"); 
     searchComputerButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       MISControlPanel newWindow = null; 
       try { 
        newWindow = new MISControlPanel(); 
       } catch (IOException e3) { 
        // TODO Auto-generated catch block 
        e3.printStackTrace(); 
       } 
        newWindow.setVisible(true); 

        try { 
         new test(); 
        } catch (IOException e2) { 
         // TODO Auto-generated catch block 
         e2.printStackTrace(); 
        } 

       String line; 
       BufferedWriter bw = null; 
       BufferedWriter writer = null; 
       try { 
        writer = new BufferedWriter(new FileWriter(tempFile)); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 


       String s = null; 

       Process p = null; 
       /* 
       * try { // p = Runtime.getRuntime().exec(
       * "cmd /c start c:\\computerQuery.bat computerName"); } catch 
       * (IOException e1) { // TODO Auto-generated catch block 
       * e1.printStackTrace(); } 
       */ 
       try { 

        p = Runtime.getRuntime().exec("c:\\computerQuery.bat"); 

       } catch (IOException e1) { 

        // TODO Auto-generated catch block 

        e1.printStackTrace(); 

       } 
       StringBuffer sbuffer = new StringBuffer(); 
       BufferedReader in = new BufferedReader(new InputStreamReader(p 
         .getInputStream())); 

       try { 

        while ((line = in.readLine()) != null) { 

         System.out.println(line); 

         // textArea.append(line); 

         String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET"; 
         LdapName ldapName = new LdapName(dn); 
         String commonName = (String) ldapName.getRdn(
           ldapName.size() - 1).getValue(); 

        } 
        ComputerQuery.sendParam(); 

       } catch (IOException e1) { 

        // TODO Auto-generated catch block 

        e1.printStackTrace(); 

       } catch (InvalidNameException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } finally 

       { 
        try { 
         fw.close(); 

        } 

        catch (IOException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 

       try { 

        in.close(); 

       } catch (IOException e1) { 

        // TODO Auto-generated catch block 

        e1.printStackTrace(); 

       } 

       ComputerQuery.sendParam(); 
     /* 
        SwingUtilities.invokeLater(new Runnable() { 
         public void run() { 
          testLabel.setText(CN); 
         } 
         }); 
      */ 
      } 
     }); 


     try (BufferedReader br = new BufferedReader(new FileReader(
       "resultofbatch.txt"))) { 

      final Pattern PATTERN = Pattern.compile("CN=([^,]+).*"); 
      try { 
       while ((sCurrentLine = br.readLine()) != null) { 

        String[] tokens = PATTERN.split(","); // This will return 
                  // you a array, 
                  // containing the 
                  // string array 
                  // splitted by what 
                  // you write inside 
                  // it. 
        // should be in your case the split, since they are 
        // seperated by "," 
        // System.out.println(sCurrentLine); 
        CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0]; 

        System.out.println(CN); 

       } 

      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
     } catch (IOException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 
     // SwingUtilities.invokeLater(updateCN()); 
     searchComputerButton.setBounds(269, 17, 89, 20); 
     frame.getContentPane().add(searchComputerButton); 

     textField = new JTextField(); 
     textField.setBounds(164, 17, 95, 20); 
     frame.getContentPane().add(textField); 
     textField.setColumns(10); 

     JLabel lblComputerName = new JLabel("Computer Name:"); 
     lblComputerName.setForeground(Color.BLACK); 
     lblComputerName.setFont(new Font("Tahoma", Font.BOLD, 14)); 
     lblComputerName.setBounds(41, 11, 124, 29); 
     frame.getContentPane().add(lblComputerName); 
     java.net.InetAddress localMachine = null; 
     try { 
      localMachine = java.net.InetAddress.getLocalHost(); 
     } catch (UnknownHostException e2) { 
      // TODO Auto-generated catch block 
      e2.printStackTrace(); 
     } 

     JLabel label = null; 
     JLabel lblOpid = new JLabel("Computer by OPID:"); 
     lblOpid.setFont(new Font("Tahoma", Font.BOLD, 14)); 
     lblOpid.setBounds(23, 46, 137, 20); 
     frame.getContentPane().add(lblOpid); 

     textField_1 = new JTextField(); 
     textField_1.setBounds(164, 48, 95, 20); 
     frame.getContentPane().add(textField_1); 
     textField_1.setColumns(10); 

     JButton btnNewButton_2 = new JButton("Search"); 
     btnNewButton_2.setBounds(269, 47, 89, 23); 
     frame.getContentPane().add(btnNewButton_2); 

    } 
    public void setVisible(boolean visible){ 
     frame.setVisible(true); 
    } 
    private void threadStart() { 
      SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 

         } 
      }); 
     } 

} 
+0

我没有收到NullPointerException。我没有收到任何错误,只是没有正确开放课程。它是空白的。我被迫初始化“newWindow”,但是我初始化为null – user6680

+0

'setVisible()'方法应该添加到'MISControlPanel'类中,而不是'MISSearch'。我认为这些是单独的类 –

回答

1

我想这是因为你用newWindow.setVisible(true);但你MISControlPanel没有扩展Window类继承setVisible()米ethod,但有JFrame字段,您需要将其设置为分隔符。尝试将此方法添加到MISControlPanel

public void setVisible(boolean visible){ 
    frame.setVisible(visible); 
} 
+0

我添加了该方法,但我认为它可能会试图扩展窗口,因为MISControlPanel类仍然空白。我在上面添加了完整的类代码。添加的setVisible方法位于类的底部。查看上面的代码 – user6680

+0

@ user6680但是现在没有MISControlPanel类。所以没有它,它是如何工作的?你只是改名? –

+0

MISControlPanel类仍然存在。它位于上面的MISSearch类的默认包中。 – user6680