Java居中面板 [英] Centering Panel in Java

查看:286
本文介绍了Java居中面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法将位于另一个面板内部的面板垂直居中.我完全按照我所研究的示例进行操作,但仍然没有运气.

For some reason i am having problems centering my panel vertically that is located inside another panel. I do exactly as the examples i studied but still no luck.

下面是我的代码.尽管在我的容器面板上使用了setAlignmentY(0.5f),但是当我调整窗口大小时,它仍然不会居中.

Down there is my code. Despite using setAlignmentY(0.5f) on my container panel, it still wont center when i resize the window.

尽管setAligenmentX(0.5f),容器面板内的组件也不会居中.

Also the components inside container panel wont center either, despite setAligenmentX(0.5f).

我想知道是否有解决方案,我几乎尝试了所有方法,但是找不到解决方案.

I wonder if there is a solution for this, I pretty much tried everything out there but couldnt find a solution.

JLabel idLabel;
    JLabel passLabel;
    JTextField id;
    JTextField pass;
    JButton enter;
    JPanel container;

    public JournalLogin()
    { 
        //setLayout(new FlowLayout());
        //setPreferredSize(new Dimension(500, 500));
        //setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));


        container = new JPanel();
        container.setLayout(new MigLayout());
        container.setAlignmentX(0.5f);
        container.setAlignmentY(0.5f);
        container.setPreferredSize(new Dimension(300, 300));
        container.setBorder(BorderFactory.createTitledBorder("Login"));
        add(container);

        idLabel = new JLabel("ID:");
        idLabel.setAlignmentX(0.5f);
        container.add(idLabel);

        id = new JTextField();
        id.setText("id");
        id.setAlignmentX(0.5f);
        id.setPreferredSize(new Dimension(80, 20));
        container.add(id, "wrap"); 

推荐答案

setAlignmentX和Y不是执行此操作的方法.在容器中居中组件的一种方法是让容器使用GridBagLayout并在不使用任何GridBagConstraints的情况下添加组件,这就是所谓的默认添加.还有其他方法.

setAlignmentX and Y are not the way to go about doing this. One way to center a component in a container is to have the container use GridBagLayout and to add the component without using any GridBagConstraints, a so-called default addition. There are other ways as well.

例如,更改尼克·里普(Nick Rippe)的榜样(对他而言为1 +):

For example to alter Nick Rippe's example (1+ to him):

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;

import javax.swing.*;

public class UpdatePane2 extends JPanel {
   private static final int PREF_W = 300;
   private static final int PREF_H = 200;

   public UpdatePane2() {
      JPanel innerPanel = new JPanel();
      innerPanel.setLayout(new BorderLayout());
      innerPanel.add(new JLabel("Hi Mom", SwingConstants.CENTER), 
            BorderLayout.NORTH);
      innerPanel.add(new JButton("Click Me"), BorderLayout.CENTER);

      setLayout(new GridBagLayout());
      add(innerPanel);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("UpdatePane2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new UpdatePane2());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

这篇关于Java居中面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆