Java GUI布局 [英] Java gui layout
问题描述
如何在基于列的Java中创建gui布局?我的意思是:
How can I make a gui layout in java which is column based? by that i mean:
JLabel
JTextField
JLabel
JTextField
JLabel
JTextField
JLabel
JTextField
JLabel
JTextField
JLabel
JTextField
我希望它们全部堆叠在一起,而不是并排放置.在过去,我使用了不合适的FlowLayout和gridLayout,这使每个组件都比我需要的大得多.有什么想法吗?
I want them all stacked on to of each other, rather than side by side. In the past I've used FlowLayout which isn't suitable, and gridLayout which makes each component much larger than I require. Any ideas?
推荐答案
查看用户友好的布局管理器.
Look at BoxLayout, but more important, go through the layout manager tutorial to get an overview of all the user-friendly layout managers.
另外,GridLayout有时在这种情况下也能很好地工作,但是您可能希望将JTextFields放置在JPanels内,并将JPanels添加到网格中,以使JTextField看起来不大.
Edit 1:
Also, GridLayout sometimes works well in this situation, but you may want to place your JTextFields inside of JPanels and add the JPanels to the grid, so that the JTextFields aren't huge looking.
如果您有一堆带有关联的JLabel的JTextField,我从表示JLabel文本的String数组中工作,然后将JTextFields放入 Map< String,JTextField>
这样我就可以基于它的相关String轻松获得对JTextField的引用.示例如下...
Edit 2:
If you have a bunch of JTextFields with associated JLabels, I've had success and fun working from an array of String that represents the JLabel texts, and then placing the JTextFields into a Map<String, JTextField>
so that I can easily get a reference to the JTextField based on it's related String. Example to follow...
编辑3
如所承诺的,示例:
Edit 3
as promised, the example:
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class LabelsAndFields {
public static final String[] LABEL_TEXTS = {
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
private JPanel mainPanel = new JPanel();
private static final int FIELD_COLS = 10;
private Map<String, JTextField> textFieldMap = new HashMap<String, JTextField>();
public LabelsAndFields() {
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
for (String labelText : LABEL_TEXTS) {
JTextField textField = new JTextField(FIELD_COLS);
textFieldMap.put(labelText, textField);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(labelText), BorderLayout.NORTH);
panel.add(textField);
int gap = 8;
panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.add(panel);
}
}
public String getText(String labelTextKey) {
JTextField field = textFieldMap.get(labelTextKey);
if (field == null) {
throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
}
return field.getText();
}
public void setText(String labelTextKey, String text) {
JTextField field = textFieldMap.get(labelTextKey);
if (field == null) {
throw new IllegalArgumentException(labelTextKey + "is not a valid textFieldMap Key");
}
field.setText(text);
}
public JPanel getMainPanel() {
return mainPanel;
}
private static void createAndShowUI() {
final LabelsAndFields labelsAndFields = new LabelsAndFields();
JButton showAllTextBtn = new JButton(new AbstractAction("Show All Text") {
@Override
public void actionPerformed(ActionEvent arg0) {
for (String labelText : LabelsAndFields.LABEL_TEXTS) {
System.out.printf("%10s: %s%n", labelText, labelsAndFields.getText(labelText));
}
}
});
JFrame frame = new JFrame("LabelsAndFields");
frame.getContentPane().add(labelsAndFields.getMainPanel(), BorderLayout.CENTER);
frame.getContentPane().add(showAllTextBtn, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
这篇关于Java GUI布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!