我想爲項目編寫一個簡單的退休計算器。
我正在嘗試獲取文檔偵聽器,以便從不同的文本字段獲取輸入並將其用於我的計算。Document Listener java「無法實例化類型DocumentListener」
出於某種原因,我無法讓DocumentListener實例化,並且出現錯誤。我認爲我缺乏對如何做到這一點的理解。
那是給我的錯誤代碼的具體線路是這樣的:
DocumentListener DL = new DocumentListener();
這裏是一流的,我嘗試使用此:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class TextFields extends JPanel implements DocumentListener
{
public TextFields()
{
setLayout(new GridLayout(5,1));
setFont(new Font("Tahoma", Font.PLAIN, 14));
DocumentListener DL = new DocumentListener();
JTextField age = new JTextField("Age");
age.getDocument().addDocumentListener(DL);
JLabel ageLabel = new JLabel("Age: ");
JTextField initialSavings = new JTextField("Intial Savings");
JLabel ISLabel = new JLabel("Inital Savings: ");
JTextField ageRetire = new JTextField("Retirement Age");
JLabel RALabel = new JLabel("Retirement Age: ");
JTextField inflation = new JTextField("Inflation Rate");
JLabel inflationLabel = new JLabel("Inflation Rate: ");
JTextField dailySavings = new JTextField("Daily Savings");
JLabel DSLabel = new JLabel("Daily Savings: ");
JTextField DeathAge = new JTextField("Age Of Death");
JLabel DALabel = new JLabel("When will you die? ");
JTextField retirementIncome = new JTextField("Retirement Income:");
JLabel RILabel = new JLabel("Retirement Income: ");
JTextField interest = new JTextField("Interest Rate");
JLabel interestLabel = new JLabel("Interest: ");
add(ageLabel);
add(age);
add(ISLabel);
add(initialSavings);
add(RALabel);
add(ageRetire);
add(inflationLabel);
add(inflation);
add(DSLabel);
add(dailySavings);
add(DALabel);
add(DeathAge);
add(interestLabel);
add(interest);
add(RILabel);
add(retirementIncome);
}
@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
}
自從我開始程序設計師,我將不勝感激簡單的答案,不需要大量的知識才能理解。
非常感謝您的時間和幫助! Roy。