2016-05-02 20 views
0

Ich möchte 2 Dinge:ausblenden JButton nach Klick auf das

  1. ausblenden meine Taste „PLAY“, nachdem ich darauf klicken.
  2. Um weitere Tasten an den gleichen Positionen zu verlassen, in dem sie waren, bevor ich die PLAY-Taste

    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); 
        } 
    } 
    
+0

nicht 'setVisible (falsch);' Arbeit? –

+0

Ich habe versucht, es wie folgt in Funktion zu setzen: b1.addActionListener (new Graj()); \t statische Klasse Graj implementiert Action { \t \t public void actionPerformed (Action e) { \t \t} \t \t \t \t public void hideButton (Action e, JButton b) { \t \t \t b.setVisible (false); \t \t} \t} – wuwunio94

+0

hinzufügen actionlistner zum Posten. –

Antwort

1

hinzufügen Aktion Hörer auf Ihre PLAY Taste b1 versteckte.

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

Es soll den Job des Versteckens für Sie tun.

+1

Danke, ich habe es gemacht – wuwunio94

+0

Froh, dass du es getan hast :) – Sanjeev