我想写每秒改变颜色两次的文本。有人可以告诉我为什么它不起作用吗?如果您能向我解释该怎么做,我将非常高兴。
import java.awt.EventQueue;
import java.awt.Color;
import javax.swing.*;
import java.util.Random;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
public class test {
private JFrame frmTest;
private JTextField textField;
private JLabel lblNewLabel;
private JTextField textField_1;
private JLabel lblNombreUtilisPour;
private JTextField textField_2;
private JLabel lblNombreChiffr;
private JLabel lblNombreDechiffr;
private JTextField textField_3;
private JButton btnDehiffrer;
private JSeparator separator;
private JTextField textField_4;
private JButton button;
private JLabel label;
private JTextField textField_6;
private JLabel label_2;
private JTextField textField_7;
private JLabel label_3;
private JButton button_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test window = new test();
window.frmTest.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public test() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
frmTest = new JFrame();
frmTest.setResizable(false);
frmTest.setTitle("TPE");
frmTest.setBounds(100, 100, 450, 450);
frmTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTest.getContentPane().setLayout(null);
JLabel label_1 = new JLabel(new ImageIcon("test5.gif"));
label_1.setForeground(randomColor.brighter());
frmTest.setContentPane(label_1);
textField = new JTextField();
textField.setSelectedTextColor(randomColor.brighter());
textField.setOpaque(false);
textField.setForeground(randomColor.brighter());
textField.setName("");
textField.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
textField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
if(!(Character.isDigit(evt.getKeyChar()))){
evt.consume();
}
}
});
}
});
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setToolTipText("Entrer un nombre ici");
textField.setBounds(215, 32, 219, 20);
frmTest.getContentPane().add(textField);
textField.setColumns(10);
textField.setDocument(new JTextFieldLimit(9));
lblNewLabel = new JLabel("Entrer un nombre:");
lblNewLabel.setForeground(randomColor.brighter());
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNewLabel.setBounds(10, 30, 141, 20);
frmTest.getContentPane().add(lblNewLabel);
textField_1 = new JTextField();
textField_1.setSelectedTextColor(randomColor.brighter());
textField_1.setForeground(randomColor.brighter());
textField_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_1.setOpaque(false);
textField_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_1.setHorizontalAlignment(SwingConstants.CENTER);
textField_1.setToolTipText("Nombre utilisé pour chiffrer: ");
textField_1.setEditable(false);
textField_1.setColumns(10);
textField_1.setBounds(215, 63, 219, 20);
frmTest.getContentPane().add(textField_1);
lblNombreUtilisPour = new JLabel("Nombre utilisé pour chiffrer: ");
lblNombreUtilisPour.setForeground(randomColor.brighter());
lblNombreUtilisPour.setToolTipText("Nombre utilisé pour chiffrer: ");
lblNombreUtilisPour.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNombreUtilisPour.setBounds(10, 61, 195, 20);
frmTest.getContentPane().add(lblNombreUtilisPour);
textField_2 = new JTextField();
textField_2.setSelectedTextColor(randomColor.brighter());
textField_2.setForeground(randomColor.brighter());
textField_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_2.setOpaque(false);
textField_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_2.setEditable(false);
textField_2.setHorizontalAlignment(SwingConstants.CENTER);
textField_2.setToolTipText("Nombre chiffré: ");
textField_2.setColumns(10);
textField_2.setBounds(215, 94, 219, 20);
frmTest.getContentPane().add(textField_2);
lblNombreChiffr = new JLabel("Nombre chiffré: ");
lblNombreChiffr.setForeground(randomColor.brighter());
lblNombreChiffr.setToolTipText("Nombre chiffré: ");
lblNombreChiffr.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNombreChiffr.setBounds(10, 92, 195, 20);
frmTest.getContentPane().add(lblNombreChiffr);
textField_3 = new JTextField();
textField_3.setSelectedTextColor(randomColor.brighter());
textField_3.setForeground(randomColor.brighter());
textField_3.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_3.setOpaque(false);
textField_3.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_3.setHorizontalAlignment(SwingConstants.CENTER);
textField_3.setEditable(false);
textField_3.setToolTipText("Nombre dechiffré: ");
textField_3.setColumns(10);
textField_3.setBounds(215, 125, 219, 20);
frmTest.getContentPane().add(textField_3);
lblNombreDechiffr = new JLabel("Nombre dechiffré: ");
lblNombreDechiffr.setForeground(randomColor.brighter());
lblNombreDechiffr.setToolTipText("Nombre dechiffré: ");
lblNombreDechiffr.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNombreDechiffr.setBounds(10, 125, 151, 20);
frmTest.getContentPane().add(lblNombreDechiffr);
btnDehiffrer = new JButton("Dechiffrer");
btnDehiffrer.setHorizontalTextPosition(SwingConstants.CENTER);
btnDehiffrer.setBorder(null);
btnDehiffrer.setContentAreaFilled(false);
btnDehiffrer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int min = 0;
int max = 250;
Random r = new Random();
//Demande de nombre
int nmb = Integer.parseInt(textField.getText());
//Chifrement
int chif = r.nextInt(max-min) * r.nextInt(max-min) + r.nextInt(max-min) / r.nextInt(max-min);
int chif_nmb = nmb * chif;
int dechif_nmb = chif_nmb / chif;
textField_3.setText(""+dechif_nmb);
}
});
JButton btnButton1 = new JButton("Chiffrer");
btnButton1.setHorizontalTextPosition(SwingConstants.CENTER);
btnButton1.setContentAreaFilled(false);
btnButton1.setBorder(null);
btnButton1.setOpaque(false);
btnButton1.setToolTipText("Chiffrer");
btnButton1.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnButton1.setForeground(randomColor.brighter());
btnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int min = 0;
int max = 250;
Random r = new Random();
//Demande de nombre
int nmb = Integer.parseInt(textField.getText());
//Chifrement
int chif = r.nextInt(max-min) * r.nextInt(max-min) + r.nextInt(max-min) / r.nextInt(max-min);
textField_1.setText(""+chif);
int chif_nmb = nmb * chif;
textField_2.setText(""+chif_nmb);
}
});
btnButton1.setBounds(0, 156, 450, 23);
frmTest.getContentPane().add(btnButton1);
btnDehiffrer.setToolTipText("Dechiffrer");
btnDehiffrer.setForeground(randomColor.brighter());
btnDehiffrer.setFont(new Font("Tahoma", Font.PLAIN, 16));
btnDehiffrer.setBounds(0, 190, 450, 23);
frmTest.getContentPane().add(btnDehiffrer);
separator = new JSeparator();
separator.setOpaque(true);
separator.setBackground(randomColor.brighter());
separator.setForeground(randomColor.brighter());
separator.setBounds(0, 224, 450, 2);
frmTest.getContentPane().add(separator);
textField_4 = new JTextField();
textField_4.setSelectedTextColor(randomColor.brighter());
textField_4.setForeground(randomColor.brighter());
textField_4.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_4.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_4.setOpaque(false);
textField_4.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
textField_4.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
if(!(Character.isDigit(evt.getKeyChar()))){
evt.consume();
}
}
});
}
});
textField_4.setToolTipText("Entrer un nombre ici");
textField_4.setHorizontalAlignment(SwingConstants.CENTER);
textField_4.setColumns(10);
textField_4.setBounds(215, 261, 219, 20);
frmTest.getContentPane().add(textField_4);
textField_4.setDocument(new JTextFieldLimit(9));
label = new JLabel("Entrer un nombre:");
label.setForeground(randomColor.brighter());
label.setFont(new Font("Tahoma", Font.PLAIN, 15));
label.setBounds(10, 259, 141, 20);
frmTest.getContentPane().add(label);
textField_6 = new JTextField();
textField_6.setSelectedTextColor(randomColor.brighter());
textField_6.setForeground(randomColor.brighter());
textField_6.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_6.setOpaque(false);
textField_6.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_6.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
textField_6.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
if(!(Character.isDigit(evt.getKeyChar()))){
evt.consume();
}
}
});
}
});
textField_6.setToolTipText("Nombre chiffré: ");
textField_6.setHorizontalAlignment(SwingConstants.CENTER);
textField_6.setColumns(10);
textField_6.setBounds(215, 292, 219, 20);
frmTest.getContentPane().add(textField_6);
textField_6.setDocument(new JTextFieldLimit(9));
label_2 = new JLabel("Nombre chiffré: ");
label_2.setForeground(randomColor.brighter());
label_2.setToolTipText("Nombre chiffré: ");
label_2.setFont(new Font("Tahoma", Font.PLAIN, 15));
label_2.setBounds(10, 290, 195, 20);
frmTest.getContentPane().add(label_2);
textField_7 = new JTextField();
textField_7.setSelectedTextColor(randomColor.brighter());
textField_7.setForeground(randomColor.brighter());
textField_7.setFont(new Font("Tahoma", Font.PLAIN, 14));
textField_7.setOpaque(false);
textField_7.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
textField_7.setEditable(false);
textField_7.setToolTipText("Nombre dechiffré: ");
textField_7.setHorizontalAlignment(SwingConstants.CENTER);
textField_7.setColumns(10);
textField_7.setBounds(215, 323, 219, 20);
frmTest.getContentPane().add(textField_7);
label_3 = new JLabel("Nombre dechiffré: ");
label_3.setForeground(randomColor.brighter());
label_3.setToolTipText("Nombre dechiffré: ");
label_3.setFont(new Font("Tahoma", Font.PLAIN, 15));
label_3.setBounds(10, 321, 151, 20);
frmTest.getContentPane().add(label_3);
button = new JButton("Chiffrer");
button.setHorizontalTextPosition(SwingConstants.CENTER);
button.setContentAreaFilled(false);
button.setBorder(null);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Demande de nombre
int nmb = Integer.parseInt(textField_4.getText());
//Chifrement
int chif_nmb = (510-nmb)+834*nmb;
textField_6.setText(""+chif_nmb);
}
});
button.setToolTipText("Chiffrer");
button.setForeground(randomColor.brighter());
button.setFont(new Font("Tahoma", Font.PLAIN, 16));
button.setBounds(0, 354, 450, 23);
frmTest.getContentPane().add(button);
button_1 = new JButton("Dechiffrer");
button_1.setHorizontalTextPosition(SwingConstants.CENTER);
button_1.setBorder(null);
button_1.setContentAreaFilled(false);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Demande de nombre
int nmb = Integer.parseInt(textField_6.getText());
//Chifrement
int chif_nmb = (nmb - 510)/833;
textField_7.setText(""+chif_nmb);
}
});
button_1.setToolTipText("Dechiffrer");
button_1.setForeground(randomColor.brighter());
button_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
button_1.setBounds(0, 388, 450, 23);
frmTest.getContentPane().add(button_1);
JLabel lblCh = new JLabel("Chiffrement complexe");
lblCh.setForeground(randomColor.brighter());
lblCh.setFont(new Font("Comic Sans MS", Font.BOLD, 19));
lblCh.setBounds(121, 0, 219, 28);
frmTest.getContentPane().add(lblCh);
JLabel label_4 = new JLabel("Chiffrement normal");
label_4.setForeground(randomColor.brighter());
label_4.setFont(new Font("Comic Sans MS", Font.BOLD, 19));
label_4.setBounds(121, 230, 219, 28);
frmTest.getContentPane().add(label_4);
}
}
答案 0 :(得分:0)
您可以编写一个Thread来根据需要更改组件的颜色,例如:
class Effect extends Thread {
public void run() {
try {
while (true) {
yourLabel.setForeground(new Color(aValue, bValue, cValue)); // you can apply coloring logic
Thread.sleep(500); //sleep for 0.5 secs , so your color changes twice every second
}
} catch (InterruptedException e) {
}
}
}