簡體   English   中英

單擊后隱藏JButton

[英]Hide JButton after click on it

我想要兩件事:

  1. 單擊它后,隱藏我的“播放”按鈕。
  2. 將其他按鈕保留在我隱藏“播放”按鈕之前的相同位置

     public class MusicPlayer { public static void main (String[] args) { JFrame frame = new JFrame("Main window"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(600, 400)); frame.setResizable(false); frame.setVisible(true); frame.pack(); frame.setLayout(new BorderLayout()); JPanel panel = new JPanel(new GridBagLayout()); JButton b1 = new JButton("PLAY"); JButton b2 = new JButton("PAUSE"); JButton b3 = new JButton("STOP"); GridBagConstraints layout = new GridBagConstraints(); layout.insets = new Insets(0, 10, 5, 0); layout.anchor = GridBagConstraints.SOUTH; layout.weighty = 1; panel.add(b1, layout); panel.add(b2, layout); panel.add(b3, layout); frame.add(panel, BorderLayout.CENTER); panel.setBackground(Color.blue); b1.setBackground(Color.green); b2.setBackground(Color.yellow); b3.setBackground(Color.red); } } 

將動作偵聽器添加到“ 播放”按鈕b1

b1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
     b1.setVisible(false); 
  } 
}

它會為你做躲藏的工作。

暫無
暫無

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

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