我必须制作一个简单的程序,它GUI
看起来像这样:
,,,,,,,,,,
| Read | <---a Button
``````````
name |``````````| <---a Textfield
``````````
|
|
a Label
我的问题是如何在“读取”和文本字段Label
的垂直框中获取“名称”,因为名称和文本字段是水平的?Button
我必须制作一个简单的程序,它GUI
看起来像这样:
,,,,,,,,,,
| Read | <---a Button
``````````
name |``````````| <---a Textfield
``````````
|
|
a Label
我的问题是如何在“读取”和文本字段Label
的垂直框中获取“名称”,因为名称和文本字段是水平的?Button
请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();
}
});
}
}