【问题标题】:How do I open a JFrame from another class?如何从另一个类打开 JFrame?
【发布时间】:2015-06-05 13:24:33
【问题描述】:

所以我需要使用“Louncher”类在“Window”类中打开一个 JFrame。 它不起作用,我不知道为什么,因为我真的是 java 新手。

这就是 Louncher:

package struktogrammer;

public class Louncher {

    public static void main(String[] args) {

        Window feld = new Window();

    }

}

这是窗口:

package struktogrammer;


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Window extends JFrame {

    private int width = 1000;
    private int hight = 750;

    public Window() {

        Window frame = new Window();

        frame.setTitle("Struktogramm");
        frame.setSize(width, hight);
        frame.setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                dispose();
                System.exit(0);
            }
        });
    }

    public Window(int width, int hight) {

        this.width = width;
        this.hight = hight;

        Window frame = new Window();

        frame.setTitle("Struktogramm");
        frame.setSize(width, hight);
        frame.setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
                dispose();
                System.exit(0);
            }
        });
    }

}

当运行 Louncher 类时,我得到了 5 页的错误:

Exception in thread "main" java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at javax.swing.JFrame.<init>(Unknown Source)
    at struktogrammer.Window.<init>(Window.java:15)
    at struktogrammer.Window.<init>(Window.java:17)
    ... (same lign about 1200 times)
    at struktogrammer.Window.<init>(Window.java:17)

它接缝我运行一个无限循环,因为它不能使用构造函数? 请帮忙!

【问题讨论】:

  • public Window() { Window frame = new Window(); 这将导致无限循环。你知道为什么吗?
  • 哦,哇...我现在感觉有点迟钝了 :D 非常感谢!
  • 不要忘记 both 构造函数会犯同样的错误。 ;)
  • 提示:而不是addWindowListener(.. 使用this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 或(我更喜欢)this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

标签: java swing class jframe stack-overflow


【解决方案1】:

每次您创建一个新窗口时,都会创建另一个窗口...无限。您需要在 Window 对象的构造函数中删除另一个 Window 对象的创建。

 public Window() {

    // Window frame = new Window(); <---Remove this

    frame.setTitle("Struktogramm");
    frame.setSize(width, hight);
    frame.setVisible(true);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent ev) {
            dispose();
            System.exit(0);
        }
    });
}

【讨论】:

    【解决方案2】:

    在 Window 类中,您正在再次创建窗口类框架:

     Window frame = new Window();
    

    由于您不需要每次调用该方法时都在此类中创建框架,因此请将其删除。每次创建框架时,您都会创建一个框架。无限循环。

    【讨论】:

      【解决方案3】:

      制作一个 JFrame 并在其中添加一个 JButton,而不是在按钮中添加动作侦听器并在其中添加此代码,如下所示:

      这段代码创建了一个带有按钮的框架,当按下按钮时,新窗口被打开。

      public class Example extends JFrame {
      
      public Example() {
          super("Title");
          setLayout(new FlowLayout());
      
          JButton b = new JButton("Open new Frame");
          b.addActionListener(new ActionListener() {
      
              public void actionPerformed(ActionEvent arg0) {
      
                  newWindow nw = new newWindow();
      
              }
          });
      
          add(b);
      
        } 
       }
      

      新窗口代码:

      public class newWindow extends JFrame {
      
      newWindow() {
          super("title");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(400,400);
          setVisible(true);
       }  
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-12
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        相关资源
        最近更新 更多