【问题标题】:JButtons cutoff on bottom of window窗口底部的 JButtons 截止
【发布时间】:2018-01-25 21:26:49
【问题描述】:

我一直在学习如何用 Java 制作 GUI,作为作业的一部分,我们需要有两排按钮,一排在另一排。我有所有的按钮,但底部的按钮大约被切断了一半。我无法弄清楚如何让它们充分显示。我已经尝试调整窗口的 Y 大小,但它们仍然保持截止。有谁知道如何让底部的三个按钮完全显示? Image of what's happening

相关代码

JPanel rightA = new JPanel();
JPanel bottomB = new JPanel();
JPanel bottomB2 = new JPanel();
JLabel jlName = new JLabel("Item Name: ");
JLabel jlNum = new JLabel("Number of: ");
JLabel jlCost = new JLabel("Cost: ");
JLabel jlOwed = new JLabel("Amount Owed: ");
JButton jbCalculate = new JButton("Calculate");
JButton jbSave = new JButton("Save");
JButton jbClear = new JButton("Clear");
JButton jbExit = new JButton("Exit");
JButton jbLoad = new JButton("Load");
JButton jbPrev = new JButton("<Prev");
JButton jbNext = new JButton("Next>");
this.setTitle("Items Order Calculator");
this.setMinimumSize(new Dimension(400, 200));
rightA.add(jlName);
rightA.add(jtfName);
rightA.add(jlNum);
rightA.add(jtfNum);
rightA.add(jlCost);
rightA.add(jtfCost);
rightA.add(jlOwed);
rightA.add(jtfOwed);
bottomB.add(jbCalculate);
bottomB.add(jbSave);
bottomB.add(jbClear);
bottomB.add(jbExit);
bottomB2.add(jbLoad);
bottomB2.add(jbPrev);
bottomB2.add(jbNext);
jtfOwed.setEnabled(false);
rightA.setLayout(new GridLayout(4, 2));
this.add(rightA, BorderLayout.EAST);
bottomB.add(bottomB2);
this.add(bottomB, BorderLayout.SOUTH);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);

【问题讨论】:

  • 您是否尝试在设置为可见之前调用this.pack()?
  • 您应该致电rightA.setLayout(...)在向该面板添加组件。
  • @FrancisBartkowiak 当我使用 this.pack() 时,我得到 this as a result 而我需要两行,类似于 this。

标签: java jframe jpanel jbutton


【解决方案1】:

我认为问题在于您将 bottomB2 面板添加到 bottomB 面板中,并且他们没有正确传达他们的首选尺寸。

尝试创建另一个具有边框布局的面板,并将它们添加到该面板的北部和南部区域。然后将这个新的底部面板添加到南的 gui 中。

JPanel bottomAll = new JPanel(new BorderLayout());
bottomAll.add(bottomB, BorderLayout.NORTH);
bottomAll.add(bottomB2, BorderLayout.SOUTH);
this.add(bottomAll, BorderLayout.SOUTH);

【讨论】:

  • 效果很好,谢谢!我什至没有想过这两个面板会相互传达自己的尺寸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多