JSplitPane 忽略对齐
JSplitPane ignoring alignment
你好 StackExchange 社区,
我对这个 JSplitPane 束手无策,我正试图将它放入我的框架中,它位于我框架的右侧,而不是将其填满或至少位于左侧。
如果有人能帮我解决这个问题,我将不胜感激。
见下图我的问题和代码
问题区域是带有 "tab1" 和 "tab2" 的窗格、分隔线以及该分隔线右侧的窗格:
我已经尝试在 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
应占 space 的百分比,它会根据您的 window 调整大小。
你好 StackExchange 社区,
我对这个 JSplitPane 束手无策,我正试图将它放入我的框架中,它位于我框架的右侧,而不是将其填满或至少位于左侧。 如果有人能帮我解决这个问题,我将不胜感激。
见下图我的问题和代码
问题区域是带有 "tab1" 和 "tab2" 的窗格、分隔线以及该分隔线右侧的窗格:
我已经尝试在 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
应占 space 的百分比,它会根据您的 window 调整大小。