0

这是作业,这个问题扩展了这个

所以有一个按钮First, Prev, Next, 和Last

每个都应该修改

Item ID, Name, Rating, Price, Units, Value, Fee, ValueW/Fee, Total Inventory Value 

最后一个是所有单位的静态总和。

我不确定是否应该让每个按钮都像这样进行多次调用。

productName.setText( product.getProductName() );
itemNumber.setText( String.valueOf( product.getItemNumber() ) );

或者让每个 JTextArea 监听按钮,然后更改其字段。这还有效吗?

4

1 回答 1

1

为每个按钮注册一个 ActionListener。在该 ActionListener 的 actionPerformed 方法的主体中,获取要显示的项目并将其传递给负责将值设置到文本字段的方法。

就像是:

JButton button = new JButton("Next");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        DVDObject obj = getNextDVD();
        populateFields(obj);
    }
});

...

private DVDObject getNextDVD() {
    // gets the next object to display
    // you could call this method for each of the buttons, 
    // passing in an argument that determines which Object
    // to return (first, last, next, previous, whatever)
}

private void populateFields(DVDObject dvd) {
    // write out the values from the object passed in to the
    // fields
}

I'm guessing you've got some kind of collection of objects that contain all the information about DVDs, I've taken a stab in the dark and called it "DVDObject" here.

于 2009-09-06T22:36:51.847 回答