开发者

Java: Difficulty with Swing

I'm trying to modify a GUI. It is hosting a GLCanvas displaying JOGL content.

Here is the code to set it up:

private void setupWindow() {
    this.frame = new JFrame(WINDOW_TITLE);
    frame.setSize(width, height);
    frame.setVisible(true);

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

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(listener);
    menu.add(exitItem);
    frame.setJMenuBar(menuBar);
}

Currently, the canvas takes up the entire space in the window, aside from the menu bar. I'd like to make space for other controls in the window, like buttons and list boxes. How can I do it?

I tried inserting the following, but it didn't work:

private void setupWindow() {
    this.frame = new JFrame(WINDOW_TITLE);
    frame.setSize(width, height);

    // ** inserted the following:
    JPanel canvasPanel = new JPanel(new BorderLayout());
    canvasPanel.add(canvas);
    canvasPanel.setSize(30, 40);
    canvasPanel.setVisible(true);
    // **

    frame.add(canvasPanel);
    frame.setVisible(true);

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

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);
    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(listener);
    menu.add(exitItem);
    frame.setJMenuBar(menuBar);
}

This doesn't modify the appearance of the window at all.

What should I be doing here? I'm not too familiar with Java GUIs.

Update: Changing the 开发者_JAVA技巧constructor's argument from BorderLayout to FlowLayout causes the GLCanvas to disappear.


Per Anon's answer, you really need to better understand the layout managers. Your question is too open ended.

In case this helps though:

    JPanel canvasPanel = new JPanel(new BorderLayout());
    frame.add(canvasPanel, BorderLayout.CENTER);

    canvasPanel.add(canvas);


    Box leftBtnsBox = Box.createVerticalBox();
    frame.add(leftBtns, BorderLayout.WEST);

    leftBtns.add(new JButton("Button 1"));
    leftBtns.add(new JButton("Button 2"));

This code sets a BordeLayout explicitly, but I think the default layout manager for panels (and content panes) is BorderLayout. This code will put two buttons to the top left of the canvas panel arranged vertically. Because you are using these layout managers, your second code example's setSize had no effect.


I suggest checking out this guide to layout managers - it will give you enough info to see what you should try.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜