0

JPanel が 2 つあり、jpanel1 のボタン 1 を押して、jpanel2 のボタン 2 で変更する情報を取得できるようにする必要があります。私は数日間この問題に取り組んできましたが、何をしようとしても行き詰まります。

私の考えは、jpanel2のボタンをjpanel1のボタンが押されると変更されるjpanelのボタンに変換する方法が必要だということです。ボタンを操作するメソッドを取得する方法がわかりません。

これが私がこれまでに持っているものです。

MyJPanel:

public class myJPanel extends JPanel { 
    public myJPanel(){           
        super();
        JButton j2 = new JButton("..");

        setBackground(Color.gray);      
        setLayout(new BorderLayout());

        myJPanel1 p1 = new myJPanel1(JButton(j2)); // thought something

        //like this would work to pass jpanel2 to jpanel1 using
        //myjpanel as the common class
        add(p1,"North");
        myJPanel2 p2 = new myJPanel2(j2);
        add(p2,"Center");                               
    }
}     

パネル1

public class myJPanel1 extends JPanel implements ActionListener {   
    student st1 = new student("Fred","Fonseca",44);

    JButton j = new JButton(st1.getInfo());
    JButton b1 = new JButton("..");


    public myJPanel1(JButton j2) {
        super();
        setBackground(Color.yellow);

        // the whatsUp of this student has to shown in the other panel
        j.addActionListener(this); 
        add(j);
        add(b1);             
    }

    public void actionPerformed(ActionEvent event) {

        Object obj = event.getSource();
        //=====================================

        if (obj == j){            
            j2.setText(st1.whatsUp()); // Output on JButton in JPanel2 
            b1.setText(st1.whatsUp());
        }      
    }
}

JPanel2

public class myJPanel2 extends JPanel {
    JButton j1 = new JButton("When the user clicks on the button in the UPPER panel" );

    public void setButton(JButton j2){
        j1 = j2; // shouldn't this pass convert the information between the classes?
    }

    public myJPanel2(JButton j1){
        super();
        setBackground(Color.pink);
        setLayout(new GridLayout(3,1));
        add(j1);
        j1.setText("");
    }    
}
4

3 に答える 3

1

申し訳ありませんが、別のクラスがアクセスできるようにフィールドを公開するように人々が言っ​​ているので、あなたは悪いアドバイスを受けていると思います。はい、これは機能しますが、クラスに望ましくない未知の副作用が発生する可能性があるため、長期的には危険なことになる可能性があります. プライベートにすることでフィールドを保護し、変更したい状態を変更する機能のみを外部クラスに許可し、クラスにこれを行うためのパブリックメソッドを与えることで、それ以外は許可しない方がよいでしょう。たとえば、次のようなクラスがあるとします。

// you'll need imports here

public class MyPanel extends JPanel {
   private JButton button = new JButton();

   public MyPanel(String name) {
      add(button);
      setBorder(BorderFactory.createTitledBorder(name));
   }

   public void setButtonAction(Action action) {
      button.setAction(action);
   }

   public void setButtonText(String text) {
      button.setText(text);
   }
}

次に、外部クラスは、ボタンのアクション (より堅牢な ActionListener と考えてください) とそのテキストを設定できます。

次に、このクラスを次のように使用できます。

import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class ChangingButtons extends JPanel {

   public ChangingButtons() {
      final MyPanel panel1 = new MyPanel("Panel 1");
      final MyPanel panel2 = new MyPanel("Panel 2");
      panel2.setButtonText("Button 2");

      panel1.setButtonAction(new AbstractAction("Button 1") {
         @Override
         public void actionPerformed(ActionEvent e) {
            panel2.setButtonText("Foobar!");
         }
      });

      add(panel1);
      add(panel2);
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("ChangingButtons");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ChangingButtons());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

私のクラスは不必要に JPanel を拡張するため、この例でもあまりにも多くのことを公開していることに注意してください。

余談ですが、質問のタイトル「ボタンを使用した継承」は、問題が継承に関するものではなく、JButtons を拡張するのではなく、クラス間通信とオブジェクト指向プログラミングの基礎に関するものであるため、少し誤解を招く可能性があります。

于 2013-10-01T21:39:08.630 に答える
0

これはあなたを助けるかもしれません:

MyJPanel1

public class MyJPanel1 extends JPanel { 

    private JButton localButton;

    private JButton remoteButton;

    public MyJPanel1(JButton localButton, JButton remoteButton){
        this.localButton= localButton;
        this.remoteButton = remoteButton;

        //add localButton to the JPanel

        //you can do, whatever you want with the remote (other panel) button.
        this.localButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event) {
                this.remoteButton.setText("I can reach you Button2"); 
            }
        }); 
    }
}

MyJPanel2

public class MyJPanel2 extends JPanel { 

    private JButton localButton;

    private JButton remoteButton;

    public MyJPanel2(JButton localButton, JButton remoteButton){
        this.localButton = localButton;
        this.remoteButton = remoteButton;

        //add localButton to the JPanel

        //you can do, whatever you want with the remote (other panel) button.
        this.localButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event) {
                this.remoteButton.setText("I can reach you Button1"); 
            }
        }); 
    }
}

そしてあなたのメインパネル。

public class MyJPanel extends JPanel { 
    public myJPanel(){           
        super();

        JButton j1 = new JButton("Button1");
        JButton j2 = new JButton("Button2");

        //Notice the order of parameters here (Local for one is remote for the other)
        MyJPanel1 p1 = new MyJPanel1(j1, j2);
        MyJPanel2 p2 = new MyJPanel2(j2, j1);

        setBackground(Color.gray);      
        setLayout(new BorderLayout());

        add(p1,"North");    
        add(p2,"Center");                               
    }
}     
于 2013-10-01T21:07:12.310 に答える