JFrame到Window类

时间:2011-05-11 08:05:08

标签: java swing window jframe

我想知道这行代码是真的还是有更好的方法? 任何人都可以帮助我吗?

JFrame jframe=new JFrame()
Window window;
jframe.setUndecorated(true);


window=(Window)jframe; //is this line true?
谢谢。

2 个答案:

答案 0 :(得分:2)

是的,但是你不需要演员。 java.swing.JFrame是java.awt.Window的子类,所以没关系。而且我找不到应用于Window变量的方法不适用于JFrame变量的原因。它不应该发生,因为Java只使用后期绑定方法调用。

尝试检查您的代码,检查您是否导入了正确的类,因为我认为您误解了某些内容。

答案 1 :(得分:2)

如果你正在使用JFrame,我的建议是你尝试这样的事情。首先调用createAndShowGUI()的主方法:

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
        try {
            createAndShowGUI();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        }
    });
    }

然后创建JFrame结构:

static void createAndShowGUI() throws UnsupportedLookAndFeelException {
    // Creates the window (JFrame)
    frame = new JFrame("Name of the window");//                                    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    new Interface();
    frame.pack();
    frame.setSize(700, 400);
    frame.setLocationRelativeTo(null);// centers the window in the screen
    frame.setVisible(true);
    }

Interface()是我创建的类的构造函数,它使用frame作为主窗口,并在其中添加JPanels,但您可以通过许多其他方式执行。

我想你想要的是展示一个窗口,不是吗?目前尚不清楚的是你是否想要使用Swing组件。