-1

不久前我问过一个类似的问题,所以如果我这样做是在滥用网站,我很抱歉问这个问题,但这个问题有点不同。我有一个 Main 类,我在其中创建了一个 JFrame 和一个 JPanel。为了避免混乱,我创建了另一个名为“Buttons”的类来保存我所有的 JButton。我想将我的 JButton 添加到 mainPanel(我的 JPanel),但是我无法从 Button 的类继承对 mainPanel 的访问。

这是我的主要课程:

package main;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
    public Main() {
        Main m = new Main();
        //The main window
        JFrame Main = new JFrame("Don't Blink");
        Main.setSize(500,500);
        Main.setResizable(false);
        Main.setVisible(true);
        Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //The main window's panel
        JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
        mainPanel.setSize(500, 500);
        mainPanel.setVisible(true);
        Main.add(mainPanel);
    }

    public static void main(String[] args) {
         Main m = new Main();
        }
}

按钮类:

package main;

import javax.swing.JButton;

public class Buttons extends Main {
    public Buttons(Main m) {
        //The button to get a job
        JButton workButton = new JButton("Get a job");
        mainPanel.add(workButton);//The mainPanel here gives me
                                      //the error "mainPanel can
                                      //not be resolved". It 
                                      //doesn't seem to recoginze
                                      //mainPanel
    }
}
4

3 回答 3

1

这里有几个问题......首先这是一个糟糕的 OOP 设计,但让我们看看......

您应该将 mainPanel 作为类变量添加

public class Main extends JPanel {
  JPanel mainPanel;

现在开始你不声明类型,因为你想把它放在一个类范围内:

this.mainPanel = new JPanel(); // I want to add buttons to this panel from the Buttons class

您试图在 Buttons 中调用它,但您只在 Main 构造函数中本地声明它。

您在 new Main 中调用 new Main,我相信这将以无限递归结束。

remove Main m = new Main(); from your Main constructor.

I don't think that inherit Main on Buttons is good, but if you really need that, Main does not know about Buttons, anyway you could use the main method in Buttons instead of Main. I'd remove the main method from Main class and add it to Buttons class instead since it is extending Main anyway.

  public static void main(String[] args) {
    Buttons m = new Buttons();
  }

Now, buttons claims to need a Main as argument, that is weird because you want to call the Main, not another Main, I'd remove the argument Main from Buttons constructor. You may think that then you will not have mainPanel but Buttons is already a Main class so it has mainPanel too.

于 2013-10-02T18:52:25.827 回答
0

从 OOP 的角度来看,这有点奇怪,而且似乎不正确。

在我看来,你不应该为此创建一个单独的类,而只是在类内部创建一个方法,Main并将面板的引用传递给它:

public class Main extends JPanel{

    public Main(){

        Main m = new Main();

            //The main window
            JFrame Main = new JFrame("Don't Blink");
            Main.setSize(500,500);
            Main.setResizable(false);
            Main.setVisible(true);
            Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //The main window's panel
            JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
            mainPanel.setSize(500, 500);
            mainPanel.setVisible(true);
            Main.add(mainPanel);

            addTheButtons(mainPanel); //this does the heavy lifting 
    }

    private addTheButtons(JPanel mainPanel){

        //The button to get a job
        JButton workButton = new JButton("Get a job");
        mainPanel.add(workButton); 

    }
}

只要您不想重用在其他类中创建按钮的逻辑,这将起作用 - 但就我的经验而言,这种 UI 代码粒度不会得到太多重用......

于 2013-10-02T18:47:49.383 回答
0

首先,用大写字母命名变量名是不好的做法,比如 Main。使用驼峰式。

请不要为此使用继承。我建议你定义一个方法如下:

private Container getButtonContainer() {
    final JPanel buttonPanel = new JPanel(...);

    ...

    buttonPanel.add(button1);
    buttonPanel.add(button2);

    return buttonPanel;
}

然后将此面板添加到主面板。

于 2013-10-02T18:52:06.690 回答