gpt4 book ai didi

java - JSplitPane 忽略对齐

转载 作者:行者123 更新时间:2023-12-02 10:00:56 27 4
gpt4 key购买 nike

StackExchange 社区您好,

我对这个 JSplitPane 束手无策,我试图将其放入我的框架中,它位于我框架的右侧,而不是填充它或至少位于左侧。如果有人能帮助我解决这个问题,我将非常感激。

请参阅下面我的问题和代码的图片

问题区域是带有“tab1”和“tab2”的 Pane 、分隔线以及该分隔线右侧的 Pane :

enter image description here

我尝试在 JSplitPane 的所有单独部分上设置 setAlignmentX(Component.LEFT_ALIGNMENT) 。我还尝试制作一个 JPanel 来容纳它并对齐它。一切都无济于事。

此外,我找到的现有信息并不相关,大多数人都在讨论如何对齐 JSplitPane 的内容。

下面的代码是制作这个框架所需的全部代码,如果你们中的任何人需要它来帮助我,请随意。

package test;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

public class FrameMaker {

public int x = 0;
private ArrayList<String> mpLabels;
private JFrame theFrame;

public void MakeFrame() {
theFrame = new JFrame("title");
theFrame.getContentPane().setLayout(new BoxLayout(theFrame.getContentPane(), BoxLayout.Y_AXIS));

mpLabels = new ArrayList<String>();


//label1
JPanel bgSwitches = new JPanel();
JLabel calcsLabel = new JLabel("top label, broadened with lines to exaggerate problem-------------------------------------------");
bgSwitches.add(calcsLabel);


//label2
JPanel topLevel = new JPanel();
JLabel textinfo = new JLabel("label below that");
topLevel.add(textinfo);


//splitpane tabs
mpLabels.add("tab1");
mpLabels.add("tab2");

String[] mpLabelsAr = new String[mpLabels.size()];
JList<String> posL = new JList<String>(mpLabels.toArray(mpLabels.toArray(mpLabelsAr)));
posL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

//panel inside the right splitpane pane, this is needed for something later.
JPanel RPanel = new JPanel();
RPanel.setLayout(new BoxLayout(RPanel, BoxLayout.Y_AXIS));

JScrollPane scrollPos = new JScrollPane(posL);
JScrollPane scrollROI = new JScrollPane(RPanel);

JSplitPane posPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scrollPos,scrollROI);
posPanel.setOneTouchExpandable(false);
posPanel.setDividerLocation(75);


//label and textfield
JLabel msLabel = new JLabel("another label");
JTextField msField = new JTextField("textfield");
JPanel buttonPanel = new JPanel();
buttonPanel.add(msLabel);
buttonPanel.add(msField);

bgSwitches.setBackground(new Color(0,0,255));
theFrame.add(bgSwitches);

topLevel.setBackground(new Color(0,255,0));
theFrame.add(topLevel);

posPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
posPanel.setBackground(new Color(0,255,255));
theFrame.add(posPanel);

buttonPanel.setBackground(new Color(255,0,0));
theFrame.add(buttonPanel);

theFrame.pack();
theFrame.setVisible(true);
}
}

最佳答案

您还需要将所有其他元素向左对齐。

bgSwitches.setAlignmentX(Component.LEFT_ALIGNMENT);
topLevel.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

您还可以添加 posPanel.setResizeWeight(VALUE BETWEEN 0 AND 1); 来指定 JSplitPane 应占用的空间百分比,并且它将随您的窗口调整大小.

关于java - JSplitPane 忽略对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55635801/

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