做出图形用户接口1
2015-08-01 21:01
295 查看
<pre name="code" class="java">import javax.swing.*; public class SimpleGuil//第一个GUI程序1 { public static void main(String [] args) { JFrame frame=new JFrame(); JButton button=new JButton(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这一行程序会在window关闭时候把程序结束掉 frame.getContentPane().add(button);// frame.setSize(300,300); frame.setVisible(true); } }
import java.awt.*;//自己的绘图组件的程序2 import javax.swing.*; class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.orange); g.fillRect(20,50,100,100);//fillRect()函数功能:该函数用指定的画刷填充矩形,此函数包括矩形的左上边界,但不包括矩形的右下边界。<div class="para">//函数原型:int FillRect(HDC hdc, CONST RECT *lprc, HBRUSH hbr);{( <strong>voidFillRect(LPCRECT</strong><em>lpRect</em><strong>,CBrush*</strong><em>pBrush</em><strong>);}</strong></div><div class="para">)</div> } }
import javax.swing.*; public class SimpleGuil { public static void main(String [] args) { JFrame frame=new JFrame(); MyDrawPanel my=new MyDrawPanel(); frame.getContentPane().add(my); //JButton button=new JButton(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//这一行程序会在window关闭时候把程序结束掉 //frame.getContentPane().add(button);// frame.setSize(300,300); frame.setVisible(true); } }
import javax.swing.*;//取得按钮的ActionEvent程序3 import java.awt.event.*;//import进有ActionListener和ActionEvent的包 public class SimpleGuilB implements ActionListener//实现此接口这表示SimpleGuilB是个ActionListener(事件只会通知有实现ActionListener的类) { JButton button; public static void main(String [] args) { SimpleGuilB gui=new SimpleGuilB(); gui.go(); } public void go() { JFrame frame=new JFrame(); button=new JButton(); button.addActionListener(this);//向按钮注册 frame.getContentPane().add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); frame.setVisible(true); } public void actionPerformed(ActionEvent event)//实现interface的方法,这是真正处理事件的方法,按钮会以ActionEvent对象作为参数来调用此方法 { button.setText("l've been clicked!"); } }
相关文章推荐
- 内部类,局部类,匿名内部类
- 程序化执行很困难,但必须勇往直前
- NSLog字典和数组打印为中文(Foundation框架类目)
- poco JSON 的使用
- 求2014^X 因子和
- 汇编学习笔记
- Python学习之字典
- 第一周周六
- Sqrt(x)
- ThreadLocal的设计与使用(原理篇)
- 达内培训,安卓4G课程,第一天笔记(手抄版)
- Windows系统技巧
- HDU 1712 分组背包问题
- HDU 1113-Word Amalgamation
- vim替换命令
- 【C/C++学院】0725-内存补码分析/补码原码实战/打印整数二进制数据/静态库说明
- cmder简单使用
- 随意交易(人为)与100%机械交易(程序化)的选择
- 使用wsimport生成ws服务端
- struts2注解实现页面的跳转