如何设置使用 gridLayout 的 JPanel 最大或首选尺寸?

发布于 2024-08-21 06:02:37 字数 1301 浏览 9 评论 0原文

我在使用包含两个 JPanel 的非常简单的框架时遇到了问题。 问题出在包含四个 JButton 的 Center JPanel 的布局上。 如何为按钮或直接为使用 GridLayout 的 JPanel 设置更好的尺寸。关于图片的问题:

alt http://img42.imageshack.us/img42/4601 /恐怖.jpg

这里是代码: ` JFrame window = new JFrame("Horrible! LOL");

    JTextField textField = new JTextField("");
    textField.setPreferredSize(new Dimension(200,20));
    JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    textPanel.add(textField);
    window.add(textPanel, BorderLayout.NORTH);

    JButton plus = new JButton("+");
    //plus.setPreferredSize(new Dimension(50,50)); nothing would change
    JButton minus = new JButton("-");
    JButton per = new JButton("x");
    JButton divide = new JButton("/");
    JPanel prova = new JPanel(new GridLayout(2,2,10,10));
    Dimension d = new Dimension(20,20);
    prova.setMaximumSize(d); // nothing changed!

    prova.add(plus);
    prova.add(minus);
    prova.add(per);
    prova.add(divide);
    window.add(prova, BorderLayout.CENTER);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(250,300);
    window.setResizable(false);
    window.setVisible(true);`

哪个是好的解决方案?

亲切的问候

I had problem using a very simple frame containing two JPanel.
The problem is on the layout of the Center JPanel that contains four JButton.
How can I set a better size for buttons or directly for JPanel that uses the GridLayout. On the picture the problem:

alt http://img42.imageshack.us/img42/4601/horrible.jpg
!

Here the code: ` JFrame window = new JFrame("Horrible! LOL");

    JTextField textField = new JTextField("");
    textField.setPreferredSize(new Dimension(200,20));
    JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    textPanel.add(textField);
    window.add(textPanel, BorderLayout.NORTH);

    JButton plus = new JButton("+");
    //plus.setPreferredSize(new Dimension(50,50)); nothing would change
    JButton minus = new JButton("-");
    JButton per = new JButton("x");
    JButton divide = new JButton("/");
    JPanel prova = new JPanel(new GridLayout(2,2,10,10));
    Dimension d = new Dimension(20,20);
    prova.setMaximumSize(d); // nothing changed!

    prova.add(plus);
    prova.add(minus);
    prova.add(per);
    prova.add(divide);
    window.add(prova, BorderLayout.CENTER);

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(250,300);
    window.setResizable(false);
    window.setVisible(true);`

Which is a good solution?

Kind regards

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

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

发布评论

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

评论(2

百思不得你姐 2024-08-28 06:02:37

不幸的是,网格布局不尊重首选尺寸。但如果你想坚持网格布局,那么你可以尝试这样的事情:

    public static JComponent wrap(JComponent comp)
    {
        JPanel panel = new JPanel();
        panel.add(comp);
        return panel;
    }

然后而不是直接添加到 prova 添加这样的:

            prova.add(wrap(plus)); 
            prova.add(wrap(minus)); 
            prova.add(wrap(per)); 
            prova.add(wrap(divide));

经过测试,工作完美!
不过还有其他更好的方法

Unfortunately gridlayout doesent respect preferred sizes. But still if you want to stick to grid layout then you can try something like this:

    public static JComponent wrap(JComponent comp)
    {
        JPanel panel = new JPanel();
        panel.add(comp);
        return panel;
    }

And then instead of direclty adding in to prova add like this:

            prova.add(wrap(plus)); 
            prova.add(wrap(minus)); 
            prova.add(wrap(per)); 
            prova.add(wrap(divide));

Tested, Works perfect!!
There are other better ways though

夜清冷一曲。 2024-08-28 06:02:37

这就是发生在我身上的事情:
它肯定连接到网格的上边缘。

替代文本 http://img96.imageshack.us/img96/9431/stillnot.jpg< /a>

即使在这种情况下,在wrap方法中我可以设置buttons/comp的preferredSize,每个按钮都在它自己的边缘。其他解决方案怎么样。你会如何放置计算器的按钮?
亲切的问候并再次感谢!

That's what happen to me:
It's definitely attached to the upper edge of the grid.

alt text http://img96.imageshack.us/img96/9431/stillnot.jpg

Even if in this case, in the wrap method I can set the preferredSize of buttons/comp, every buttons is on its own edge. What about others solutions. How would you position buttons for a calculator?
Kind regards and thanx angain!

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