【问题标题】:How to center JPanel when JFrame is resized or maximized?调整JFrame大小或最大化时如何使JPanel居中?
【发布时间】:2014-01-11 04:31:08
【问题描述】:

当我最大化或重新调整JFrame 的大小时,我希望我的JPanel 仍位于中心。我该怎么做?

试过了:

  jframe.add(panel, BorderLayout.CENTER);
     panel.setAlignmentX(JComponent.CENTER_ALIGNMENT);

但它不起作用。

这是我的其他代码:

public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    login frame = new login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public login() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        setVisible(true);
        setBounds(5, 5, 409, 267);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(0, 128, 128));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
            contentPane.setLayout(null);

        panel = new JPanel();
        panel.setBounds(57, 42, 292, 167);
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 255)));
        panel.setLayout(null);
        contentPane.add(panel);

        textField = new JTextField();
        textField.setBounds(71, 35, 192, 26);
        panel.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(71, 72, 192, 26);
        panel.add(passwordField);

        JLabel lblNewLabel = new JLabel("Username:");
        lblNewLabel.setBounds(6, 41, 68, 14);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Password:");
        lblNewLabel_1.setBounds(6, 78, 68, 14);
        panel.add(lblNewLabel_1);

        JButton btnNewButton = new JButton("Login");
        btnNewButton.setBounds(71, 120, 89, 23);
        panel.add(btnNewButton);



        JButton btnNewButton_1 = new JButton("Cancel");
        btnNewButton_1.setBounds(174, 120, 89, 23);
        panel.add(btnNewButton_1);

我该怎么做呢?

【问题讨论】:

  • 1) Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,请发布MCVE(要成为 MCVE,该代码需要类声明和导入)。

标签: java swing jframe jpanel


【解决方案1】:

您可以简单地在父容器上使用GridBagLayout。它将所有组件放置在容器的中心周围。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class CenterComponent {

    public static void main(String[] args) {
        new CenterComponent();
    }

    public CenterComponent() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel content = new JPanel(new GridBagLayout());
                content.setBackground(Color.GREEN);
                content.setBorder(new EmptyBorder(20, 20, 20, 20));
                frame.setContentPane(content);
                frame.add(new LoginPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class LoginPane extends JPanel {

        public LoginPane() {
            setLayout(new GridBagLayout());
            setBorder(new TitledBorder("Login"));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            add(new JLabel("Username:"), gbc);
            gbc.gridy++;
            add(new JLabel("Password:"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            gbc.gridwidth = 1;
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            add(new JButton("Login"), gbc);
            gbc.gridx++;
            add(new JButton("Cancel"), gbc);
        }

    }

}

【讨论】:

  • 您的代码运行良好。我的项目似乎无法做到这一点。
  • 父容器使用什么布局管理器? LoginPane 是否使用自己的布局管理器...
  • 布局管理器是绝对布局。再看我的帖子。我发布了我的代码。那我该怎么做呢?
  • 那是你的问题。您放置登录对话框的容器应该使用GridBagLayout,否则您将需要手动进行所有重新定位
  • 当我最大化它时,我怎样才能使它更大并且仍然在中心?因为面板太小了。
【解决方案2】:

不要使用空布局。您可以使用 BoxLayout 或 GridLayout 执行此操作,具体取决于您想要的行为。查看教程了解更多信息:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

【讨论】:

  • @MadProgrammer 当我最大化 jframe 的大小时它不会居中
  • @Rohan21 是的,确实如此。 GridBagLayout 使用容器的中心来定位子组件。请参阅我的答案以进行演示...
【解决方案3】:

替换

panel.setLayout(null);

panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

panel.setLayout(new GridLayout(3,1, 5, 5));

【讨论】:

  • 这不会使组件居中。呃,让它填满可用空间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 2012-09-01
相关资源
最近更新 更多