做出图形用户接口1

<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!");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值