我对Java很陌生,我只是想通过一本教科书自学。教科书提供了一个小程序的如下代码:
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SavitchCh6Prjct14 extends JApplet implements ActionListener
{
private JLabel response;
private Container contentPane;
public void init()
{
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
//Create Button:
JButton aButton = new JButton("Push me!");
aButton.addActionListener(this);
//create label
response = new JLabel("Thanks. That felt good!");
ImageIcon smileyFaceIcon = new ImageIcon("smiley.jpg");
response.setIcon(smileyFaceIcon);
response.setVisible(false);//invisible until button is clicked.
//Add button:
contentPane.setLayout(new FlowLayout());
contentPane.add(aButton);
//Add Label
contentPane.add(response);
}//end init
public void actionPerformed(ActionEvent e)
{
contentPane.setBackground(Color.PINK);
response.setVisible(true);//show label when true
}//end actionPerformed
}//end class
我的一个练习是让点击的按钮在被点击后变得不可见。
在“reponse.setVisible(true);”下的“actionPerformed”中 我尝试插入代码:
aButton.setVisible(false);
但这给了我一条错误消息,我不确定还能做些什么来改变这个现有的代码以使按钮在被点击后消失。