3

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

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

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

4

1 回答 1

3

请DYM???

在此处输入图像描述

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();
            }
        });
    }
}
于 2013-05-24T11:59:26.193 回答