日期:2014-05-20  浏览次数:20953 次

swing 求助
欲实现Hello,Swing World !原先字体为绿色,点击按钮后,逐个变为红色,大家看看下面代码有什么问题,现在是点击按钮后,需要actionPerformed方法运行完后,变色后字体才有效,导致没达到效果,麻烦高手看下

Java code

package com.test.oct27;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * 
 * 
 * 
 */
public class TAcionEvent extends JApplet {

    /**
     * 
     */
    private static final long serialVersionUID = 2169662119220746054L;
    JLabel[] jls;
    JButton jb;
    String str;
    int len;
    JPanel jp;
    Container container;

    /**
     * @throws HeadlessException
     */
    public TAcionEvent() {
        init();
        addEvent();
    }

    public void init() {
        jb = new JButton("start");
        container = this.getContentPane();
        str = "Hello,Swing World !";
        len = str.length();
        jls = new JLabel[len];
        jp = new JPanel();
        jp.setLayout(new GridLayout(1, len));
        for (int i = 0; i < len; i++) {
            jls[i] = new JLabel(str.substring(i, i + 1));
            jls[i].setForeground(Color.green);
            jp.add(jls[i]);
        }
        container.add(jp);
        container.add(jb, BorderLayout.SOUTH);
    }

    public void addEvent() {    
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if (cmd.equals("start")) {
                    jb.setText("stop");
                    for (int i = 0; i < len; i++) {    
                        jls[i].setForeground(Color.red);
                        delay();
                    }
                } else {
                    jb.setText("start");
                    for (int i = 0; i < len; i++) {
                        jls[i].setText(str.substring(i, i + 1));
                        jls[i].setForeground(Color.green);
                    }
                }

            }
        });
        
    }

    public void delay() {
        for (int i = 0; i < 100000; i++) {
            for (int j = 0; j < 10000; j++) {

            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame jf = new JFrame("TAcionEvent");
        TAcionEvent te = new TAcionEvent();
        jf.getContentPane().add(te);
        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        jf.setSize(300, 300);
        jf.setVisible(true);

    }
}



------解决方案--------------------
Java code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Rocks {
    public static void main(final java.lang.String[] args) {
        java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
                @Override public void run(){
                    final JFrame frame = new JFrame("Java Rocks");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);
                    frame.setPreferredSize(new Dimension(600,400));
                    final Display display = new Display();

                    final JButton start = new JButton("Start");
                    final JButton stop = new JButton("Stop");
                    JPanel buttons = new JPanel();
                    buttons.add(start);
                    buttons.add(stop);

                    frame.getContentPane().add(display);
                    frame.getContentPane().add(buttons,BorderLayout.PAGE_END);
                    frame.pack();
                    frame.setVisible(true);
                    final Timer timer = new Timer(1000,new ActionListener(){
                            @Override public void actionPerformed(ActionEvent e){
                                display.start();
                            }
                        });

                    start.addActionListener(new ActionListener(){
                            @Override public void actionPerformed(ActionEvent e){
                                timer.start();
                            }
                        });

                    stop.addActionListener(new ActionListener(){
                            @Override public void actionPerformed(ActionEvent e){
                                timer.stop();
                            }
                        });

                }
            });

    }

    private static class Display extends JComponent {
        private AttributedString text = new AttributedString("Hello World!");
        private volatile int index = 1;

        public Display(){
            super();
            text.addAttribute(TextAttribute.FOREGROUND,Color.GREEN,0,12);
            text.addAttribute(TextAttribute.SIZE,24,0,12);
            text.addAttribute(TextAttribute.WEIGHT,TextAttribute.WEIGHT_BOLD);
        }

        public void start(){
            if (index <= 12) {
                text.addAttribute(TextAttribute.FOREGROUND,Color.RED,0,index++);
            }
            repaint();
        }

        @Override public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g.drawString(text.getIterator(), 15, 150);
        }
    }
}