[英]Java Swing BoxLayout
I created a Form with Y.AXIS ordered Boxes. 我用Y.AXIS订购盒创建了一个表格。 Every box contains a JLabel and a JList.
每个框都包含一个JLabel和一个JList。 This boxes contents are X.Axis ordered.
此框的内容是X.Axis排序的。 (picture)
(图片)
Here is the code I'm working with. 这是我正在使用的代码。 createCustomJList is returning a JList
createCustomJList返回一个JList
String ean = (String) table.getModel().getValueAt(selection[0], 0);
JPanel addinfo= new JPanel();
String[] operations=new String[{"ROHTABAK","HERSTELLER","WARENGRUPPE","MARKENLOGO"};
Box moreInfo[] = new Box[4];
for(int i=0;i<operations.length;i++){
moreInfo[i] = Box.createHorizontalBox();
moreInfo[i].add(new JLabel(operations[i]));
moreInfo[i].add(createCustomJList(database.customgetter(operations[i],ean)));
addinfo.add(moreInfo[i]);
}
BoxLayout layout = new BoxLayout(addinfo, BoxLayout.Y_AXIS);
addinfo.setLayout(layout);
JOptionPane.showMessageDialog(null,
addinfo, "Naehere Infos",
JOptionPane.OK_CANCEL_OPTION);
EDIT: I tried the solution with the gridlayout, but used JLists instead. 编辑:我尝试使用gridlayout解决方案,但改用JLists。
Is there a way to have something like a black border around a jlist? 有没有办法让jlist周围有黑色边框?
SOLVEDEDIT: list.setBorder(new LineBorder(Color.darkGray, 1)); 已解决:list.setBorder(new LineBorder(Color.darkGray,1));
ENDRESULT: 最终结果:
Myself, I would use a GridBagLayout for something like this. 我自己,我会使用GridBagLayout这样的东西。 For instance, while this is not a perfect renditioning of your problem, and I use JTextFields where you use JLists, you get the idea:
例如,虽然这不是对问题的完美诠释,但我在使用JList的地方使用了JTextFields,但您会明白:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class InputForm extends JPanel {
private static final int COLUMNS = 10;
private static final int GAP = 3;
private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
private String[] labelTexts;
private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
public InputForm(String[] labelTexts) {
this.labelTexts = labelTexts;
setLayout(new GridBagLayout());
for (int i = 0; i < labelTexts.length; i++) {
String text = labelTexts[i];
JTextField field = new JTextField(COLUMNS);
fieldMap.put(text, field);
addLabel(text, i);
addTextField(field, i);
}
}
public String[] getLabelTexts() {
return labelTexts;
}
private void addTextField(JTextField field, int row) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx = 1;
gbc.gridy = row;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = TEXTFIELD_INSETS;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(field, gbc);
}
private void addLabel(String text, int row) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.gridx = 0;
gbc.gridy = row;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = LABEL_INSETS;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(new JLabel(text), gbc);
}
public String getFieldText(String key) {
String text = "";
JTextField field = fieldMap.get(key);
if (field != null) {
text = field.getText();
}
return text;
}
private static void createAndShowGui() {
String[] labelTexts = new String[] { "ROHTABAK", "HERSTELLER",
"WARENGRUPPE", "MARKENLOGO" };
InputForm inputForm = new InputForm(labelTexts);
int result = JOptionPane.showConfirmDialog(null, inputForm, "Naehere Infos",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
for (String text : labelTexts) {
System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
which when displayed shows: 显示时显示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.