java - 使用按钮继承

标签 java swing oop methods

我有一个作业,其中有两个 JPanel,我需要能够按下 jpanel1 中的 button1 并获取要在 jpanel2 的 button2 上更改的信息。我已经解决这个问题几天了,无论我尝试什么,我都会陷入困境。

我的想法是,我需要一种方法将 jpanel2 中的按钮转换为 jpanel 中的按钮,一旦按下 jpanel1 中的按钮,该按钮就会发生变化。问题是我不知道如何获得使用按钮的方法。

这是我到目前为止所拥有的。

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

最佳答案

抱歉,但我认为您收到了不好的建议,因为人们告诉您将字段公开以允许其他类访问它。是的,这会起作用,但从长远来看,这样做可能是一件危险的事情,因为它会让你的类(class)出现不需要的和未知的副作用。更好地通过将字段设置为私有(private)来保护您的字段,并且只允许外部类能够更改您想要更改的状态,而不要通过为您的类提供公共(public)方法来执行此操作。例如,如果您有这样的类(class):

// 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,而是与类间通信和面向对象编程基础知识有关.

关于java - 使用按钮继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19125373/

相关文章:

java - 将列表的第一个元素输入数组的最快方法

java - 授予特定 Java Swing 应用程序完全权限

Java IDE Dialox Box 被遮挡(Ubuntu 上的 Intellij)

java - 将 Action 发送给正在监听它的按钮

python - 如何将某些内容添加到属性中?

java - 在Eclipse中配置构建路径问题-Java

java - 测量花费在 GC 上的时间

java - 能够声明一个对象但不能实例化它有什么意义

python - 在对象定义中使用 "()"和不使用它有什么区别?

java - 如何将数据附加到现有 Dropbox 文件?