[java] kolor czcionki inny co 1 sekunde

0

Witam
chodzi mi o zmiane czcionki po odpaleniu programu (czcionka ma zmieniac kolor) np. raz czerwona raz zielona co 1 sekunde... ktos moze by mi wskazal jakis kierunek... albo moze robil cos podobnego kiedys... za wszelka pomoc bede wdzieczny...


       wynik.setText("<html><Font Color=blue>" 
               + slowo + "</Font><Font Color=black> </font></html>");

       wynik.setText("<html><Font Color=red>" 
               + slowo + "</Font><Font Color=black> </font></html>");
          

jak takie cos mozna wyswietlac na zmiane... chodzi o kolor
pozdrawiam

0

Nie do końca rozumiem o co Ci chodzi. Czy dodanie kolejnego wyrazu do wyniku ma powodować zmianę koloru liter dodawanego wyrazu na kolejny? Czy może cały tekst w wyniku ma zmieniać kolor?

0

Stworz sobie watek, ktory bedzie zmienial wartosc koloru dla tego czegos. Najbardziej elegencko byloby dziedziczenie po tym komponencie (zalozmy, ze to Label), gdzie dolozylbys wartosc koloru, watek, ktory ta wartosc zmienialby co sekunde i odswiezal komponent oraz zmieniona metode paint, ktora uwzglednialaby kolor przy rysowaniu.

pozdrawiam
johny

0

nom wszystko pieknie wyjasniles... a ja jestem poczatkujacy wiec... moze jakis przyklad??

0
fran1o napisał(a)

nom wszystko pieknie wyjasniles... a ja jestem poczatkujacy wiec... moze jakis przyklad??

Heh, pisales, ze szukasz kierunku i nie pisales, ze jestes poczatkujacy ani, ze chcesz przyklad. No, ale dzisiaj bede dobrym wujkiem, dam w zasadzie gotowca:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class FlashingLabelExample extends JFrame{
	
	interface Colourable
	{
		void setColor(Color colour);
		Color getColor();
	}
	interface Refreshable
	{
		void refresh();
		
	}
	
	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();
		}
	}
	
	public FlashingLabelExample() 
	{
		setSize(600,300);
		setTitle("FlashingLabelExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		ColouredLabel label= new ColouredLabel("flashing text");
		add(label);
		label.Start();
		setVisible(true);
	}
	
	
	public static void main(String[] args) 
	{
		new FlashingLabelExample();
	}

}

pozdrawiam
johny

0

wszystko sie ladnie kompiluje ale przy uruchomieniu wyskakuje:

java.lang.Error: Do not use FlashingLabelExample.add() use FlashingLabelExample.getContentPane().add() instead
	at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
	at javax.swing.JFrame.addImpl(JFrame.java:491)
	at java.awt.Container.add(Container.java:307)
	at FlashingLabelExample.<init>(FlashingLabelExample.java:96)
	at FlashingLabelExample.main(FlashingLabelExample.java:104)
Exception in thread "main" Exit code: 1
There were errors

:-/

0
fran1o napisał(a)

wszystko sie ladnie kompiluje ale przy uruchomieniu wyskakuje:

java.lang.Error: Do not use FlashingLabelExample.add() use FlashingLabelExample.getContentPane().add() instead
	at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
	at javax.swing.JFrame.addImpl(JFrame.java:491)
	at java.awt.Container.add(Container.java:307)
	at FlashingLabelExample.<init>(FlashingLabelExample.java:96)
	at FlashingLabelExample.main(FlashingLabelExample.java:104)
Exception in thread "main" Exit code: 1
There were errors

:-/

Moj przyklad dziala pod Java 5.0, to sobie zainstaluj, ew. zmien uzycie tych metod tak jak sugeruje kompilator.

pozdrawiam
johny

0

ok dziala
ale mam jeszcze maly problem. Chodzi o to zeby slowo wpisywac samemu z pola JTextField
np tak jak tu nizej (jak polaczyc te 2 kody):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

         
public class Zamiana2 implements ActionListener
{
  JPanel panel;
  JFrame frame;
  JTextField argument;
  JLabel wynik, tekst;
  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 JLabel("Witaj swiecie !!!");
    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 {
    
     
      String slowo = (argument.getText());{
      //ustaw. koloru czcionki
       
      
       wynik.setText("<html><Font Color=blue>" 
               + slowo + "</Font><Font Color=black> </font></html>");

       wynik.setText("<html><Font Color=red>" 
               + slowo + "</Font><Font Color=black> </font></html>");
       
       
       }
                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();
  }
}

moglbys pomoc jeszcze??

0

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

1 użytkowników online, w tym zalogowanych: 0, gości: 1