我尝试在5秒后更改jtextfield边框。但不起作用。我的代码:
// Here vaildate a field and set border to red
if (ApplicationNameField.getText().equals("")) {
Border newBorder = BorderFactory.createLineBorder(Color.RED, 1);
ApplicationNameField.setBorder(newBorder);
ErrorCode.setText("Error field cant be empty");
}
if (ApplicationHostField.getText().equals("")) {
Border newBorder = BorderFactory.createLineBorder(Color.RED, 1);
ApplicationHostField.setBorder(newBorder);
ErrorCode.setText("Error field cant be empty");
}
// here i would change border to start normal color (color black)
try {
TimeUnit.SECONDS.sleep(5);
Border newBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
ApplicationNameField.setBorder(newBorder);
Border newBorder2 = BorderFactory.createLineBorder(Color.BLACK, 1);
ApplicationHostField.setBorder(newBorder2);
} catch (InterruptedException ex) {
System.out.print(ex.getMessage());
}
在此代码之后,我的JTextFields边框是黑色的,并且没有用红色着色。
我也尝试过:
Thread.sleep(5000);
但效果是一样的。有人可以帮忙吗?
答案 0 :(得分:4)
您需要使用javax.swing.Timer
在5秒后触发所需的操作。 Thread.wait
(或任何其他类型的等待)没有按预期工作,因为在线程返回主事件循环之前不会绘制任何内容。
答案 1 :(得分:1)
同意@ammoQ。请提供一个使用javax.swing.Timer的示例。
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.AbstractBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class TimerTest implements ActionListener{
JTextField textField;
public void createUI(){
JFrame frame = new JFrame("Timer Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
MainPanel mainPanel = new MainPanel();
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
frame.add(mainPanel,BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(5 * 1000, this);
timer.start();
}
public static void main(String[] args) {
TimerTest timerTest = new TimerTest();
timerTest.createUI();
}
@SuppressWarnings("serial")
class MainPanel extends JPanel{
public MainPanel(){
textField = new JTextField();
textField.setColumns(30);
textField.setBorder(BorderFactory.createCompoundBorder(
new LineBorder(Color.red, 3),
new EmptyBorder(new Insets(15, 25, 15, 25))));
add(textField,BorderLayout.CENTER);
setBackground(new Color(211,211,211));
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setBorder(BorderFactory.createCompoundBorder(
new LineBorder(Color.black, 3),
new EmptyBorder(new Insets(15, 25, 15, 25))));
}
}
答案 2 :(得分:0)
您可以使用SwingWorker
执行此操作。请参阅下面的运行示例。
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
public class JTextFieldTest extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTextFieldTest frame = new JTextFieldTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JTextFieldTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 298, 220);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textField = new JTextField();
textField.setColumns(10);
textField.setBorder(BorderFactory.createLineBorder(Color.RED));
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
// Simulate doing something useful.
Thread.sleep(5000);
textField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return null;
}
};
worker.execute();
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(
Alignment.LEADING).addGroup(
gl_contentPane
.createSequentialGroup()
.addGap(87)
.addComponent(textField, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE)));
gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(
Alignment.LEADING).addGroup(
gl_contentPane
.createSequentialGroup()
.addGap(70)
.addComponent(textField, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE)));
contentPane.setLayout(gl_contentPane);
}
}