واجهة المستخدم الرسومية في جافا، الطبقات الخاصة للمستمع لا تعمل

StackOverflow https://stackoverflow.com/questions/3698195

سؤال

أحاول إنشاء واجهة مستخدم رسومية في Java، باستخدام شيء على هذا المنوال:

public class GUIApp
{
    DrawingPanel dp;
    buttonPanel bp;
    ova = Oval;
 public GUIApp()
    {
        JFrame win = new JFrame("Drawing App");
        dp = new DrawingPanel();
        bp = new buttonPanel(this);

    //Settings for panels and frame

        ova = new Oval(100,100,100,100);

      }
      public void setOval(int c){
        //Change color of oval
      }
  }

ثم في صفي ButtonPanel لدي هذا:

public class ButtonPanel extends JPanel
{

    private JButton btnRed, btnGreen, btnBlue;

    public ButtonPanel(GUIApp d)
    {

        ButtonListener listener = new ButtonListener();
        btnRed = new JButton("Red");
        btnGreen = new JButton("Green");
        btnBlue = new JButton("Blue");

        btnRed.addActionListener(listener);
        btnGreen.addActionListener(listener);
        btnBlue.addActionListener(listener);

        setBackground(Color.lightGray);
        GridLayout grid = new GridLayout(3,1);
        add(btnRed,grid);
        add(btnGreen,grid);
        add(btnBlue,grid);
    }

    private class ButtonListener implements ActionListener{

        public void clickButton(ActionEvent event) {
            Object location = event.getSource();
            if (location == btnRed){
                d.setOval(1);
            }
            else if(location == btnGreen){
                d.setOval(2);
            }
            else if(location == btnBlue){
                d.setOval(3);
        }
        }

}
}

لكن netbeans يعطي خطأً لفئة ButtonListener الداخلية، ولا أعرف السبب.لا أعرف أيضًا كيفية استدعاء setOval بشكل صحيح من داخل تلك الفئة إلى فئة GUIApp.ما الخطأ الذي افعله؟

هل كانت مفيدة؟

المحلول

المشكلة هي أنه عند التنفيذ ActionListener يجب عليك تحديد الطريقة actionPerformed(ActionEvent e);أنت لم تفعل ذلك في الخاص بك ButtonListener فصل.لا يمكنك تسمية الطريقة بأي شيء تريده (كما فعلت مع clickButton)، لذلك يجب عليك فقط إعادة تسمية ملفك clickButton طريقة ل actionPerformed (والمضي قدما وإضافة @Override الشرح أيضا).

الآن من أجل الاتصال d.setOval من داخل طبقتك الداخلية، d يجب أن يكون في النطاق عندما actionPerformed تسمى الطريقة .هناك طريقتان لتحقيق ذلك:هل يمكن أن تجعل d متغير عضو في صفك، أو يمكنك تحديد الخاص بك ButtonListener كفئة مجهولة.

على سبيل المثال، إذا قمت بحفظها d كمتغير عضو فإن الكود الخاص بك سيبدو كما يلي:

public class ButtonPanel {
 private GUIApp d;

 public ButtonPanel(GUIApp d) {
  this.d = d;
  // The rest of your code here...
 }
}

أو يمكنك استخدام فئة مجهولة مثل هذا:

public ButtonPanel(GUIApp d) {
 ActionListener listener = new ActionListener(){
  @Override
  public void actionPerformed(ActionEvent event) {
   Object location = event.getSource();
   if (btnRed.equals(location)) {
    d.setOval(1);
   } else if (btnGreen.equals(location)) {
    d.setOval(2);
   } else if (btnBlue.equals(location)) {
    d.setOval(3);
   }
  }
 };
 // The rest of your constructor code here ...
}

ملحوظة: لاحظ كيف قمت أيضًا بتغيير استخدام == ل equals() للمساواة بين الكائنات.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top