无法访问内部 class 变量
Cannot get access to inner class variables
我无法访问内部 class "listPanel" 对象中的变量。
有变量 "tutajcos" 但我无法从 CosView class.
中的其他方法访问
有什么问题? Eclipse 没有提示我任何东西
package cos.view;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import util.Model;
import util.View;
import javax.swing.*;
import cos.controller.CosController;
import cos.model.CosModel;
public class CosView extends View implements ActionListener {
private JPanel buttonsPanel;
private JPanel listPanel;
private CosModel theModel;
private CosController theController;
public CosView(CosController theController, CosModel theModel) {
this.theModel = theModel;
this.theController = theController;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
buildGui();
}
});
}
private void buildGui() {
setTitle("Program GUI");
listPanel = new ListPanel();
buttonsPanel = new ButtonPanel();
add(buttonsPanel, BorderLayout.NORTH);
add(listPanel, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
registerWithModel(theModel);
}
class ButtonPanel extends JPanel {
JButton refreshButton = new JButton("Refresh");
JTextField adresField = new JTextField("tutaj link", 10);
public ButtonPanel() {
refreshButton.addActionListener(CosView.this);
add(refreshButton);
add(adresField);
}
}
class ListPanel extends JPanel {
JTextField tutajcos;
public ListPanel() {
tutajcos = new JTextField(8);
add(tutajcos);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
theController.processUserAction(action);
}
@Override
public void update(Observable o, Object arg) {
System.out.println("Updating interface");
if (o instanceof CosModel) {
String content;
//there is a problem-------------
listPanel.tutajcos.setText("siema");
}
}
}
问题不在于访问修饰符,而在于继承。您的 listPanel
变量声明为 JPanel
类型,其中没有名为 tutajcos
的可访问字段。
为了能够以您尝试的方式访问它,您需要将 listPanel 声明为 ListPanel:
private ListPanel listPanel;
或调用前施法:
((ListPanel)listPanel).tutajcos.setText("siema");
我无法访问内部 class "listPanel" 对象中的变量。 有变量 "tutajcos" 但我无法从 CosView class.
中的其他方法访问有什么问题? Eclipse 没有提示我任何东西
package cos.view;
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import util.Model;
import util.View;
import javax.swing.*;
import cos.controller.CosController;
import cos.model.CosModel;
public class CosView extends View implements ActionListener {
private JPanel buttonsPanel;
private JPanel listPanel;
private CosModel theModel;
private CosController theController;
public CosView(CosController theController, CosModel theModel) {
this.theModel = theModel;
this.theController = theController;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
buildGui();
}
});
}
private void buildGui() {
setTitle("Program GUI");
listPanel = new ListPanel();
buttonsPanel = new ButtonPanel();
add(buttonsPanel, BorderLayout.NORTH);
add(listPanel, BorderLayout.CENTER);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 600);
registerWithModel(theModel);
}
class ButtonPanel extends JPanel {
JButton refreshButton = new JButton("Refresh");
JTextField adresField = new JTextField("tutaj link", 10);
public ButtonPanel() {
refreshButton.addActionListener(CosView.this);
add(refreshButton);
add(adresField);
}
}
class ListPanel extends JPanel {
JTextField tutajcos;
public ListPanel() {
tutajcos = new JTextField(8);
add(tutajcos);
}
}
@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
theController.processUserAction(action);
}
@Override
public void update(Observable o, Object arg) {
System.out.println("Updating interface");
if (o instanceof CosModel) {
String content;
//there is a problem-------------
listPanel.tutajcos.setText("siema");
}
}
}
问题不在于访问修饰符,而在于继承。您的 listPanel
变量声明为 JPanel
类型,其中没有名为 tutajcos
的可访问字段。
为了能够以您尝试的方式访问它,您需要将 listPanel 声明为 ListPanel:
private ListPanel listPanel;
或调用前施法:
((ListPanel)listPanel).tutajcos.setText("siema");