开发者

JFrame to Window class

i want to know which this line code it really true or is there a better way? can any one help me?

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


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

thank 开发者_如何学Goyou.


Yes it's true but you don't need the cast. java.swing.JFrame is a child class of java.awt.Window so it's ok. And I can't find a reason why a method applied to your Window variable wouldn't apply to a JFrame variable. It's not supposed to happen as Java only uses late binding for method calls.

Try to review your code, to check if you import the right classes, because I think you're misunderstanding something.


If you are using JFrame, my suggestion is that you try something like this. First a main method that calls createAndShowGUI():

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

Then you create JFrame structure:

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() is a constructor of a class I created, that uses frame as main window, and adds JPanels inside it, but you can do in many other ways.

I guess that what you want is to show a window, don't you? What it is not clear is if you want to use Swing components.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜