我开发了自己的属性类型数据结构,用于在应用程序中设置一些基本数据。我只是使用常规属性,但我希望用户能够编辑这些属性然后记录,显示顺序以及它们是否处于活动状态。此外,为了能够对不同的属性进行分组。
我创建了一个面板,显示属性并带有“编辑”按钮,以便他们实际进行更改。
然后另一个面板加载一个组并显示该组的所有属性。
这两个小组似乎都很好。然后,当我创建一个应该将所有组加载到每个面板中的JDialog时,我无法显示它。我玩弄了很多布局,没有运气。另外,我需要一个滚动窗格,所以当他们加班时。
我不太清楚在这一点上要做什么...如果我在面板上添加按钮,它会滚动得很好,所以我假设我想添加的面板有问题。
这是属性组类:
import java.util.LinkedHashMap;
public class SMoKEPropertyGroup {
private String name = null;
private String notes = null;
private int displayOrder = 0;
private boolean active = true;
private LinkedHashMap<String, SMoKEProperty> properties = new LinkedHashMap<>();
public SMoKEPropertyGroup(String name, String notes, int displayOrder, boolean active) {
super();
this.name = name;
this.notes = notes;
this.displayOrder = displayOrder;
this.active = active;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public int getDisplayOrder() {
return displayOrder;
}
public void setDisplayOrder(int displayOrder) {
this.displayOrder = displayOrder;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public LinkedHashMap<String, SMoKEProperty> getProperties() {
return properties;
}
public void addProperties(SMoKEProperty property) {
this.properties.put(property.getName(), property);
}
}
这是实际的属性类:
public class SMoKEProperty {
private String groupName = null;
private String name = null;
private String value = null;
private String notes = null;
private int displayOrder = 0;
private boolean active = true;
private String displayableName = null;
public SMoKEProperty(String groupName, String name, String value, String notes, int displayOrder, boolean active) {
super();
this.groupName = groupName;
this.name = name;
this.value = value;
this.notes = notes;
this.displayOrder = displayOrder;
this.active = active;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getName() {
return name;
}
public String getDisplayableName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public int getDisplayOrder() {
return displayOrder;
}
public void setDisplayOrder(int displayOrder) {
this.displayOrder = displayOrder;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
@Override
public String toString() {
return "SMoKEProperty \n" + " groupName=" + groupName + "\ndisplayableName=" + getDisplayableName() + "\nname="
+ name + "\nvalue=" + value + "\nnotes=" + notes + "\ndisplayOrder=" + displayOrder + "\nactive="
+ active + "\n\n";
}
}
以下是显示属性的面板:
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class PanelSmokeProperty extends JPanel {
private JLabel lblName = new JLabel();
public PanelSmokeProperty(SMoKEProperty prop) {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 318, 567, 97, 0 };
gridBagLayout.rowHeights = new int[] { 25, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
setLayout(gridBagLayout);
JButton btnEdit = new JButton("Edit");
lblName.setHorizontalAlignment(SwingConstants.RIGHT);
lblName.setFont(new Font("Tahoma", Font.PLAIN, 13));
GridBagConstraints gbc_lblName = new GridBagConstraints();
gbc_lblName.weighty = 0.4;
gbc_lblName.weightx = 0.4;
gbc_lblName.fill = GridBagConstraints.HORIZONTAL;
gbc_lblName.insets = new Insets(0, 0, 0, 5);
gbc_lblName.gridx = 0;
gbc_lblName.gridy = 0;
this.add(lblName, gbc_lblName);
lblName.setText(prop.getName() + " = ");
JLabel lblValue = new JLabel(prop.getValue());
GridBagConstraints gbc_lblValue = new GridBagConstraints();
gbc_lblValue.weighty = 0.4;
gbc_lblValue.weightx = 0.4;
gbc_lblValue.fill = GridBagConstraints.HORIZONTAL;
gbc_lblValue.insets = new Insets(0, 0, 0, 5);
gbc_lblValue.gridx = 1;
gbc_lblValue.gridy = 0;
add(lblValue, gbc_lblValue);
GridBagConstraints gbc_btnEdit = new GridBagConstraints();
gbc_btnEdit.weighty = 0.2;
gbc_btnEdit.weightx = 0.2;
gbc_btnEdit.anchor = GridBagConstraints.NORTH;
gbc_btnEdit.fill = GridBagConstraints.BOTH;
gbc_btnEdit.gridx = 2;
gbc_btnEdit.gridy = 0;
add(btnEdit, gbc_btnEdit);
}
}
以下是显示属性组的面板:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
public class PanelSmokePropertyGroup extends JPanel {
public PanelSmokePropertyGroup(SMoKEPropertyGroup propGroup) {
this.setBorder(new TitledBorder(null, propGroup.getName(), TitledBorder.LEADING, TitledBorder.TOP, null, null));
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 1133, 0 };
gridBagLayout.rowHeights = new int[] { 100, 176, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
setLayout(gridBagLayout);
JLabel lblLabelForDescription = new JLabel("Label for Description");
lblLabelForDescription.setPreferredSize(new Dimension(100, 75));
lblLabelForDescription.setVerticalAlignment(SwingConstants.TOP);
GridBagConstraints gbc_lblLabelForDescription = new GridBagConstraints();
gbc_lblLabelForDescription.anchor = GridBagConstraints.NORTH;
gbc_lblLabelForDescription.fill = GridBagConstraints.HORIZONTAL;
gbc_lblLabelForDescription.insets = new Insets(0, 0, 5, 0);
gbc_lblLabelForDescription.gridx = 0;
gbc_lblLabelForDescription.gridy = 0;
this.add(lblLabelForDescription, gbc_lblLabelForDescription);
JPanel panelForProperties = new JPanel();
GridBagConstraints gbc_panelForProperties = new GridBagConstraints();
gbc_panelForProperties.weighty = 1.0;
gbc_panelForProperties.weightx = 1.0;
gbc_panelForProperties.gridx = 0;
gbc_panelForProperties.gridy = 1;
add(panelForProperties, gbc_panelForProperties);
GridBagLayout gbl_panelForProperties = new GridBagLayout();
gbl_panelForProperties.columnWidths = new int[] { 0, 0 };
gbl_panelForProperties.rowHeights = new int[] { 0, 0 };
gbl_panelForProperties.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gbl_panelForProperties.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
panelForProperties.setLayout(gbl_panelForProperties);
GridBagConstraints gbc_panelProperty = new GridBagConstraints();
gbc_panelProperty.fill = GridBagConstraints.HORIZONTAL;
gbc_panelProperty.gridx = 0;
// int scrollPaneHeight = (int) scrollPane.getBounds().getHeight();
// panelForProperties.setPreferredSize(new Dimension(749,
// scrollPaneHeight));
int y = 0;
for (SMoKEProperty p : propGroup.getProperties().values()) {
PanelSmokeProperty panelPropery = new PanelSmokeProperty(p);
gbc_panelProperty.gridy = y++;
panelForProperties.add(panelPropery, gbc_panelProperty);
}
}
public static void main(String... args) {
JFrame frame = new JFrame("test");
frame.setPreferredSize(new Dimension(700, 700));
PanelSmokePropertyGroup panel = new PanelSmokePropertyGroup(DialogProperties.getGroups().get("Group 1"));
// PanelSmokePropertyGroup2 panel2 = new
// PanelSmokePropertyGroup2(DialogProperties.getGroups().get("Group
// 2"));
frame.getContentPane().add(panel);
// frame.getContentPane().add(panel2);
frame.setVisible(true);
}
}
最后,这是使用所有这些并且不会正确显示这些面板的JDialog:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.LinkedHashMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class DialogProperties extends JDialog {
private final JPanel contentPanel = new JPanel();
public static void main(String[] args) {
try {
DialogProperties dn = new DialogProperties();
dn.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dn.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public DialogProperties() {
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("System Properties");
setModal(true);
setBounds(100, 100, 869, 608);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 851, 0 };
gridBagLayout.rowHeights = new int[] { 561, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagConstraints gbc_contentPanel = new GridBagConstraints();
gbc_contentPanel.fill = GridBagConstraints.BOTH;
gbc_contentPanel.gridx = 0;
gbc_contentPanel.gridy = 0;
getContentPane().add(contentPanel, gbc_contentPanel);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[] { 1, 800, 0 };
gbl_contentPanel.rowHeights = new int[] { 477, 50, 0 };
gbl_contentPanel.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
contentPanel.setLayout(gbl_contentPanel);
JScrollPane scrollPane = new JScrollPane();
// scrollPane.setBounds(12, 13, 1235, 382);
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridwidth = 2;
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
contentPanel.add(scrollPane, gbc_scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(new GridLayout(0, 1));
JButton okButton = new JButton("OK");
GridBagConstraints gbc_okButton = new GridBagConstraints();
gbc_okButton.ipady = 45;
gbc_okButton.ipadx = 45;
gbc_okButton.anchor = GridBagConstraints.SOUTHEAST;
gbc_okButton.gridx = 1;
gbc_okButton.gridy = 1;
contentPanel.add(okButton, gbc_okButton);
okButton.setActionCommand("OK");
getRootPane().setDefaultButton(okButton);
for (SMoKEPropertyGroup group : getGroups().values()) {
PanelSmokePropertyGroup p = new PanelSmokePropertyGroup(group);
panel.add(p);
}
}
public static LinkedHashMap<String, SMoKEPropertyGroup> getGroups() {
LinkedHashMap<String, SMoKEPropertyGroup> groups = new LinkedHashMap<>();
for (int i = 1; i <= 10; i++) {
SMoKEPropertyGroup g1 = new SMoKEPropertyGroup("Group " + i, "some notes", 0, true);
for (int p = 1; p <= 15; p++) {
g1.addProperties(new SMoKEProperty(g1.getName(), "Property " + p, "something", "notes", 0, true));
}
groups.put(g1.getName(), g1);
}
return groups;
}
}