今天下午做的改变界面字体着色的JAVA程序...

今天搞这个字体程序搞了一下午,因为原本对JAVA的事件模型还不太了解,所以只能自己摸索着.

很容易看懂的程序,所以就不加注解了!!!

java 代码
  1. import javax.swing.*;   
  2. import java.awt.*;   
  3. import java.awt.Color;   
  4. import java.awt.event.*;   
  5.   
  6. public class MyFrame {   
  7.        
  8.     /** Creates a new instance of Myframe */  
  9.     public static void main(String[] args) {   
  10.         SimpleFrame frame=new SimpleFrame();   
  11.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  12.         frame.show();   
  13.     }   
  14.        
  15.        
  16. }   
  17. class SimpleFrame extends JFrame{   
  18.     public SimpleFrame(){   
  19.         setTitle("还在写这个难看的程序");   
  20.         setSize(MYWIDTH,MYHEIGHT);   
  21.         MyPanel panel=new MyPanel();   
  22.         Container contentPane=getContentPane();   
  23.         contentPane.add(panel);   
  24.            
  25.     }   
  26.     public static final int MYWIDTH=600;   
  27.     public static final int MYHEIGHT=200;   
  28. }   
  29. class MyPanel extends JPanel{   
  30.     public MyPanel(){   
  31.         JButton redButton=new JButton("RED");   
  32.         JButton greenButton=new JButton("GREEN");   
  33.         JButton blueButton=new JButton("BLUE");   
  34.         add(redButton);   
  35.         add(greenButton);   
  36.         add(blueButton);   
  37.         ColorAction redAction=new ColorAction(Color.RED);   
  38.         ColorAction greenAction=new ColorAction(Color.GREEN);   
  39.         ColorAction blueAction=new ColorAction(Color.BLUE);   
  40.         redButton.addActionListener(redAction);   
  41.         greenButton.addActionListener(greenAction);   
  42.         blueButton.addActionListener(blueAction);    
  43.     }   
  44.        
  45.     public void paintComponent(Graphics g){   
  46.         super.paintComponents(g);           
  47.         Graphics2D g2=(Graphics2D)g;   
  48.         Font f=new Font("黑体",Font.BOLD,36);   
  49.         g2.setFont(f);    
  50.         g2.drawString("也能往这面板上加点东西啊!!",M_X,M_Y);   
  51.     }   
  52.        
  53.     private class ColorAction implements ActionListener{   
  54.         public ColorAction(Color c){   
  55.            
  56.         fontColor=c;   
  57.         }   
  58.         public void actionPerformed(ActionEvent event){   
  59.         setForeground(fontColor);   
  60.            
  61.           
  62.        }   
  63.         private Color fontColor;   
  64.           
  65.             }   
  66.                           
  67.                
  68.           
  69.            
  70.       
  71.     public static final int M_X=75;   
  72.     public static final int M_Y=100;   
  73. }   
  74.   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值