【发布时间】: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