java按钮事件,在线等~

来源:百度知道 编辑:UC知道 时间:2024/06/04 05:50:24
当按了按钮后跳转到另一个程序中,有这样的事件吗?
如果有具体是怎么样的呢?

首先要implents ActionListener,所以你要实现actionPerformed方法,我这里是你点一下按钮,数字就自己加一。然后你用button.addActionListern(this);因为这个class已经实现了阿窗体哦你Performed这个方法,所以括号里放this就行了。

写的潦草,请见谅吧~

import javax.swing.*;
import java.awt.event.*;

class test implements ActionListener {

public static int count = 0;
JFrame frame;
JPanel panel;
JLabel label;
JButton button;

public test(){
frame = new JFrame("test");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("Press");
button.addActionListener(this);

label = new JLabel("Number is " + count);
panel.add(button);
panel.add(label);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}//end test

public void actionPerformed(ActionEvent e) {
count++;
label.setText("Number