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(); } }
相关文章推荐
- IOS开发之UITableView
- 如何修改序列(Sequence)的初始值(START WITH)
- 动态计算UITableViewCell高度
- /var/spool/clientmqueue文件多导致磁盘满
- java.lang.ClassNotFoundException: com.opensymphony.xwork2.util.ValueStack
- 【从零开始学NGUI 】 (五)PopupList
- make 2>&1 | tee build.log 求解释
- [leetcode] Longest Consecutive Sequence
- 【Guitar Pro 6 中文破解版】完整安装教程+下载地址
- soapui 不同版本之间切换excel
- 2.RequestParam、RequestHeader、CookieValue注解
- hdu5297 Y sequence(容斥原理+迭代)
- Hibernate Query Language
- 自定义系统控件的外观:UIApearance
- 小胖说事34-----iOS UIImageView 的contentMode属性应用
- [转载]Integer.parseInt()和这个Integer.valueOf()的详解
- Win10准正式版Build 10240多国语言包下载(含111种)
- 优化UITableViewCell高度计算的那些事
- 【从零开始学NGUI 】 (四)Slider
- codeforces(559B)--B. Equivalent Strings(暴搜 或 最小表示法)