我可以在Java BoxLayout中混合水平和垂直框吗?

时间:2013-05-24 11:40:18

标签: java swing awt layout-manager boxlayout

我必须制作一个简单的程序,GUI看起来像这样:

      ,,,,,,,,,,
     | Read    |     <---a Button
      `````````` 
name |``````````|   <---a Textfield
      ``````````
 |
 |
 a Label

我的问题是如何在Label'阅读'和文本字段`的垂直框中获取Button'名称',因为名称和文本字段是水平的?

1 个答案:

答案 0 :(得分:3)

请DYM ???

enter image description here

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

public class BoxStructAndJComponents {

    private JLabel intro = new JLabel("The chosen name:");
    private JFrame frame;    
    private JLabel name = new JLabel("JLabel");

    public BoxStructAndJComponents() {
        frame = new JFrame("JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = createUI();
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public JPanel createUI() {
        intro = new JLabel("The chosen name:");
        intro.setLabelFor(name);
        final JButton button = new JButton("Pick a new name...");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
        intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        button.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(intro);
        panel.add(Box.createVerticalStrut(5));
        panel.add(name);
        panel.add(Box.createRigidArea(new Dimension(150, 10)));
        panel.add(button);
        return panel;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                BoxStructAndJComponents bb = new BoxStructAndJComponents();
            }
        });
    }
}