簡體   English   中英

如何通過單擊一個JButton到同一框架添加新的JButton

[英]How to Add a new JButton with clicking a JButton to the same Frame

我對Swings不太好,請幫幫我。 senario:我試圖在同一框架中添加一個新的Jbutton,同時單擊一個現有的Jbutton,但是它不起作用。

提前致謝。

我正在使用下面的代碼

JButton addAnotherButton = new JButton("Add New Button");
addAnotherButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {  
    if (event.getSource() == addAnotherButton) {
      JButton newButton = new JButton("New born Button");
      newButton.addActionListener(this);
    }
  }
});
addAnotherButton.setFont(new Font("Times New Roman", Font.BOLD, 12));
addAnotherButton.setBounds(276, 222, 137, 29);          
contentPane.add(addAnotherButton);

在代碼中,您將按鈕添加到contentPane中:

contentPane.add(addAnotherButton);

偵聽器不會將按鈕添加到任何ui組件,因此您將看不到它。

也許,您想要將按鈕添加到同一窗格中:

contentPane.add(newButton);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM