我一直在关注一些有关文字处理程序的教程,并且按照每个步骤进行操作,但是遇到此错误时我陷入了困境:
Display不是抽象的,并且不会覆盖Display中的抽象方法actionPerformed()
我用于Display的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextPane;
public class Display extends JPanel implements ActionListener {
private JTextPane textArea;
private JButton saveButton;
private JComboBox colorCombo;
private JComboBox fontCombo;
private JLabel processorLabel;
private JSlider fontSize;
//addAction Listener methods.
public void actionperformed(ActionEvent e){
}
// create arrays.
String[] colorItems = {"red", "Blue", "Green", "Purple", "Orange", "Black"};
String[] fontItems = {"Monospaced", "Serif", "San Serif"};
//constructor.
public Display(){
init(); // Display calls for init.
}
public void init(){ // where buttons and labels coding go.
//construct components.
textArea = new JTextPane();
saveButton = new JButton("save");
colorCombo = new JComboBox(colorItems);
fontCombo = new JComboBox(fontItems);
processorLabel = new JLabel("Mo's W.P");
fontSize = new JSlider(10, 30);
//Slider work.
fontSize.setOrientation(JSlider.HORIZONTAL);
fontSize.setMinorTickSpacing(1);
fontSize.setMajorTickSpacing(5);
fontSize.setPaintTicks(true); //displays the text.
fontSize.setPaintLabels(true); //displays paint pop ups
// Make text area presentable.
textArea.setBackground(Color.LIGHT_GRAY);
//textArea.setForeground(color);
//adjust size and layout.
setPreferredSize(new Dimension(817, 473));
setLayout(null);
//add components.
add(textArea);
add(saveButton);
add(colorCombo);
add(fontCombo);
add(processorLabel);
add(fontSize);
textArea.setBounds(10, 10, 650, 450);
saveButton.setBounds(670, 315, 140, 35);
colorCombo.setBounds(670, 205, 140, 53);
fontCombo.setBounds(670, 150, 140, 35);
processorLabel.setBounds(670, 20, 140, 35);
fontSize.setBounds(670, 95, 140, 40);
//add action listeners.
saveButton.addActionListener(this);
colorCombo.addActionListener(this);
fontCombo.addActionListener(this);
}
}
最佳答案
Java中的标识符区分大小写。
您的代码中有一个名为actionperformed
的方法,但是希望实现actionPerformed
。
如果我们的方法重写另一个方法或实现抽象方法声明(请注意:接口中的所有方法声明都是隐式抽象),则使用@Override
注释也是一种好习惯。如果方法确实覆盖另一个方法,则编译器可以执行其他检查,否则将产生错误。