扩展简单GUI的功能



我正在尝试自学如何使用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 twon");
        }
    });
    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);
}
}

由于上一个问题,我已经能够在窗口关闭时正确退出程序,并实现组合框,其中按下按钮将打印在组合框内选择的值。

我有几个功能想要实现,但我不确定如何前进。

我想完成的第一件事,可能也是最简单的一件事,就是禁用调整窗口大小的功能。由于我无法找到一种简单的方法来实现在调整大小时保持字段的相对性,因此取消调整窗口大小的功能是下一个最佳选择。

我想澄清的另一个选项是在构建后编辑组合框的内容。理想情况下,我希望能够通过按下按钮来添加和删除字段中的条目。我找不到允许我设置框内容的方法,而实现这一点的唯一方法似乎是每次更改时创建一个新的组合框。有没有更有效的方法可以做到这一点?

要使您的框架具有固定大小,只需使用setResizable(false)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);// <- add this line

如果你想更改组合框的内容,你可以使用多种方法之一,如

  • removeItem(item)将删除特定元素
  • removeItemAt(index)将删除指定位置的元素(从0开始计数)
  • addItem(item)将在组合框末尾添加新项目
  • insertItemAt(item, index)将在指定位置插入项目,向下移动当前项目
  • 你也有removeAllItems()猜它的作用:)

您可以在类的文档中找到许多有用的信息

是禁用调整窗口大小的功能。

frame.setResizable( false );

由于我无法找到一种简单的方法来完成在调整大小时保持字段相对

有比SpringLayout更容易使用的布局管理器。这种布局通常只由IDE使用,因为它相对复杂。这就是为什么我们大多数人都是手工构建GUI的,这样我们就不受IDE的限制。

我找不到允许我设置盒子内容的方法,

若要替换整个内容,需要替换模型。

comboBox.setModel(...);

这就是所有Swing组件的工作方式,因此理解模型的概念非常重要。您的应用程序代码应该只更新模型。在JComboBox的情况下,该组件有一些辅助方法,它们将为您更新模型。

对于您的JComboBox问题,您可以使用getItemAt(int index)来检索您的项目(还有其他getter),使用addItem(E item)添加到您的列表中,最后使用removeItemAt(int index)从列表中删除项目(还有额外的删除方法)。只需确保对其调用setEditable(true),因为通过取消替换JComboBox是不可编辑的。

此外,要使您的窗口大小固定,请在其上调用setResizable(false);

最新更新