⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档 (2).txt

📁 这段程序是我的一个项目里的一段
💻 TXT
字号:
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestBackground implements MouseMotionListener,ActionListener {
 /**
  * 杨训庭 java编程 项目笔记
  * msn:cfayang@hotmail.com
  * tel:13811715388
  */


 static final int r=3;
 static int ox, oy;
 static int flag=1;//颜色控制
 public static void main(String[] args) {
 JFrame f = new JFrame();
 
 JPanel panel = new JPanel() {
  
   ImageIcon  img = new ImageIcon("edit.jpg");
  public void paintComponent(Graphics g) {
   g.drawImage(img.getImage(), 0,40, this);
   super.paintComponent(g);
  }
 };

 panel.setLayout(new FlowLayout());
  
  JButton jButton1 = new JButton("红");
  JButton jButton2 = new JButton("黑");
  JButton jButton3 = new JButton("绿");
  JButton jButton4 = new JButton("保存");
  JButton jButton5 = new JButton("退出");
  
  
  panel.setOpaque(false);
  panel.add(jButton1);
  panel.add(jButton2);
  panel.add(jButton3);
  panel.add(jButton4);
  panel.add(jButton5);
  
  jButton1.addActionListener(new  TestBackground());
  jButton2.addActionListener(new  TestBackground());
  jButton3.addActionListener(new  TestBackground());
  jButton4.addActionListener(new  TestBackground());
  panel.addMouseMotionListener(new  TestBackground());
  
  f.setContentPane(panel);
  f.setSize(800, 600);
  f.setVisible(true);
 }

public void mouseDragged(MouseEvent e) {
 Container c=(Container)e.getSource();
    Graphics g=c.getGraphics();
     if (oy>=40) {
     if(flag==1){
      g.setColor(new Color(168,0,255));
      g.fillRect(ox, oy, 3, 3);}
     else if(flag==2){
      g.setColor(new Color(0,0,0));
     g.fillRect(ox, oy, 3, 3);
     }
     else{
      g.setColor(new Color(0,255,0));
       g.fillRect(ox, oy, 3, 3);
    }}
    ox=e.getX();oy=e.getY();
 
}
public void mouseMoved(MouseEvent e) {
    ox=-1;oy=-1;
 
}
public void actionPerformed(ActionEvent e) {
 if(e.getActionCommand().equals("红")){
      flag=1; }
     else if (e.getActionCommand().equals("黑")){
      flag=2;}
     else if (e.getActionCommand().equals("绿")){
      flag=3;}
     else if (e.getActionCommand().equals("退出"))
      System.exit(0);}
}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -