您的位置:首页 > 其它

做出图形用户接口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!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: