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框架,里面有画图,显示字和颜色,还有按钮以及按钮事件