繁体   English   中英

JPanel在其他JPanel中的Java Swing简单中心

[英]Java Swing simple center of JPanel in other JPanel

我有这个非常容易想要一个漂亮的中心任务JPanel另一个里面JPanel 父被设置为900, 550 ,和孩子应该是大约200,400左右。

为此,我想给父母一个BorderLayout ,然后设置孩子的setPreferredSize(200, 400) 这个孩子将在CENTER 两个空的JPanels将位于EASTWEST 当然这没有用。 给两个侧面板一个setPreferredSize()当然可以DID起作用。 问题在于,缩小“框架”会导致中间窗格消失。

这是应该显示问题的一些示例代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Temporary {

    public static Temporary myObj = null;
    private JFrame mainFrame;

    public void go(){
        mainFrame = new JFrame("Swing");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setPreferredSize(new Dimension(900,550));

        JPanel mainCards = new JPanel(new CardLayout());
        mainCards.add(loginLayer(),  "Login");

        mainFrame.setContentPane(mainCards);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
    }

    public JPanel loginLayer(){
        JPanel masterPane = new JPanel(new BorderLayout());

        JPanel centerPane = new JPanel();
        centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));
        centerPane.setPreferredSize(new Dimension(100,200));

        JLabel label = new JLabel("Swing is overly");
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        centerPane.add(label);
        JButton button = new JButton("complicated");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        centerPane.add(button);
        JTextField textField = new JTextField(10);
        centerPane.add(textField);

        JPanel filler = new JPanel();
        JPanel filler2 = new JPanel();

        masterPane.add(filler, BorderLayout.WEST);
        masterPane.add(centerPane, BorderLayout.CENTER);
        masterPane.add(filler2, BorderLayout.EAST);

        return masterPane;
    }

    public static void main(String[] args){
        myObj = new Temporary();
        myObj.go();
    }
}

在Java Swing中,您通常希望避免创建一堆具有首选大小和绝对位置的静态放置的项,因为调整大小会使事情变得怪异(如您所注意到的)。 相反,您希望依赖于流畅的LayoutManager 有一位优秀的教程在这里 或者,如果您想提供某种样机来显示您要创建的实际UI,我可以提供更多反馈。

BorderLayout本质BorderLayoutBorderLayout将为CENTER组件提供尽可能多的可用空间。 这就是它的设计方式。

如果希望组件在父容器中居中,但要保持其首选大小,则应考虑使用GridBagLayout 没有任何其他限制,这应该可以达到您想要的结果

例如...

public JPanel loginLayer(){
    JPanel masterPane = new JPanel(new GridBagLayout);

    JPanel centerPane = new JPanel();
    centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS));

    JLabel label = new JLabel("Swing is overly");
    label.setAlignmentX(Component.CENTER_ALIGNMENT);
    centerPane.add(label);
    JButton button = new JButton("complicated");
    button.setAlignmentX(Component.CENTER_ALIGNMENT);
    centerPane.add(button);
    JTextField textField = new JTextField(10);
    centerPane.add(textField);

    masterPane.add(centerPane);

    // Add additional borders to providing padding around the center pane
    // as you need

    return masterPane;
}

我还将避免以这种方式主动设置组件的首选大小,因为您添加到组件中的组件可能会超出您的期望,相反,请使用EmptyBorder之类的东西(例如)来添加额外的空白组件及其内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM