Java 为不同的swing类打开新的JFrame窗口

Java 为不同的swing类打开新的JFrame窗口,java,swing,Java,Swing,我在jbutton click上打开另一个swing类时遇到问题。在action listener中,我将 JButton searchComputerButton = new JButton("Search"); searchComputerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MISMain mi

我在jbutton click上打开另一个swing类时遇到问题。在action listener中,我将

JButton searchComputerButton = new JButton("Search");
    searchComputerButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             MISMain misMain = new MISMain();
             misMain.setVisible(true);
             etc...
但是我在mismin.setVisible(true)上得到了一个红色的扭曲错误,我不确定为什么。 它表示“类型不匹配的方法setVisible(boolean)未定义” 在Eclipse中,这两个类都在同一个包中,它在类中识别不匹配,所以我不确定为什么会出现错误。如果您需要更多信息,请告诉我。感谢您的帮助

按要求输入第一部分

public MISMain() 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, 658, 618);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setTitle("MIS Advanced Computerers");
        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) {

                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();


            }
        });

您可以将方法添加到
MSMain
,这将使
frame
可见:

public void setVisible(boolean input){
    frame.setVisible(input);
}

没有扩展JFrame

Mismin不是一个框架-对吗


因此,无法将其设置为可见。为“框架”添加一个getter(并使其成为成员)。您可以将帧设置为可见。

如注释中所述,您不能在
MISMain
上调用
setVisible
方法,因为该类没有
setVisible
方法。相反,它是
JFrame
的一部分

有两种可能的解决方案

一个是不匹配的is-AJFrame。该实现看起来是这样的。注意:因为它本身就是JFrame,所以不必有框架成员变量

class MISMain extends JFrame {
   public MISMain() throws IOException {
     getContentPane().setBackground(Color.LIGHT_GRAY);
     getContentPane().setForeground(Color.RED);
     setBounds(100, 100, 658, 618);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     getContentPane().setLayout(null);
     setTitle("MIS Advanced Computerers");
     setResizable(false);
     //All other code.
   }
}
然后从
actionPerformed
创建一个新的mismin实例,并调用
setVisible(true)在其上,框架将可见

class MISMain {
  JFrame frame;
  //... other field
  //..all the implementations you have
  //Now add a method like : show() as below

  public void show() {
   frame.setVisible(true);
  }
}
第二种方法是错配HAS-AJFrame。此实现基本上与您当前的实现类似。但是,您必须添加一些代码,使框架可见

class MISMain {
  JFrame frame;
  //... other field
  //..all the implementations you have
  //Now add a method like : show() as below

  public void show() {
   frame.setVisible(true);
  }
}

至少在Swing windows的情况下,IS-A方法比HAS-A更直观。

请至少在几行起始线上发布
missin的代码。错误基本上是说,
missin
类中没有setVisible方法。似乎
MISMain
类没有扩展
JFrame
。我更新了上面的代码,并添加了一部分与之相关的mismin。我还根据对公共类mismin扩展JFrame的建议更新了mismin{并且它会打开一个新的gui,但它是空的,并且不是missin类。仅仅扩展
JFrame
不会有帮助。这将不会显示您使用
frame=new JFrame();
code创建的框架。相反,在
missin
中提供一个名为
setVisible()
的公共方法,在那里您实际执行
frame.setVisible(true)
尝试在回答中提供更多信息。为什么这样做?这不是引用另一个类。我需要它来打开名为miscrotrolpanel的类,该类与mismin位于同一个包中。然后您应该有一种方法从客户端调用miscrotrolpanel(事件处理程序代码)上的
setVisible(true)