Java Swing 第二个 JFrame 布局填充空间?

发布于 2024-12-11 06:24:12 字数 4846 浏览 0 评论 0原文

我一直在学习手写 Swing,并且一直在使用 Sun Doc 教程。我正在使用 GridLayout,第一次使用时它使所有按钮填充可用空间。在我的第二次尝试中,我实现了第二个网格来在顶部保留一些控件。我确信这确实很简单,但在阅读和玩了很长时间之后,我无法像我第一次尝试那样让第二个下部窗格充满按钮。请帮忙?

package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JFrame {
    GridLayout gameLayout = new GridLayout(6,6);
    JLabel score = new JLabel("Score:");

    public Game(String name) {
        super(name);
        setResizable(true);
    }

    public void addComponentsToPane(final Container pane) {
        final JPanel mainGameWindow = new JPanel();
        mainGameWindow.setLayout(gameLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(2,4));

        //Add buttons to experiment with Grid Layout
        mainGameWindow.add(new JButton("sqrOne1"));
        mainGameWindow.add(new JButton("sqrOne2"));
        mainGameWindow.add(new JButton("sqrOne3"));
        mainGameWindow.add(new JButton("sqrOne4"));
        mainGameWindow.add(new JButton("sqrOne5"));
        mainGameWindow.add(new JButton("sqrOne6")); 
        mainGameWindow.add(new JButton("sqrTwo1"));
        mainGameWindow.add(new JButton("sqrTwo2"));
        mainGameWindow.add(new JButton("sqrTwo3"));
        mainGameWindow.add(new JButton("sqrTwo4"));
        mainGameWindow.add(new JButton("sqrTwo5"));
        mainGameWindow.add(new JButton("sqrTwo6")); 
        mainGameWindow.add(new JButton("sqrThree1"));
        mainGameWindow.add(new JButton("sqrThree2"));
        mainGameWindow.add(new JButton("sqrThree3"));
        mainGameWindow.add(new JButton("sqrThree4"));
        mainGameWindow.add(new JButton("sqrThree5"));
        mainGameWindow.add(new JButton("sqrThree6"));   
        mainGameWindow.add(new JButton("sqrFour1"));
        mainGameWindow.add(new JButton("sqrFour2"));
        mainGameWindow.add(new JButton("sqrFour3"));
        mainGameWindow.add(new JButton("sqrFour4"));
        mainGameWindow.add(new JButton("sqrFour5"));
        mainGameWindow.add(new JButton("sqrFour6"));    
        mainGameWindow.add(new JButton("sqrFive1"));
        mainGameWindow.add(new JButton("sqrFive2"));
        mainGameWindow.add(new JButton("sqrFive3"));
        mainGameWindow.add(new JButton("sqrFive4"));
        mainGameWindow.add(new JButton("sqrFive5"));
        mainGameWindow.add(new JButton("sqrFive6"));        
        mainGameWindow.add(new JButton("sqrSix1"));
        mainGameWindow.add(new JButton("sqrSix2"));
        mainGameWindow.add(new JButton("sqrSix3"));
        mainGameWindow.add(new JButton("sqrSix4"));
        mainGameWindow.add(new JButton("sqrSix5"));
        mainGameWindow.add(new JButton("sqrSix6"));

        //Add controls to set up horizontal and vertical gaps
        controls.add(new Label("Match pairs of Promient Tech Heads!"));
        controls.add(score);
        controls.add(new JButton("Solve"));
        controls.add(new JButton("Scrabble")); 

        //Process the Apply gaps button press    
        gameLayout.layoutContainer(mainGameWindow);

        pane.add(controls, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);        
        pane.add(mainGameWindow, BorderLayout.SOUTH);
    }

    private void setLabelText(String text)
    {
        score.setText(text);
        score.paintImmediately(score.getVisibleRect());
    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        Game frame = new Game("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setSize(600,600);
        frame.setMinimumSize(new Dimension(400, 400));            
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I've been learning to hand write Swing, and have been using the Sun Doc tutorials. I am using a GridLayout and on my first go it made all the buttons fill the available space. On my second go I've implemented a second grid to hold some controls at the top. I'm sure this is something really easy but after reading and playing for ages I can't get the second lower pane to fill with the buttons like it did on my first attempt. Help pls?

package layout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Game extends JFrame {
    GridLayout gameLayout = new GridLayout(6,6);
    JLabel score = new JLabel("Score:");

    public Game(String name) {
        super(name);
        setResizable(true);
    }

    public void addComponentsToPane(final Container pane) {
        final JPanel mainGameWindow = new JPanel();
        mainGameWindow.setLayout(gameLayout);
        JPanel controls = new JPanel();
        controls.setLayout(new GridLayout(2,4));

        //Add buttons to experiment with Grid Layout
        mainGameWindow.add(new JButton("sqrOne1"));
        mainGameWindow.add(new JButton("sqrOne2"));
        mainGameWindow.add(new JButton("sqrOne3"));
        mainGameWindow.add(new JButton("sqrOne4"));
        mainGameWindow.add(new JButton("sqrOne5"));
        mainGameWindow.add(new JButton("sqrOne6")); 
        mainGameWindow.add(new JButton("sqrTwo1"));
        mainGameWindow.add(new JButton("sqrTwo2"));
        mainGameWindow.add(new JButton("sqrTwo3"));
        mainGameWindow.add(new JButton("sqrTwo4"));
        mainGameWindow.add(new JButton("sqrTwo5"));
        mainGameWindow.add(new JButton("sqrTwo6")); 
        mainGameWindow.add(new JButton("sqrThree1"));
        mainGameWindow.add(new JButton("sqrThree2"));
        mainGameWindow.add(new JButton("sqrThree3"));
        mainGameWindow.add(new JButton("sqrThree4"));
        mainGameWindow.add(new JButton("sqrThree5"));
        mainGameWindow.add(new JButton("sqrThree6"));   
        mainGameWindow.add(new JButton("sqrFour1"));
        mainGameWindow.add(new JButton("sqrFour2"));
        mainGameWindow.add(new JButton("sqrFour3"));
        mainGameWindow.add(new JButton("sqrFour4"));
        mainGameWindow.add(new JButton("sqrFour5"));
        mainGameWindow.add(new JButton("sqrFour6"));    
        mainGameWindow.add(new JButton("sqrFive1"));
        mainGameWindow.add(new JButton("sqrFive2"));
        mainGameWindow.add(new JButton("sqrFive3"));
        mainGameWindow.add(new JButton("sqrFive4"));
        mainGameWindow.add(new JButton("sqrFive5"));
        mainGameWindow.add(new JButton("sqrFive6"));        
        mainGameWindow.add(new JButton("sqrSix1"));
        mainGameWindow.add(new JButton("sqrSix2"));
        mainGameWindow.add(new JButton("sqrSix3"));
        mainGameWindow.add(new JButton("sqrSix4"));
        mainGameWindow.add(new JButton("sqrSix5"));
        mainGameWindow.add(new JButton("sqrSix6"));

        //Add controls to set up horizontal and vertical gaps
        controls.add(new Label("Match pairs of Promient Tech Heads!"));
        controls.add(score);
        controls.add(new JButton("Solve"));
        controls.add(new JButton("Scrabble")); 

        //Process the Apply gaps button press    
        gameLayout.layoutContainer(mainGameWindow);

        pane.add(controls, BorderLayout.NORTH);
        pane.add(new JSeparator(), BorderLayout.CENTER);        
        pane.add(mainGameWindow, BorderLayout.SOUTH);
    }

    private void setLabelText(String text)
    {
        score.setText(text);
        score.paintImmediately(score.getVisibleRect());
    }


    private static void createAndShowGUI() {
        //Create and set up the window.
        Game frame = new Game("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        frame.addComponentsToPane(frame.getContentPane());
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setSize(600,600);
        frame.setMinimumSize(new Dimension(400, 400));            
    }

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-18 06:24:12

不阅读您的问题,仅阅读代码,要运行此代码,您必须

GridBagLayoutDemo frame = new GridBagLayoutDemo("GridLayoutDemo");

更改为

Game frame = new Game("GridLayoutDemo");

编辑

通过调用方法 pack(); 1);您正确设置了 JComponents 的大小,

2) 然后您正确设置了 setVisible(true);

随后 setSize() 的代码在 SOUTH 区域 上创建了间隙,

接近正确的位置应该是

private static void createAndShowGUI() {
    Game frame = new Game("Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addComponentsToPane(frame.getContentPane());
    frame.setMinimumSize(new Dimension(400, 400));
    frame.pack();
    frame.setVisible(true);
}

not read your question, only the code, for running this code you have to change

GridBagLayoutDemo frame = new GridBagLayoutDemo("GridLayoutDemo");

to

Game frame = new Game("GridLayoutDemo");

EDIT

1) by invoke method pack(); you set size for JComponents correctly,

2) then you correctly to setVisible(true);

but

then code for setSize() created gap on the SOUTH area,

near to the correct should be

private static void createAndShowGUI() {
    Game frame = new Game("Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addComponentsToPane(frame.getContentPane());
    frame.setMinimumSize(new Dimension(400, 400));
    frame.pack();
    frame.setVisible(true);
}
通知家属抬走 2024-12-18 06:24:12

我想我为未来的读者发现了问题,按钮需要声明为:

JButton btnOne1 = new JButton("btnOne1");
btnOne1.setPreferredSize(new Dimension(100, 100));

然后

mainGameWindow.add(btnOne1);

这会使它们填充它们所在的窗口。

I think I found the problem for future readers, the buttons need to be declared as:

JButton btnOne1 = new JButton("btnOne1");
btnOne1.setPreferredSize(new Dimension(100, 100));

and then

mainGameWindow.add(btnOne1);

This then makes them fill the window they are in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文