您的位置:首页 > 产品设计 > UI/UE

Simple Java GUI DEMO(quote from head first java 2e)

2015-07-23 10:44 337 查看
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
class MyDrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(0,  0, this.getWidth(), this.getHeight());

int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);

Color randomColor = new Color(red, green, blue);
g.setColor(randomColor);
g.fillOval(70, 70, 100, 100);
}
}
public class SimpleGui3C implements ActionListener {
JFrame frame;

public static void main(String[] args) {
SimpleGui3C gui = new SimpleGui3C();
gui.go();
}

public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button = new JButton("Change colors");
button.addActionListener(this);

MyDrawPanel drawPanel = new MyDrawPanel();

frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(300, 300);
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
frame.repaint();
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: