gpt4 book ai didi

java - 动态显示附加文本的 TextArea

转载 作者:行者123 更新时间:2023-12-01 13:06:00 25 4
gpt4 key购买 nike

我想执行此任务,以便在每个任务执行时附加已存在的 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);
}
}

最佳答案

您的嵌套类需要外部类的实例,因为它不是静态的 - 但您没有外部类的实例。

尝试使嵌套类静态

static  class Task extends SwingWorker<Void, Void>

同时将此字段设为静态

private static  JTextArea logText = new JTextArea();

关于java - 动态显示附加文本的 TextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23267885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com