我試圖教自己如何使用Java swing和Window Builder Pro製作GUI,看完幾個YouTube視頻後,閱讀一些教程,並詢問位於前面的問題here我已經完成下列。擴展一個簡單的GUI的功能
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);
}
}
由於前一個問題,我已經能夠實現在窗口關閉程序的適當退出,以及組合框,其中一個按下按鈕將打印的組合框裏面選擇的價值的實現。
我有幾個功能我想執行,但我不確定如何前進。
我想要完成的第一件事,也許是最簡單的,就是禁用調整窗口大小的功能。由於我無法找到簡單的方法來完成在調整大小時保留字段的相關性,因此消除調整窗口大小的能力是下一個最佳選擇。
我想澄清的另一個選項是在施工後編輯組合框的內容。理想情況下,我希望能夠通過按鈕添加和刪除字段中的條目。我無法找到一種方法,允許我設置框的內容,並且看起來唯一的方法就是在每次更改時創建一個新的組合框。有沒有更有效的方法來做到這一點?