2015-10-23 112 views
0

即時嘗試添加一個ActionListener到我的按鈕爲我的計算器我正在做。問題是當我嘗試創建一個ActionListener時,Im被顯示錯誤。我在一個類中嘗試過,然後我創建了一個監聽器類來查看是否有幫助。這裏是我的代碼:Java不能實例化類型ActionListener

package main; 

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public abstract class Main extends JFrame implements Listener{ 
public static void main(String[] args) throws IOException{ 
//Main variables 
String dis = "0"; 
double ans = Double.parseDouble(dis); 


//Making frames 
JFrame frame = new JFrame("Calculator"); 
JPanel panel = new JPanel(); 

//Buttons 
JButton enter = new JButton("Enter"); 
JButton sub = new JButton("-"); 
JButton add = new JButton("+"); 
JButton div = new JButton("÷"); 
JButton mult = new JButton("*"); 
JTextField text = new JTextField(dis); 

//Font 
Font bigFont = text.getFont().deriveFont(Font.PLAIN, 30f); 
Font butf = text.getFont().deriveFont(Font.PLAIN, 20f); 
//Methods 
panel.setLayout(new FlowLayout()); 
panel.add(text); 
panel.setSize(590, 100); 
text.setColumns(22); 
text.setFont(bigFont); 
text.setHorizontalAlignment(JTextField.RIGHT); 
text.setEditable(false); 
enter.setForeground(Color.RED); 
sub.setForeground(Color.RED); 
div.setForeground(Color.RED); 
mult.setForeground(Color.RED); 
add.setForeground(Color.RED); 
//Buttons Methods 
enter.setBounds(470, 450, 100, 150); 
sub.setBounds(470, 350, 100, 90); 
div.setBounds(470, 250, 100, 90); 
mult.setBounds(470, 150, 100, 90); 
add.setBounds(470, 50, 100, 90); 
enter.setFont(butf); 
sub.setFont(butf); 
div.setFont(butf); 
mult.setFont(butf); 
add.setFont(butf); 
//Frame 
frame.add(div); 
frame.add(mult); 
frame.add(sub); 
frame.add(enter); 
frame.add(add); 
frame.add(panel); 
frame.setSize(600, 650); 
frame.setLocationRelativeTo(null); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 


//extra 
text.setSize(1000, 100); 


//Actions 

}}

package main; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 

public interface Listener extends ActionListener { 
//Throwing error here 'Cant instantiate the type ActionListener' 
ActionListener al = new ActionListener(); 
public default void actionPerformed(ActionEvent e){ 

    } 



} 

任何人都知道如何解決這個問題?

+1

你正在創建ActionListener'的'一個實例,這是一個界面內的'interface'? – MadProgrammer

+0

您也將對佈局結果非常失望,請查看[佈置容器中的組件](http://docs.oracle.com/javase/tutorial/uiswing/layout/index。 html)以獲得更好的解決方案 – MadProgrammer

回答

1

要聲明一個ActionListener使用

public class Listener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     //dostuff 
    } 
} 
1
  • ActionListenerinterface,它不能被實例化,沒有提供一個具體的實現它的合同
  • interface s不能包含實例字段

簡而言之,它應該看起來更像...

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public interface Listener extends ActionListener { 

    public default void actionPerformed(ActionEvent e) { 
    } 

} 

你也可以想有在

在Swing中,你應該確保你的UI是隻能在事件分派線程的上下文中創建和操作。看看Initial Threads更多細節

你也將是非常失望佈局的結果,看看Laying Out Components Within a Container更好的解決方案

+0

您應該聲明一個類Listener,而不是一個接口Listener – phflack

+0

@phflack不,您不必將它放入您的IDE並查看。您可能還想看看[默認方法](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html)。我並不是說OP所做的是正確的,但是他們試圖根據他們提供的代碼嘗試去做;) – MadProgrammer

+0

似乎是錯誤的方法,因爲它是ActionListener中唯一的方法,如果你試圖向它添加功能或更新它會更有意義,但是接口在那裏告訴其他人實現該功能。我想你希望能夠使用它來讓Foo類實現Listener,並且它可以在不需要做更多事情的情況下獲得功能。需要考慮它將代碼段分成多少。 – phflack