// TestButton.javaimport java.awt.*;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.*;public class TestButton extends JFrame implements ActionListener{private JButton jButton1;private JLabel jLabel1;private int count=0;public static void main(String[] args){TestButton frame = new TestButton();frame.pack();frame.setVisible(true);}public TestButton(){setTitle("TestButton");//创建一个带有标签文本和图标的按钮jButton1=new JButton("Press me",new ImageIcon("button.jpg"));//设置按钮的热键为"ALT+P"jButton1.setMnemonic('P');//设置按钮的提示信息为"press me"jButton1.setToolTipText("Press me");jLabel1 = new JLabel("按钮点击了 0 次");getContentPane().setLayout(new FlowLayout());getContentPane().add(jButton1);getContentPane().add(jLabel1);//为按钮注册动作事件的事件监听器jButton1.addActionListener(this);}//事件处理方法public void actionPerformed(ActionEvent e){count++;jLabel1.setText("按钮点击了 "+count+" 次");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。