Jbutton将需要使用数组来修改8个JTextField的。听按钮或文本?
-
21-09-2019 - |
题
这是家庭作业,并且该问题延长这一个
因此,有用于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中监听按钮,然后改变它的字段。确实,即使是工作?
解决方案
注册为每个按钮一个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
}
我猜你已经有了某种包含所有关于DVD的信息对象的集合,我已经采取了在黑暗中刺伤,并把它称为“DVDObject”在这里。
不隶属于 StackOverflow