تحتاج JButtons إلى تعديل 8 JTextFields باستخدام صفيف. استمع إلى الأزرار أو النص؟
-
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 لكل زر. في مجموعة ActionPerformed الخاصة بـ ActionListener ، احصل على العنصر لعرضه ونقله إلى طريقة ستكون مسؤولة عن تعيين القيم على حقول النص.
شيء مثل:
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