【问题标题】:How to align JPanel in java如何在java中对齐JPanel
【发布时间】:2014-02-14 03:21:46
【问题描述】:

我有一个 JPanel,它在一个盒子布局中,但我不确定如何将 JPanel 对齐到窗口的中心(即使调整窗口大小也保持居中)我已经尝试寻找解决方案,但所有问题似乎都结束了与我正在寻找的东西相比,它很复杂。

import java.awt.*;
import javax.swing.*;

public class Stacker extends JFrame {
public Stacker() {
    super("Stacker");
    setSize(430, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // create top panel
    JPanel commandPane = new JPanel();
    BoxLayout vertical = new BoxLayout(commandPane,
        BoxLayout.Y_AXIS);
    commandPane.setLayout(vertical);

    JButton subscribe = new JButton("Subscribe");
    JButton unsubscribe = new JButton("Unsubscribe");
    JButton refresh = new JButton("Refresh");
    JButton save = new JButton("Save");

    commandPane.add(subscribe);
    commandPane.add(unsubscribe);
    commandPane.add(refresh);
    commandPane.add(save);

    JMenuItem j1 = new JMenuItem("File");
    JMenuItem j2 = new JMenuItem("Open");
    JMenuItem j3 = new JMenuItem("Close");
    JMenuBar menubar = new JMenuBar();
    JMenu menu = new JMenu("Feeds");
    menu.add(j1);
    menu.add(j2);
    menu.add(j3);
    menubar.add(menu);

    setJMenuBar(menubar);



    // create bottom panel
    /*JPanel textPane = new JPanel();
    JTextArea text = new JTextArea(4, 70);
    JScrollPane scrollPane = new JScrollPane(text);
    // put them together
    FlowLayout flow = new FlowLayout();
    setLayout(flow);
    add(commandPane);
    add(scrollPane); */
    setJMenuBar(menubar);
    add(commandPane);
    setVisible(true);
}

public static void main(String[] arguments) {
    Stacker st = new Stacker();
}
}

【问题讨论】:

    标签: java swing layout centering boxlayout


    【解决方案1】:

    您说您使用的是 BoxLayout,但是带有 BoxLayout 的 JPanel 是您想要居中的 JPanel,还是包含您想要居中的 JPanel?

    如果它包含要居中的 JPanel,则可以在 JPanel 的任一侧添加胶水以居中。如果你要居中的JPanel,那么你可以使用GridBagLayout或者BoxLayout来达到你说的效果。

    谷歌搜索“Java 中心组件”之类的内容会给您带来大量结果。

    【讨论】:

      【解决方案2】:

      对于这个想法(从你的描述中仍然不清楚)使用 GridBagLayout 而不设置 GridBagConstraints

      .

      .

      .

      import java.awt.*;
      import javax.swing.*;
      
      public class CenteredJPanel {
      
          private JFrame frame = new JFrame("Test");
          private JPanel panel = new JPanel();
          private JButton subscribe = new JButton("Subscribe");
          private JButton unsubscribe = new JButton("Unsubscribe");
          private JButton refresh = new JButton("Refresh");
          private JButton save = new JButton("Save");
      
          public CenteredJPanel() {
              panel.setLayout(new GridBagLayout());
              panel.add(subscribe);
              panel.add(unsubscribe);
              panel.add(refresh);
              panel.add(save);
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.add(panel);
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
          }
      
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      CenteredJPanel centeredJLabel = new CenteredJPanel();
                  }
              });
          }
      }
      

      【讨论】:

      • +1 但是 .. panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); .. frame.setSize(400, 300); 为什么不增加边框的大小和 pack() 框架的大小?
      • @Andrew Thompson EmptyBorder == balast , pack() == 我从头开始...,模拟第二次居中。打印屏幕
      • 这太棒了,但是如何让按钮在 y 轴上彼此重叠?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多