今天搞这个字体程序搞了一下午,因为原本对JAVA的事件模型还不太了解,所以只能自己摸索着.
很容易看懂的程序,所以就不加注解了!!!
java 代码
- import javax.swing.*;
- import java.awt.*;
- import java.awt.Color;
- import java.awt.event.*;
- public class MyFrame {
- /** Creates a new instance of Myframe */
- public static void main(String[] args) {
- SimpleFrame frame=new SimpleFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.show();
- }
- }
- class SimpleFrame extends JFrame{
- public SimpleFrame(){
- setTitle("还在写这个难看的程序");
- setSize(MYWIDTH,MYHEIGHT);
- MyPanel panel=new MyPanel();
- Container contentPane=getContentPane();
- contentPane.add(panel);
- }
- public static final int MYWIDTH=600;
- public static final int MYHEIGHT=200;
- }
- class MyPanel extends JPanel{
- public MyPanel(){
- JButton redButton=new JButton("RED");
- JButton greenButton=new JButton("GREEN");
- JButton blueButton=new JButton("BLUE");
- add(redButton);
- add(greenButton);
- add(blueButton);
- ColorAction redAction=new ColorAction(Color.RED);
- ColorAction greenAction=new ColorAction(Color.GREEN);
- ColorAction blueAction=new ColorAction(Color.BLUE);
- redButton.addActionListener(redAction);
- greenButton.addActionListener(greenAction);
- blueButton.addActionListener(blueAction);
- }
- public void paintComponent(Graphics g){
- super.paintComponents(g);
- Graphics2D g2=(Graphics2D)g;
- Font f=new Font("黑体",Font.BOLD,36);
- g2.setFont(f);
- g2.drawString("也能往这面板上加点东西啊!!",M_X,M_Y);
- }
- private class ColorAction implements ActionListener{
- public ColorAction(Color c){
- fontColor=c;
- }
- public void actionPerformed(ActionEvent event){
- setForeground(fontColor);
- }
- private Color fontColor;
- }
- public static final int M_X=75;
- public static final int M_Y=100;
- }