我正在尝试自学如何使用 Java swing 和 Window Builder Pro 制作 GUI,在观看了几个 youtube 视频、阅读了一些教程并询问了位于此处的上一个问题之后,我已经完成了以下操作。
public class JetstreamFrame extends JFrame {
private static final long serialVersionUID = 1L;
JTabbedPane tabPane;
private JPanel buttonOnePanel;
private JPanel buttonTwoPanel;
private SpringLayout springLayout;
private SpringLayout sl_buttonTwoPanel;
private SpringLayout sl_buttonOnePanel;
private JButton ButtonTwo;
private JButton ButtonOne;
private JScrollPane displayTextPanel;
private JTextArea textArea;
private ScrollPaneLayout sl_displayTextPanel;
private JComboBox<String> comboBox;
public JetstreamFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
tabPane = new JTabbedPane(JTabbedPane.TOP);
setupTabPane();
buttonOnePanel = new JPanel();
sl_buttonOnePanel = new SpringLayout();
setupButtonOnePanel();
buttonTwoPanel = new JPanel();
sl_buttonTwoPanel = new SpringLayout();
setupButtonTwoPanel();
ButtonOne = new JButton("Click Here!");
setupButtonOne();
setupComboBox();
ButtonTwo = new JButton("Click Here!");
setupButtonTwo();
displayTextPanel = new JScrollPane(textArea);
sl_displayTextPanel = new ScrollPaneLayout();
setupDisplayTextPanel();
textArea = new JTextArea();
displayTextPanel.setViewportView(textArea);
}
private void setupTabPane()
{
springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, 245, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().setLayout(springLayout);
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, -198, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().add(tabPane);
}
private void setupButtonOnePanel()
{
tabPane.addTab("Tab One", null, buttonOnePanel, null);
buttonOnePanel.setLayout(sl_buttonOnePanel);
}
private void setupButtonOne()
{
ButtonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
textArea.append("You pressed button one, ");
textArea.append("ComboField: " + (String)comboBox.getSelectedItem() + "\n");
}
});
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, ButtonOne, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, ButtonOne, 187, SpringLayout.WEST, buttonOnePanel);
buttonOnePanel.add(ButtonOne);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setupComboBox()
{
String[] array = { "1" , "2", "3", "4" };
comboBox = new JComboBox(array);
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, comboBox, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, comboBox, 6, SpringLayout.EAST, ButtonOne);
sl_buttonOnePanel.putConstraint(SpringLayout.EAST, comboBox, 167, SpringLayout.EAST, ButtonOne);
buttonOnePanel.add(comboBox);
}
private void setupButtonTwoPanel()
{
tabPane.addTab("Tab Two", null, buttonTwoPanel, null);
buttonTwoPanel.setLayout(sl_buttonTwoPanel);
}
private void setupButtonTwo()
{
ButtonTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
textArea.append("You pressed button two\n");
}
});
sl_buttonTwoPanel.putConstraint(SpringLayout.NORTH, ButtonTwo, 99, SpringLayout.NORTH, buttonTwoPanel);
sl_buttonTwoPanel.putConstraint(SpringLayout.WEST, ButtonTwo, 187, SpringLayout.WEST, buttonTwoPanel);
buttonTwoPanel.add(ButtonTwo);
}
private void setupDisplayTextPanel()
{
springLayout.putConstraint(SpringLayout.NORTH, displayTextPanel, 5, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.WEST, displayTextPanel, 5, SpringLayout.WEST, tabPane);
springLayout.putConstraint(SpringLayout.SOUTH, displayTextPanel, 195, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.EAST, displayTextPanel, -5, SpringLayout.EAST, tabPane);
getContentPane().add(displayTextPanel);
displayTextPanel.setLayout(sl_displayTextPanel);
}
public void start()
{
this.setSize(500, 500);
this.setVisible(true);
}
}
自从上一个问题以来,我已经能够在窗口关闭时实现程序的正确退出,并且实现一个组合框,其中按下按钮将打印在组合框中选择的值。
我有几个想要实现的功能,但我不确定如何前进。
我想要完成的第一件事,可能也是最简单的,是禁用调整窗口大小的功能。由于我无法找到一种简单的方法来实现在调整大小时保持字段相对,因此消除调整窗口大小的能力是下一个最佳选择。
我想澄清的另一个选项是在构建后编辑组合框的内容。理想情况下,我希望能够通过按下按钮从字段中添加和删除条目。我找不到允许我设置框内容的方法,而且似乎实现此目的的唯一方法是每次进行更改时创建一个新的组合框。有没有更有效的方法来做到这一点?