2015-11-28 80 views
0

我试图创建一个程序,它将从JTextField获取用户输入,并在单击JButton后将该输入添加到类CurrentAccount的对象中。到目前为止,我能够提出这个代码;通过GUI向用户输入参数添加用户输入java

jButton1.addActionListener(new ActionListener() {  
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 


     //Execute when button is pressed 
     String currentValue = jTextField1.getText() ; 
     int val = Integer.parseInt(currentValue); 
     balance = val; 
     theAccount = new CurrentAccount(balance); 
     System.out.println(theAccount.myBalance); 



    } 
}); 

但我在行中收到错误theAccount = new CurrentAccount(balance)。此外,我已经在方法外部实例化了Account,因为我将需要它作为类SavingsAccount的对象,因为它是从中继承的抽象类Account。

如果有帮助,我的CurrentAccount的代码如下;

public class CurrentAccount extends Account 
{ 
    private int myBalance; 
    private final ControlPanel myPane; 


    private int balance; 

    public CurrentAccount(ControlPanel myPane) 
    { 
     // balance= myBalance; 
     myBalance = myPane.getDimension(); 
     this.myPane=myPane; 
//  //super(balance); 
     //if (100 >= myPane) throw new IllegalArgumentException 
     //("A Savings Account can not have a balance of less than £100, you entered" + balance); 
    } 

任何帮助,这将非常感激。

+0

“我收到错误”什么错误? – resueman

+0

对不起,应该提到。在我提到的我即将收到的错误“incompatable类型:int不能转换为ControlPanel”错误 – brushbrushbrush

+0

对此问题已经有一个开放的问题,你忽略了一个问题,其评论。请不要重新提出问题,请不要忽视评论。此问题已被关闭。 –

回答

1

你有这个问题和这个代码有几个问题。首先你的编译错误消息指出:

“不兼容类型:INT不能转换为ControlPanel控制”

此错误消息被埋葬在评论也不是你的主要问题的一部分,使很多人很难看到。请避免将来再做这件事,而应将其作为您问题的重要部分。

错误消息告诉你什么是错的 - 你想创建一个新的CurrentAccount对象,但都传递一个int到它的构造:

theAccount = new CurrentAccount(balance); 

但是构造已被定义为不接受一个int,而是接受ControlPanel控制对象:

public CurrentAccount(ControlPanel myPane) { 

通常我会说,你要么需要改变构造带一个int,或者改变你怎么称呼它,所以你只传递一个ControlPanel参数 - 无论哪一个最有意义。但我不认为要么在这里是适当的。我猜测(我们不能肯定地说,因为我们对整体程序结构的了解不够),CurrentAccount实例已经存在,而不是从头开始创建一个新实例,您将要想要将余额信息传递到此实例中,如果存在方法可能使用setBalance(int balance)方法。

要获得更好和更详细的答案,请告诉我们更多关于您的程序结构和问题的信息。

+0

感谢man和对于转发的抱歉。在CurrentAccount类中的代码需要一个整数,但这意味着我只返回0。我试图创建一个setBalance方法,但我不确定在构造函数中使用ControlPanel的上下文中的方法结构。同样在我的MyFrame类中,我将控制面板添加到JFrame中,我设法让它获取用户输入并将其添加到CurrentAccount实例中,这是我希望的方式,但这只能通过JMenu和JMenu项目用户不太友好。 – brushbrushbrush