學習Java時寫的小程序

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class TuXing {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EventQueue.invokeLater(new Runnable() {
			public void run() {
			SampleFrame ss = new SampleFrame();//創建框架
			ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			ss.setVisible(true);
			}
		});
	}

}

class SampleFrame extends JFrame//框架
{
	public SampleFrame()
	{
		setTitle("liudw");
		setSize(500, 500);
		setLocation(300, 200);
		/*
		SampleComponent yy = new SampleComponent();
		add(yy);
		*/
		buttonTo = new JPanel();//創建組件
		makeButton("yellow", Color.YELLOW);
		makeButton("blue", Color.BLUE);
		makeButton("green", Color.GREEN);
	}
	public void makeButton(String name, final Color backgroundColor){
		JButton button = new JButton(name);//創建按鈕	
		buttonTo.add(button);//將按鈕添加到組件
		add(buttonTo);//將組件添加到框架
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				buttonTo.setBackground(backgroundColor);
			}
		});//將事件與按鈕動作結合
	}
	
	private JPanel buttonTo;
}
/*
class SampleComponent extends JComponent//組件
{
	protected void paintComponent(Graphics g)
	{
		g.drawString("gonging", 250, 250);
		
		Graphics2D g2 = (Graphics2D) g;
		double leftX = 100;
		double topY = 100;
		double width = 200;
		double heigh = 100;
		
		Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, heigh);
		g2.setPaint(Color.RED);
		g2.draw(rect);//畫矩形
		
		Line2D line = new Line2D.Double(leftX, topY, width+100, heigh+100);
		g2.draw(line);//畫直線

	}
	
}
*/
這是基於swing的JFrame框架,裏面有畫圖,顯示字和顏色,還有按鈕以及按鈕事件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章