import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
public class ButtonTest extends javax.swing.JPanel implements ActionListener {
private JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new ButtonTest());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ButtonTest() {
super();
initGUI();
}
private void initGUI() {
try {
setPreferredSize(new Dimension(400, 300));
{
button = new JButton();
this.add(button);
button.setText("Click Me");
button.addActionListener(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("Click Me")){
button.setText("Click Me Again");
}else{
button.setText("Click Me");
}
}
}
温馨提示:答案为网友推荐,仅供参考