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