在我看来,我正在尝试访问 JPanel 上的 JButton。我的 View 类位于一个名为 project 的包中。它本质上是一个 JFrame,其中包含一些来自名为“views”的包的 JPanel。当我在 Controller 类中使用 View 时,我可以访问面板,但我无法再向下访问按钮。我尝试将其中一个按钮更改为公开,但仍然无法访问它。我还有一个 getter 函数,但这也不给我访问权限。我在下面包含了代码和项目结构。
项目结构
Project
> src
> project
> View.java
> Controller.java
> views
> JButtonPanel.java
项目/View.java
package project;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import config.Config;
import views.JButtonPanel;
import views.JColorPanel;
import views.JImagePanel;
import views.JMaskPanel;
public class View {
protected JFrame jf;
protected JPanel jButtonPanel;
protected JPanel jImagePanel;
protected JPanel jMaskPanel;
protected JPanel jColorPanel;
View() {
BorderLayout bl = new BorderLayout();
this.jf = new JFrame();
this.jf.setTitle("Gradient coloring app");
this.jf.setPreferredSize(new Dimension(Config.screenSize.width, Config.screenSize.height));
this.jf.setLayout(bl);
this.jButtonPanel = new JButtonPanel();
this.jImagePanel = new JImagePanel(null);
this.jColorPanel = new JColorPanel();
this.jMaskPanel = new JMaskPanel();
this.jf.add(this.jButtonPanel, BorderLayout.PAGE_START);
this.jf.add(this.jImagePanel, BorderLayout.LINE_START);
this.jf.add(this.jColorPanel, BorderLayout.LINE_END);
this.jf.add(this.jMaskPanel, BorderLayout.PAGE_END);
this.jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.jf.setResizable(false);
this.jf.pack();
this.jf.setVisible(true);
}
/**
* @return the jButtonPanel
*/
public JPanel getjButtonPanel() {
return jButtonPanel;
}
/**
* @return the jImagePanel
*/
public JPanel getjImagePanel() {
return jImagePanel;
}
/**
* @return the jMaskPanel
*/
public JPanel getjMaskPanel() {
return jMaskPanel;
}
/**
* @return the jColorPanel
*/
public JPanel getjColorPanel() {
return jColorPanel;
}
/**
* @param jButtonPanel the jButtonPanel to set
*/
public void setjButtonPanel(JPanel jButtonPanel) {
this.jButtonPanel = jButtonPanel;
}
/**
* @param jImagePanel the jImagePanel to set
*/
public void setjImagePanel(JPanel jImagePanel) {
this.jImagePanel = jImagePanel;
}
/**
* @param jMaskPanel the jMaskPanel to set
*/
public void setjMaskPanel(JPanel jMaskPanel) {
this.jMaskPanel = jMaskPanel;
}
/**
* @param jColorPanel the jColorPanel to set
*/
public void setjColorPanel(JPanel jColorPanel) {
this.jColorPanel = jColorPanel;
}
}
View /JButtonPanel.java
package views;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JPanel;
import config.Config;
@SuppressWarnings("serial")
public class JButtonPanel extends JPanel {
protected JButton targetButton;
protected JButton colorSource;
protected JButton calGrad;
protected JButton addColorsToGradient;
public JButtonPanel() {
FlowLayout fl = new FlowLayout();
this.setPreferredSize(new Dimension(Config.screenSize.width, (int) (Config.screenSize.height * 0.075)));
this.setBorder(Config.buttonPanelBorder);
this.setLayout(fl);
this.targetButton = new JButton();
this.targetButton.setText("Load a target image");
this.add(this.targetButton);
...
}
/**
* @return the targetButton
*/
public JButton getTargetButton() {
return targetButton;
}
...
}
如有任何帮助,我们将不胜感激。谢谢
最佳答案
问题是您将自定义面板称为 JPanel,而不是自定义 JButtonPanel:
...
public class View {
protected JFrame jf;
protected JPanel jButtonPanel;
protected JPanel jImagePanel;
protected JPanel jMaskPanel;
protected JPanel jColorPanel;
View() { ...
那么您无权访问自定义 JPanel 扩展中定义的按钮是正常的,因为您将其声明为标准 JPanel,而实际上它没有 getTargetButton()
方法。
要解决这个问题,您有两种选择:
- 将 jButtonPanel 声明为
protected JButtonPanel jButtonPanel
- 当您需要访问其字段时,将 jButtonPanel 转换为 JButtonPanel(如果使用 instanceof 检查它实际上是一个 JButtonPanel 对象会更好)。
如果您将来不打算替换 jButtonPanel 实现,我建议您选择第一个选项。
阿莱西奥
关于java - 将 ActionListener 添加到嵌套在另一个包中的 JPanel 上的 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780639/