如何在Swing中堆叠标签?

时间:2015-10-16 00:50:12

标签: java swing jpanel layout-manager

我正在编写我的第一个Swing应用程序,但在代码中堆叠标签时遇到了一些问题。

我现在有以下内容

First Swing app

我想“输入回购的名称以及该回购的”高于“所有者的名称以搜索未解决的问题。”所以窗户不是那么宽。

这是我的代码:

public class MainFrame extends JFrame {

    private Boolean submitted = false;

    public MainFrame(String title) {
        super(title);


        // Set layout manager
        setLayout(new BorderLayout());

        // Create components
        JPanel panOuter = new JPanel(new BorderLayout());
        JPanel panLeft = new JPanel(new BorderLayout());

        panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JPanel panRight = new JPanel(new BorderLayout());
        panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JPanel panBottom = new JPanel();
        panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JPanel panTop = new JPanel();
        panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JPanel panTopTop = new JPanel();
        panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        JPanel panTopBottom = new JPanel();
        panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // Add components to content panel
        panOuter.add(panLeft, BorderLayout.WEST);
        panOuter.add(panRight, BorderLayout.EAST);
        panOuter.add(panBottom, BorderLayout.SOUTH);
        panOuter.add(panTop, BorderLayout.NORTH);


        JLabel lblTop1 = new JLabel("Enter the name of the repo and the name of the\n", JLabel.CENTER);
        JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER);
        JLabel lblLeft = new JLabel("Repo", JLabel.CENTER);
        JLabel lblRight = new JLabel("Owner", JLabel.CENTER);

        JTextField txtLeft = new JTextField("Hello", 10);
        JTextField txtRight = new JTextField("World", 10);

        JButton btnBottom = new JButton("Submit!");

        panLeft.add(lblLeft, BorderLayout.NORTH);
        panLeft.add(txtLeft, BorderLayout.CENTER);

        panRight.add(lblRight, BorderLayout.NORTH);
        panRight.add(txtRight, BorderLayout.CENTER);

        panBottom.add(btnBottom);
        panTopTop.add(lblTop1);
        panTopBottom.add(lblTop2);
        panTop.add(panTopTop, BorderLayout.NORTH);
        panTop.add(panTopBottom, BorderLayout.SOUTH);

        this.setContentPane(panOuter);
        this.pack();

        btnBottom.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(!submitted)
                    btnBottom.setText(txtLeft.getText());
                else 
                    btnBottom.setText(txtRight.getText());
                submitted = !submitted;
            }

        });
    }

}

我尝试制作一个标签为NORTHSOUTH组件的面板,但它不起作用。

有人有建议吗?

谢谢, erip

1 个答案:

答案 0 :(得分:4)

您可以尝试使用GridBagLayout ...

JPanel panTop = new JPanel(new GridBagLayout());
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//JPanel panTopTop = new JPanel();
//panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
//JPanel panTopBottom = new JPanel();
//panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

//...

//panTopTop.add(lblTop1);
//panTopBottom.add(lblTop2);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTop.add(lblTop1, gbc);
panTop.add(lblTop2, gbc);
//panTop.add(panTopBottom, BorderLayout.SOUTH);

GridBagLayout

有关详细信息,请参阅How to Use GridBagLayout

现在,你可以偷偷摸摸地使用JTextArea ......

TextArea

JTextArea ta = new JTextArea(1, 20);
ta.setText("Enter the name of the repo and the name of the owner of that repo to search for open issues.");
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setBorder(null);
ta.setFont(UIManager.getFont("Label.font"));
ta.setOpaque(false);
ta.setFocusable(false);
ta.setEditable(false);

//JLabel lblTop1 = new JLabel("<html>Enter the name of the repo and the name of the owner of that repo to search for open issues", JLabel.CENTER);
//JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER);
//...
//panTopTop.add(lblTop1);
//panTopBottom.add(lblTop2);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panTop.add(ta, gbc);

甚至只是使用Swing的HTML支持......

HTML layout

JLabel lblTop1 = new JLabel("<html><p align='center'>Enter the name of the repo and the name of the owner of that repo to search for open issues</p>", JLabel.CENTER);
panOuter.add(lblTop1, BorderLayout.NORTH);