Pare drobnych zmian i bedzie dzialac - w zasadzie wystarczy zmienic typ wyniku z JLabel na ColouredLabel
zamiana2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Zamiana2 implements ActionListener
{
JPanel panel;
JFrame frame;
JTextField argument;
JLabel tekst;
ColouredLabel wynik;
JButton zamiana;
public Zamiana2()
{
frame = new JFrame("Zamiana");
frame.setSize(500, 300);
panel = new JPanel();
panel.setLayout(null);
frame.getContentPane().add(panel);
// utworzenie komponentów (etykiet, pól tekst., przycisków)
argument = new JTextField(10);
argument.setBounds(180,140,150,18);
tekst = new JLabel("Wpisz tekst:");
tekst.setBounds(100,140,200,18);
zamiana = new JButton("OK");
zamiana.setBackground(Color.yellow);
zamiana.setForeground(Color.black);
zamiana.setBounds(340,140,60,18);
wynik = new ColouredLabel("Witaj swiecie !!!");
wynik.Start();
wynik.setBounds(210,50,200,50);
// dodanie komponentów do panelu
panel.add(argument);
panel.add(tekst);
panel.add(zamiana);
panel.add(wynik);
// nasł. zdarzeń z przycisku "zamiana"
zamiana.addActionListener(this);
frame.setVisible(true);
// obsługa zdarzenia (zamknięcie okna)
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
// definicja metody obsługującej zdarzenie (naciśnięcie przycisku)
public void actionPerformed(ActionEvent e) {
try {
wynik.setText(argument.getText());
argument.setText("");
} catch(NumberFormatException ev) {
System.out.println("Blad argumentow!? Wpisz poprawne wartosci!");
}
}
public static void main(String[] args) {
// zmiana wyglądu na "Java Look&Feel"
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {}
new Zamiana2();
}
}
Colourable.java
import java.awt.Color;
interface Colourable
{
void setColor(Color colour);
Color getColor();
}
Refreshable.java
interface Refreshable
{
void refresh();
}
ColouredLabel.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
public class ColouredLabel extends JLabel implements Colourable, Refreshable
{
class FlashingThread extends Thread
{
private Colourable component;
private int frequency;
public boolean isRunning = true;
public FlashingThread(Colourable component, int frequency)
{
this.component = component;
this.frequency = frequency;
}
public void run()
{
while(isRunning)
{
component.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
if(component instanceof Refreshable )
((Refreshable)component).refresh();
try {
sleep((int)(1000/frequency));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Color colour;
FlashingThread thread;
public ColouredLabel(String string) {
super(string);
thread = new FlashingThread(this, 1);
}
public ColouredLabel() {
super();
thread = new FlashingThread(this, 1);
}
public void Start()
{
thread.start();
}
public void paint(Graphics g)
{
this.setForeground(colour);
super.paint(g);
}
public void setColor(Color colour)
{
this.colour = colour;
}
public Color getColor()
{
return colour;
}
public void refresh()
{
repaint();
}
}
pozdrawiam
johny