- package LianXi;
- import java.lang.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class FrameTest
- {
- public static void main(String[] args)
- {
- MyFrame myframe = new MyFrame();
- myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- myframe.setSize(200,100);
- myframe.setVisible(true);
- }
- }
- class MyFrame extends JFrame
- {
- private int number = 0;
- LayoutManager layout = new FlowLayout();
- JLabel label = new JLabel();
- JButton addButton = new JButton("Add");
- public MyFrame(){
- this.setLayout(layout);
- label.setText(new Integer(number).toString());
- addButton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- add();
- }
- });
- this.add(label);
- this.add(addButton);
- }
- //声明为成员方法
- private void add(){
- number++;
- label.setText(new Integer(number).toString());
- }
- }
-
复制代码 |