我想执行此任务,以便在每个任务执行时,已经存在的JTextArea logText中的一些文本行被追加。
错误就是这一行:
task = new Task();
需要做什么才能获得一个动态显示附加文本的JTextArea。
public class ScanAccount extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JTable table;
private JTextArea logText = new JTextArea();
private static Task task;
private static String statusText;
class Task extends SwingWorker<Void, Void> {
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() {
logText.append(statusText + "\n");
logText.setCaretPosition(logText.getDocument().getLength()-1);
return null;
}
/*
* Executed in event dispatching thread
*/
@Override
public void done() {
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
ScanAccount frame = new ScanAccount();
frame.setVisible(true);
while(true)
{
Random random = new Random();
statusText = "new text is " + random.nextInt(10000);
task = new Task();
task.execute();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ScanAccount() {
setTitle("Scanning Mode : Email Accounts");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 440);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblAccountInformation = new JLabel("Account Information :");
lblAccountInformation.setBounds(12, 12, 168, 15);
contentPane.add(lblAccountInformation);
JLabel lblUsername = new JLabel("Username :");
lblUsername.setFont(new Font("Dialog", Font.PLAIN, 12));
lblUsername.setBounds(34, 37, 88, 15);
contentPane.add(lblUsername);
textField = new JTextField();
textField.setBounds(113, 35, 285, 19);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblPassword = new JLabel("Password :");
lblPassword.setFont(new Font("Dialog", Font.PLAIN, 12));
lblPassword.setBounds(34, 66, 88, 15);
contentPane.add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(113, 64, 285, 19);
contentPane.add(passwordField);
JButton btnStartScan = new JButton("Start Scan");
btnStartScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnStartScan.setBounds(308, 94, 117, 25);
contentPane.add(btnStartScan);
JLabel label_2 = new JLabel("Summary :");
label_2.setBounds(22, 185, 95, 15);
contentPane.add(label_2);
table = new JTable(new DefaultTableModel(
new Object[][] {
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
{null, null},
},
new String[] {
"File Name", "Categeory"
}
));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(77, 212, 312, 187);
contentPane.add(scrollPane);
JLabel label = new JLabel("Status :");
label.setBounds(25, 131, 70, 15);
contentPane.add(label);
JScrollPane scrollPane_status = new JScrollPane(logText);
scrollPane_status.setBounds(45, 152, 369, 21);
contentPane.add(scrollPane_status);
}
}
答案 0 :(得分:1)
您的嵌套类需要外部类的实例,因为它不是静态的 - 但您没有外部类的实例。
尝试使嵌套类静态
static class Task extends SwingWorker<Void, Void>
也使这个提交静态
private static JTextArea logText = new JTextArea();