我有一个JPanel和一个名为consoleOutput的元素。我将JPanel作为动作监听器中的方法中的参数,允许我从另一个类打印到控制台。但是,每当我尝试打印到consoleOutput时,它都不会打印,直到我的其他类中的方法结束。这是我的代码的样子: 我的MainPanel课程:
public class MainPanel extends JPanel{
private JButton submitButton;
JTextArea consoleOutput;
public MainPanel(){
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
setLayout(null);
setBackground(Color.WHITE);
Font f1 = new Font("Arial", Font.PLAIN, 14);
submitButton = new JButton("Get Cards");
submitButton.setBounds(35, 285, 107, 49);
submitButton.setFont(f1);
consoleOutput = new JTextArea();
consoleOutput.setBounds(199, 122, 375 , 210);
consoleOutput.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(3, 4, 0, 0)));
consoleOutput.setEditable(false);
consoleOutput.setFont(f1);
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String username = "Hank";
String password = "passfunnylolxD";
Cards cards = new Cards();
cards.openTabs(username, password, MainPanel.this);
}
});
add(submitButton);
add(consoleOutput);
}
}
我的卡类:
public class Cards{
private void printToConsole(String text, MainPanel panel){
panel.consoleOutput.append(text);
panel.consoleOutput.updateUI();
}
public class Cards{
public void openTabs(String username, String password, MainPanel panel){
printToConsole(username, panel); //This will be printed out 5 seconds after it is printed to the actual console
try {
TimeUnit.MILLISECONDS.sleep(5000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(username); //This will be printed out 5 seconds before it is printed to the textArea console
}
}
我不确定为什么会这样做。为什么不更新。每当我想要打印东西时,我应该怎么做才能更新它?谢谢,所有的帮助表示赞赏!!