如何顶部对齐并排面板,Java 摆动
How top-align side-by-side panels, Java swing
我希望我的不同高度面板在其包含面板中顶部对齐。
我试过使用 NORTH 的 BorderLayout,我试过 panel.setAlignmentY(TOP_ALIGNMENT)。到目前为止,任何方法都没有运气。
(至 post,我被要求提供更多描述。剩余的文本(代码除外)是我尝试满足该要求的尝试。)您可以看到包含“标签”的小面板x" 名称,每个名称垂直堆叠。我绝对希望面板内的组件垂直堆叠,而且我肯定会有不同高度的面板;不要让我把面板做成相同的高度——底部不对齐是我的艺术选择。
package view;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI2 extends JFrame {
private static final long serialVersionUID = 1L;
public GUI2() {
setTitle("Paddle Events");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocation(100, 100);
// nesting enables me to see the border of the panel
getContentPane().add(getMainPanel());
setVisible(true);
}
private Component getMainPanel() {
JPanel panel = new JPanel();
panel.add(getGreenPanel());
panel.add(getDarkGrayPanel());
return panel;
}
private JPanel getGreenPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.green));
panel.setPreferredSize(new Dimension(175, 100));
panel.add(getWrapperA());
panel.add(getWrapperB());
panel.add(getWrapperC());
return panel;
}
private JPanel getDarkGrayPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
panel.setPreferredSize(new Dimension(150, 70));
panel.add(getWrapperA());
panel.add(getWrapperB());
panel.add(getWrapperC());
return panel;
}
private Component getWrapperA() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(new JLabel("label 1"));
panel.add(new JLabel("label 2"));
panel.add(new JLabel("label 3"));
return panel;
}
private Component getWrapperB() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.setAlignmentY(TOP_ALIGNMENT); // pointless
panel.add(new JLabel("label 4"));
panel.add(new JLabel("label 5"));
return panel;
}
private Component getWrapperC() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(new JLabel("label 6"));
return panel;
}
public static void main(String[] args) {
new GUI2();
}
}
可能有不同的方法来实现同样的事情,但对我来说,使用 GridBagLayout
,请参阅 How to Use GridBagLayout 了解更多详情
import java.awt.Color;
import java.awt.Component;
import static java.awt.Component.TOP_ALIGNMENT;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Main() {
setTitle("Paddle Events");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocation(100, 100);
// nesting enables me to see the border of the panel
getContentPane().add(getMainPanel());
setVisible(true);
}
private Component getMainPanel() {
JPanel panel = new JPanel();
panel.add(getGreenPanel());
panel.add(getDarkGrayPanel());
return panel;
}
private JPanel getGreenPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
gbc.anchor = gbc.PAGE_START;
gbc.insets = new Insets(4, 2, 4, 2);
panel.setBorder(BorderFactory.createLineBorder(Color.green));
panel.setPreferredSize(new Dimension(175, 100));
panel.add(getWrapperA(), gbc);
panel.add(getWrapperB(), gbc);
panel.add(getWrapperC(), gbc);
return panel;
}
private JPanel getDarkGrayPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
gbc.anchor = gbc.PAGE_START;
gbc.insets = new Insets(4, 2, 4, 2);
panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
panel.setPreferredSize(new Dimension(150, 70));
panel.add(getWrapperA(), gbc);
panel.add(getWrapperB(), gbc);
panel.add(getWrapperC(), gbc);
return panel;
}
private Component getWrapperA() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(new JLabel("label 1"));
panel.add(new JLabel("label 2"));
panel.add(new JLabel("label 3"));
return panel;
}
private Component getWrapperB() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.setAlignmentY(TOP_ALIGNMENT); // pointless
panel.add(new JLabel("label 4"));
panel.add(new JLabel("label 5"));
return panel;
}
private Component getWrapperC() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(new JLabel("label 6"));
return panel;
}
public static void main(String[] args) {
new Main();
}
}
我希望我的不同高度面板在其包含面板中顶部对齐。
我试过使用 NORTH 的 BorderLayout,我试过 panel.setAlignmentY(TOP_ALIGNMENT)。到目前为止,任何方法都没有运气。
(至 post,我被要求提供更多描述。剩余的文本(代码除外)是我尝试满足该要求的尝试。)您可以看到包含“标签”的小面板x" 名称,每个名称垂直堆叠。我绝对希望面板内的组件垂直堆叠,而且我肯定会有不同高度的面板;不要让我把面板做成相同的高度——底部不对齐是我的艺术选择。
package view;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI2 extends JFrame {
private static final long serialVersionUID = 1L;
public GUI2() {
setTitle("Paddle Events");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocation(100, 100);
// nesting enables me to see the border of the panel
getContentPane().add(getMainPanel());
setVisible(true);
}
private Component getMainPanel() {
JPanel panel = new JPanel();
panel.add(getGreenPanel());
panel.add(getDarkGrayPanel());
return panel;
}
private JPanel getGreenPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.green));
panel.setPreferredSize(new Dimension(175, 100));
panel.add(getWrapperA());
panel.add(getWrapperB());
panel.add(getWrapperC());
return panel;
}
private JPanel getDarkGrayPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
panel.setPreferredSize(new Dimension(150, 70));
panel.add(getWrapperA());
panel.add(getWrapperB());
panel.add(getWrapperC());
return panel;
}
private Component getWrapperA() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(new JLabel("label 1"));
panel.add(new JLabel("label 2"));
panel.add(new JLabel("label 3"));
return panel;
}
private Component getWrapperB() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.setAlignmentY(TOP_ALIGNMENT); // pointless
panel.add(new JLabel("label 4"));
panel.add(new JLabel("label 5"));
return panel;
}
private Component getWrapperC() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(new JLabel("label 6"));
return panel;
}
public static void main(String[] args) {
new GUI2();
}
}
可能有不同的方法来实现同样的事情,但对我来说,使用 GridBagLayout
,请参阅 How to Use GridBagLayout 了解更多详情
import java.awt.Color;
import java.awt.Component;
import static java.awt.Component.TOP_ALIGNMENT;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
public Main() {
setTitle("Paddle Events");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocation(100, 100);
// nesting enables me to see the border of the panel
getContentPane().add(getMainPanel());
setVisible(true);
}
private Component getMainPanel() {
JPanel panel = new JPanel();
panel.add(getGreenPanel());
panel.add(getDarkGrayPanel());
return panel;
}
private JPanel getGreenPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
gbc.anchor = gbc.PAGE_START;
gbc.insets = new Insets(4, 2, 4, 2);
panel.setBorder(BorderFactory.createLineBorder(Color.green));
panel.setPreferredSize(new Dimension(175, 100));
panel.add(getWrapperA(), gbc);
panel.add(getWrapperB(), gbc);
panel.add(getWrapperC(), gbc);
return panel;
}
private JPanel getDarkGrayPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
gbc.anchor = gbc.PAGE_START;
gbc.insets = new Insets(4, 2, 4, 2);
panel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
panel.setPreferredSize(new Dimension(150, 70));
panel.add(getWrapperA(), gbc);
panel.add(getWrapperB(), gbc);
panel.add(getWrapperC(), gbc);
return panel;
}
private Component getWrapperA() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(new JLabel("label 1"));
panel.add(new JLabel("label 2"));
panel.add(new JLabel("label 3"));
return panel;
}
private Component getWrapperB() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.blue));
panel.setAlignmentY(TOP_ALIGNMENT); // pointless
panel.add(new JLabel("label 4"));
panel.add(new JLabel("label 5"));
return panel;
}
private Component getWrapperC() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.add(new JLabel("label 6"));
return panel;
}
public static void main(String[] args) {
new Main();
}
}