一:第一個圖形——按鈕
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的寫法不同
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253467.html
標籤:java
下一篇:解決mybatis報錯org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
