- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 BorderLayout() 创建一个程序,我希望它看起来像这样(但具有均匀的左右高度等)
尽管我在调整两个 JPanel(大框内的两个框)的大小时遇到了问题。目前我的 GUI 看起来像这样,
我相信这是由于 CENTER 仍然存在,我查看了如何删除它但无法让它工作,
问题
我可以编辑它使其看起来像顶部图像吗。
package fuelstation;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Fuelstation extends JFrame {
JButton btn1 = new JButton("Random Button");
public Fuelstation() {
JFrame frame = new JFrame("Fuel Station");
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setPreferredSize(new Dimension(500,350));
frame.setMaximumSize(new Dimension(500,350));
// Left Hand Side
JPanel lhs = new JPanel();
JTextArea tf_list = new JTextArea();
tf_list.setEditable(false);
tf_list.setWrapStyleWord(true);
tf_list.setText("This is a list of items");
lhs.add(tf_list);
tf_list.setSize(245, 325);
lhs.setBorder(BorderFactory.createTitledBorder("Left"));
// Left Hand Side End
// Right Hand Side
JPanel rhs = new JPanel();
rhs.setAlignmentX(Component.CENTER_ALIGNMENT);
rhs.setBorder(BorderFactory.createTitledBorder("Right"));
rhs.add(btn1);
tf_list.setSize(245, 325);
JPanel center = new JPanel();
center.setSize(0, 0);
// Right Hand Side End
frame.add(lhs, BorderLayout.WEST);
frame.add(center, BorderLayout.CENTER);
frame.add(rhs, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Fuelstation gui = new Fuelstation();
}
}
最佳答案
您需要放弃使用 BorderLayout
的要求。使用 BorderLayout
时组件的大小调整策略在 class javadoc 中说明。
The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.
通过强制您的 JFrame
达到特定大小
frame.setResizable(false);
frame.setPreferredSize(new Dimension(500,350));
frame.setMaximumSize(new Dimension(500,350));
您的中心组件将占用额外的宽度,因为 EAST
和 WEST
组件只会垂直拉伸(stretch)。
所以你需要使用另一个LayoutManager
。您可以使用 Visual guide to layout managers了解可用的 LayoutManager
及其功能。该文档指出 GridLayout
是一个不错的选择:
GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns
关于java - 使用 BorderLayout 创建两个偶数列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553164/
来自 BorderLayout javadoc: A border layout lays out a container, arranging and resizing its components
我试图在 BorderLayout 的中心添加一个 GridBagLayout,但是当我将它添加到中心时,它没有出现。 尽管如此,如果我将它放在 BorderLayout 中的另一个位置,它会正确显示
我想制作一个带有 BorderLayout 的程序,其中中心和东部在图形上是分开的。我尝试了以下操作,但不幸的是分隔符位于 JFrame 的左侧,而不是在 centerPanel 和 rightPan
我试图将两个 JButton 彼此相邻放置在 JFrame 的中心,当 JFrame 已重新调整大小。 为此,我将两个按钮放在一个带有 FlowLayout 的面板中,然后将其放置在一个带有中心 Bo
我正在尝试设置披萨订单表单的框架,但在使用 orderPanel 和 ButtonPanel 时遇到问题。我可以让其中之一出现,但不能同时出现?在我发布的当前代码中,显示了按钮,但未显示文本框/ord
我正在从事一个计算机科学项目,我遇到了一些边框布局问题:这是我的配置(简化) public class MainPanel extends JPanel{ JSplitPane Pcenter
我决定编写一个小型 Java 程序来试验 BorderLayout,因为我正在开发一个 Java 游戏,我需要同时将 2 个对象放置在一个 JFrame 中,我问的每个人都说我需要 BorderLay
我一直在尝试在这个的死点制作一个 2x2 网格,但我做不到。网格要么会覆盖其余部分,要么会适合,但我只能看到最后一个按钮。 import javax.swing.*; import java.awt.
我正在编写一个程序,该程序旨在使用在窗口的西侧和东侧有两个按钮的 BorderLayout。不知怎的,中心有一个很大的差距。有什么方法可以消除这个间隙并使两个按钮彼此相切吗?下面我附上了我的代码。如有
我正在制作一个标题栏,并将标题放在左侧,将两个按钮放在右侧。但如果我对两个按钮都使用 BorderLayout.EAST,则只会显示最后一个按钮。我可以让两者都可见吗?我尝试使用 JPanel,但它会
我分别在 MouseEnter 和 MouseExit 上添加和删除 JButton。这工作正常,但是当按钮添加到面板时,它出现在右上角,而不是指定的 BorderLayout.SOUTH 位置。 框
我正在尝试创建一个可以在其中绘画的 Canvas ,以及一个将 Canvas 重置为空状态的按钮。然而,使用 BorderLayout 我的按钮会被复制,第二个按钮是第一个按钮的图像副本,但看起来仍然
我很难弄清楚为什么这个边框布局仍然像流布局一样...... 我先尝试了这个: JPanel canvas = new JPanel(new BorderLayout()); add(canvas);
我是 Java GUI 的新手,目前正在致力于创建微波 GUI。我正在尝试将面板放置在面板中,但它并没有真正按照我认为的那样工作。我正在使用 BorderLayout,基本上将一个面板放入 Borde
我尝试做以下事情: 使用 BorderLayout 将组件添加到 JFrame 向 JFrame 添加另一个组件 我希望新组件能够“覆盖”旧组件,因为我使用的是 BorderLayout。如果我在调用
我有如下代码: public static void main(String[] args) { JFrame f = new BorderTest(); JPanel p = new
据我了解,将组件添加到 BorderLayout 时,它们会占据整个隔间的大小。将 panel1 添加到 contentPane 的北部时,似乎是下面的情况。但是,当我随后将 panel2 放在 pa
我在使用 BorderLayout 时遇到问题,但首先,这是我的 GUI 设置: 如您所见,我的 JFrame 中有 3 个不同的组件。添加 JMenu 和 JList 效果很好。但是我的 JPane
我的问题是: 如何将 JPanel 添加到东南部的 BorderLayout? 这是我的按钮组: buttonsPanel = new JPanel(); buttonsPanel.setLayout
我想要一个 JFrame,在左侧和右侧有一个黑色边框,宽度为 withfOfJFrame/10。 现在,我的尝试看起来像这样: JFrame f = new JFrame(); f.setSize(8
我是一名优秀的程序员,十分优秀!