Я пытаюсь выполнить несколько простых цепочек методов, но всегда получаю сообщение об ошибке «не могу найти символ».Например:
public JButton[] getSignOnButtons() {
return InitialFrame.getInitialPanel().getSignOnButtons();
}
Я реализую модель MVC, в пакете просмотра у меня есть 4 класса: View, InitialFrame, InitialPanel, NorthPanel.Чтобы мой контроллер мог взаимодействовать с пакетом View, я всегда прохожу класс View.
Моему классу контроллеров необходимо получить доступ к атрибутам классов View, как лучше?
Я «обманул» его ранее, обнародовав атрибуты всех классов View, чтобы я мог просто создать метод «get» из представления, например
return InitialFrame.InitialPanel.Buttons;
Спасибо за любую помощь.
Ошибка просто говорит, что "не удается найти символ" в каждом случае.
** РЕДАКТИРОВАНО с этой точки вниз ......
Это весь View Package:
public class View {
InitialFrame initialFrame;
public View(){
initialFrame = new InitialFrame();
}
public JFrame getInitialFrame() {
return initialFrame;
}
public InitialPanel getInitialPanel() {
return InitialFrame.getInitialPanel();
}
public JButton[] getSignOnButtons() {
return initialFrame.getInitialPanel().getSignOnButtons();
}
}
Это класс InitialFrame:
public final class InitialFrame extends JFrame {
private final InitialPanel initialPanel;
public InitialFrame() {
super("Welcome to the Sign-on Screen");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(700, 700);
this.setLayout(new GridLayout(1, 1));
initialPanel = new InitialPanel();
this.add(initialPanel);
//this.pack();
this.setLocationRelativeTo(null);
this.validate();
this.repaint();
this.setVisible(true);
JButton[] test = initialPanel.getSignOnButtons();
String newStr = initialPanel.getNorthPanel().getTest(); //Call to getTest
}
public InitialPanel getInitialPanel() {
return initialPanel;
}
}
// InitialPanel ___________
class InitialPanel extends JPanel{
private BorderLayout InitialPanelLayout;
private JButton[] signOnButtons;
private NorthPanel northPanel;
private JPanel southPanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel centerPanel;
private JLabel userNameLabel;
private JTextField userNameTextField;
public InitialPanel() {
this.setSize(600, 600);
InitialPanelLayout = new BorderLayout();
this.setLayout(InitialPanelLayout);
this.createPanels();
this.formatCenterPanel();
setVisible(true);
this.validate();
this.repaint();
}
/**
* Method is to create panels for all the Border Layout of initial Panel
* @param none
*/
private void createPanels() {
//Graphics comp2D = new Graphics();
//comp2D.drawString("Free the bound periodicals", 22, 100);
northPanel = new NorthPanel();
northPanel.setSize(600, 200);
this.add(northPanel, "North");
southPanel = new JPanel();
this.add(southPanel, "South");
leftPanel = new JPanel();
this.add(leftPanel, BEFORE_LINE_BEGINS);
rightPanel = new JPanel();
this.add(rightPanel, AFTER_LINE_ENDS);
centerPanel = new JPanel();
this.add(centerPanel, "Center");
}
/**
* Method is to format the center panel on the opening window.
* It uses 4 row grid layout, top row is a container with Label and TextField.
* @param none
*/
private void formatCenterPanel() {
centerPanel.setLayout(new GridLayout(5, 1));
Container userName = new Container();
userName.setLayout(new GridLayout(1, 2));
userNameLabel = new JLabel("UserName: ");
userName.add(userNameLabel);
userNameTextField = new JTextField(30);
userName.add(userNameTextField);
centerPanel.add(userName);
signOnButtons = new JButton[3];
signOnButtons[0] = new JButton("Sign-On");
signOnButtons[1] = new JButton("Register");
signOnButtons[2] = new JButton("Exit");
for (JButton butt: signOnButtons) {
centerPanel.add(butt);
}
centerPanel.validate();
centerPanel.repaint();
}
public JButton[] getSignOnButtons() {
return signOnButtons;
}
public JTextField getUserNameTextField() {
return userNameTextField;
}
public NorthPanel getNorthPanel() {
return northPanel;
}
}
ВСЕ обновлено сейчас ...
толькоостается одна ошибка: «на нестатический метод getInitialPanel () нельзя ссылаться из статического контекста»
в классе представления
public InitialPanel getInitialPanel() {
return InitialFrame.getInitialPanel();
}
Окончательное редактирование: основным решением было использование этогоКлючевое слово.После этого я мог использовать контроллер для объединения 3 или более методов для извлечения атрибутов, скрытых в пакете просмотра.
, например, в классе просмотра:
public JButton[] getSignOnButtons() {
return this.initialFrame.getInitialPanel().getSignOnButtons();
}
** РЕДАКТИРОВАТЬ 12 /25/2018 this.keyword не решает эту проблему каждый раз.Это все еще сложная операция.Иногда я просто позволял NetBeans создавать сам метод, потому что он говорит, что метод не найден, даже если он назван точно так же.