0

I have an assignment where I have two JPanels and I need to be able to push button1 in jpanel1 and get information to change on button2 of jpanel2. I've been working on the problem for a few days and no matter what I try I get stuck.

My idea is that I need a method to convert the button in jpanel2 to a button in jpanel that changes once the button in jpanel1 is pushed. Thing is that I can't figure out how to get a method to work with a button.

Here's what I have so far.

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");                               
    }
}     

panel1

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,但这似乎是您分配的要求,所以我把它留在了原处。

顺便说一句,您的问题标题“使用按钮的继承”可能有点误导,因为您的问题不是关于继承,您不是在扩展您的 JButton,而是关于类间通信和面向对象的编程基础。

于 2013-10-01T21:39:08.630 回答
0

这可能会帮助您:

我的JPanel1

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"); 
            }
        }); 
    }
}

我的JPanel2

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 回答
0

看起来你在这里陷入了混乱——如果你真的想以这种方式访问​​这些按钮,那么为什么不在课堂上公开它们。

这样您就可以直接参考面板上的按钮,详情请参阅:http ://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 。

于 2013-10-01T20:44:38.337 回答