一:第一个图形——按钮
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tx implements ActionListener {
//实现ActionListener接口
JButton button;//创建按钮
public static void main(String[] args) {
tx gui = new tx();
gui.go();
}
public void go()
{
JFrame frame = new JFrame();
button = new JButton("click me");//按钮上的文本显示
button.addActionListener(this);//向按钮注册
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//在窗口关闭时结束程序
frame.getContentPane().add(BorderLayout.CENTER,button);//将按钮放在中心
frame.setSize(300,300);//设置出现在你屏幕的那个窗口多大
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)//actionPerformed不能改喔
{
button.setText("ywq i love you");
}
}
二:第二个图形——双重按钮加变色变文本内容
/*目标: 设置两个独立按钮:需要分别创建不同的指令接受类 class 类名 implements ActionListener{}
//实现label的按钮接受信息
class LabelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
//实现改变颜色的按钮接受信息
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
*/
//图形设置及打印,ht类
import javax.swing.*;
import java.awt.*;
public class ht extends JPanel{
//注意要继承JPanel
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
//设置随机颜色
int red = (int) (Math.random()*255);
int green = (int) (Math.random()*255);
int blue = (int) (Math.random()*255);
Color startColor = new Color(red,green,blue);
//设置随机颜色
red = (int) (Math.random()*255);
green = (int) (Math.random()*255);
blue = (int) (Math.random()*255);
Color endColor = new Color(red,green,blue);
//设置渐变
GradientPaint gradient = new GradientPaint(70,70,startColor,150,150,endColor);
g2d.setPaint(gradient);
//画出图形
g2d.fillOval(70,70,100,100);
}
}
//主类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class gui3c{
public static int i = 1;
public static int getI() {
return i;
}
public static void setI(int i) {
gui3c.i = i+1;
}
JFrame frame;
JLabel label;
public static void main(String[] args) {
gui3c gui = new gui3c();
gui.go();
}
public void go()
{
//常规设置
frame =new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建color按钮
JButton colorButton = new JButton("change color");
colorButton.addActionListener(new ColorListener());
//创建label按钮
JButton labelButton = new JButton("change label");
labelButton.addActionListener(new LabelListener());
//设置label
ht draw =new ht();
label = new JLabel("i am a label");
//将label,label按钮,color按钮和ht中的图形加入frame
frame.getContentPane().add(BorderLayout.SOUTH,colorButton);
frame.getContentPane().add(BorderLayout.EAST,labelButton);
frame.getContentPane().add(BorderLayout.CENTER,draw);
frame.getContentPane().add(BorderLayout.WEST,label);
//设置图形的大小
frame.setSize(300,300);
frame.setVisible(true);
}
//实现label的按钮
class LabelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//改变文本内容
label.setText("于文晴小宝贝我爱你"+"*"+i);
setI(i);
}
}
//实现改变颜色的按钮
class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//每次repaint()都会执行paintComponent里面的指令
frame.repaint();
}
}
}
值得注意的是:
1.JFrame的用法
2.按钮创建JButton,文本创建JLabel
单个按钮与多个按钮ActionListener的写法不同