【问题标题】:Grid layout not working?网格布局不起作用?
【发布时间】:2015-08-20 06:17:56
【问题描述】:

我正在尝试制作一个 2x2 网格布局,左上角有一个 JLabel,其他三个空格上有三个按钮。当我这样做时,我得到一个大按钮(填满整个 JDialog)的意外结果,上面写着“你想推我吗”。不知道为什么会出现这个结果,请帮忙,谢谢!

    public void sinceyoupressedthecoolbutton() {

        JDialog replacementwindow = new JDialog(); //Like a window
        JButton best = new JButton("best");
        JButton first = new JButton("FIRST");
        JButton second = new JButton("Second");
        replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
        JPanel panel = new JPanel();
        replacementwindow.add(panel); //adding the JPanel itself
        replacementwindow.add(first);
        replacementwindow.add(second);
        replacementwindow.add(best);
        replacementwindow.setSize(500, 500);
        replacementwindow.setTitle("NEW WINDOW!");
        replacementwindow.setVisible(true);

    }

【问题讨论】:

  • 不,您编辑的代码不是我建议的。您将组件添加到“面板”,然后将组件添加到“replacementWindow”。不要将组件直接添加到对话框中。
  • 尝试你的方法后,网格外观丢失了..?当我将组件添加到面板时,它看起来像是多个按钮排列在一起。为什么会这样?
  • it looked like multiple buttons just lined up - 因为您没有将布局管理器设置为 GridLayout。 JPanel 的默认布局管理器是 FlowLayout。最后一个答案,您需要花时间阅读教程以学习一些基础知识。问我一个关于您阅读的内容的问题,我会回答,但我不是在这里教您基础知识,即教程中的示例的用途。您甚至可以查看教程中的 How to Use a GridLayout 演示以获取工作代码。从教程中学习正确的技术。
  • 好的,我非常感谢您为给我详细解释所付出的时间和精力。如果我在阅读您推荐的教程后有任何问题,我一定会告诉您!谢谢你,我喜欢你的回答!

标签: java swing jbutton layout-manager grid-layout


【解决方案1】:

这是因为您设置了 JButton 的布局,而不是 JDialog 的布局

改变

label.setLayout(new GridLayout(2,2,0,0));

YES.setLayout(new GridLayout(2,2,0,0));

另外,您的变量labelJButton,您可能想要更改它。

【讨论】:

  • 非常感谢您,现在我的基本网格开始运行了!不过,我仍然对这一行感到困惑: [code] JPanel frame = new JPanel(); YES.add(frame); //添加框架本身 [code] JPanel 的意义何在?
  • @Billybobsteven I am still kind of confused - 因为你的变量名很糟糕。您的“框架”不是框架,而是 JPanel。大多数人会认为它是一个 JFrame。这就是为什么我给你一些(稍微)更好的变量名来使用。甚至我的名字也应该更具描述性,但我不知道您的应用程序在做什么,所以我无法建议更具描述性的名称。
  • @Billybobsteven 我不会重复 camickr 已经说过的话。但是 JPanel 的“点”是你想要用它做什么......所以你可能想在其中添加 JLabel,对吧?即使您不需要额外的 JPanel...
  • 对于变量名的混淆,我深表歉意,但我已经用更具描述性的变量名编辑了我的问题。你介意向我解释两件事吗? 1) 为什么我们需要将 JPanel 添加到 JDialog,以及 2) 为什么输出对网格中的左上角没有任何作用?
  • @Billybobsteven 1) 在其他答案的 cmets 中几乎得到了回答。 2)你是什么意思?您在左上角添加了一个空白 JPanel,所以显然那里不会显示任何内容。
【解决方案2】:

不要向按钮添加组件。您将组件添加到面板。

所以基本代码应该是:

JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);

另外,变量名不应该以大写字符开头! “是”不遵循 Java 标准。其他变量可以。保持一致!

【讨论】:

  • 这是因为 JPanel 在 JDialog 中吗?如果是这样,如果我将它添加到 JDialog 或 JPanel 中,它不应该以任何一种方式工作吗?
  • 当您将组件添加到“对话框”时,您会将组件添加到作为 JPanel 的对话框的“内容窗格”中。最好使用我上面展示的方法或使用dialog.setContentPane(panel),而不是直接将组件添加到对话框中。阅读Using Top Level Containers 上的 Swing 教程以获取更多信息。本教程讨论了 JFrame,但 JDialog 的结构是相同的。
  • shouldn't it work even either way, if I add it to the JDialog or the JPanel? - 它会起作用,但不是有经验的程序员会这样做。组件属于面板,而不是对话框,因此将组件添加到面板中。 Read the tutorial and download other examples to learn how to better structure your code!
  • 引用我给你的教程链接:This means that if you want to take advantage of the content pane's JComponent features, you need to either typecast the return value or create your own component to be the content pane. Our examples generally take the second approach, since it's a little cleaner.
猜你喜欢
  • 2018-05-30
  • 1970-01-01
  • 2017-11-29
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多