码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA简单的SWING及AWT

时间:2015-05-07 11:43:01      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

慢慢找感觉~~

package SwingGui.sky.com;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleGui implements ActionListener {
    JFrame frame;
    JLabel label;
    int x =70;
    int y = 70;
    public static void main(String [] args) {
        SimpleGui gui = new SimpleGui();
        gui.go();
        
        
        
    }
    public void go() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton labelButton = new JButton("Change Label");
        labelButton.addActionListener(new LabelListener());
        
        JButton colorButton = new JButton("Change Circle");
        colorButton.addActionListener(new ColorListener());
        
        label = new JLabel("I‘am a label");
        MyDrawPanel drawPanel = new MyDrawPanel();
        
        frame.getContentPane().add(drawPanel);        
        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.EAST, labelButton);
        frame.getContentPane().add(BorderLayout.WEST, label);
        frame.setSize(800, 500);
        frame.setVisible(true);
        
        for (int i = 0; i < 130; i++) {
            x++;
            y++;
            drawPanel.repaint();
            
            try {
                Thread.sleep(20);
            } catch (Exception ex) {}
        }
    }
    @Override 
    public void actionPerformed(ActionEvent event) {
        JButton button=(JButton) event.getSource(); 
        button.setText("I‘v been clicked!..");
        button.setBackground(Color.blue);
    }
    
    class LabelListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            label.setText("Ouch");
        }
    }
    
    class ColorListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            frame.repaint();
        }
    }
    class MyDrawPanel extends JPanel {
        public void paintComponent(Graphics g) {
            g.setColor(Color.white);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.green);
            g.fillOval(x, y, 40, 40);
        }
    }
}

技术分享

JAVA简单的SWING及AWT

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4484074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!