[英]Hide JButton after click on it
我想要两件事:
将其他按钮保留在我隐藏“播放”按钮之前的相同位置
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.