gpt4 book ai didi

java - 一个 JFrame 中的两个 JPanel

转载 作者:行者123 更新时间:2023-12-01 18:00:06 24 4
gpt4 key购买 nike

我想在一个 JFrame 中使用两个 JPanel,它们之间有一条不可见的水平线。我玩了一下,得到了这个:

public class Application { 

public static void main(String[] args)
{

JFrame jframe = new JFrame();
jframe.setSize(500,700);
jframe.setVisible(true);
jframe.setTitle("Title");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);


JSplitPane splitPane = new JSplitPane();
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerLocation(250);
splitPane.setLeftComponent(leftPanel);
splitPane.setRightComponent(rightPanel);
jframe.add(splitPane);


}
}

现在,第一个问题是如何关闭面板之间线条的“可调整大小”?以及如何让它“不可见”?也许使用分割 Pane 以外的其他东西?

其次,如何才能仅使用 JPanel 的一侧?(我正在开发一个应用程序,可以让您在左侧画一个圆圈)。

这似乎是一个简单的问题,但我对 Java 还比较陌生。

最佳答案

正如之前在comment中所说的那样通过@MadProgrammer,您可以使用 BorderLayoutGridBagLayout但是当您将“分割”线放置在两个面板的中间时,您可以使用 GridLayout无论窗口大小是否调整,这都会使两个面板的大小相同。

我没有尝试使用 GridBagLayout,但我做了一个示例,说明如何在不使用 JSplitPane 的情况下实现此 Pane 分离。

使用 GridLayout,您所需要做的就是在 BorderLayout 中将元素添加到左 Pane (在我的示例中,我使用 JLabel 来区分它们) 您需要指定要在其中绘制圆的面板与我一样向左对齐(WEST 常量)。

但是,如果您使用 BorderLayout 方法并向右侧 Pane 添加文本或元素,它们将向右对齐,您可以通过使用不同的方式“装箱”另一个 Pane 中的元素来修复它。 Layout .

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Application {

private JFrame frame;
private JPanel containerPane;
private JPanel topPane;
private JPanel bottomPane;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Application().createAndShowGui();
}
});
}

public void createAndShowGui() {
frame = new JFrame("Example of 2 panels");
containerPane = new JPanel();
topPane = new JPanel();
bottomPane = new JPanel();

containerPane.setLayout(new GridLayout(2, 1));
topPane.setLayout(new GridLayout(1, 2));
bottomPane.setLayout(new BorderLayout());

topPane.add(new JLabel("Left side"));
topPane.add(new JLabel("Right side"));

bottomPane.add(new JLabel("Left side"), BorderLayout.WEST);
bottomPane.add(new JLabel("Right side"), BorderLayout.EAST);

topPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using GridLayout"));
bottomPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Using BorderLayout"));

containerPane.add(topPane);
containerPane.add(bottomPane);

frame.add(containerPane);

// frame.pack();
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

在此示例中,我没有调用 pack(),因为两个面板(或本例中的 JLabel)的大小不够高,无法显示差异:

使用pack():

enter image description here

调用setSize():

enter image description here

<小时/>

其他提示

  1. 不要忘记将您的程序放在Event Dispatch Thread (EDT)上,我通过在 main 方法上编写这些行来做到这一点:

    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
    new Application().createAndShowGui();
    }
    });
  2. 不要将所有代码都放在构造函数上,否则很难维护

关于java - 一个 JFrame 中的两个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com