JAVA簡單的SWING及AWT

來源:互聯網
上載者:User

標籤:

慢慢找感覺~~

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.