gpt4 book ai didi

java - actionPerformed 和方法等于不与 Controller 一起使用

转载 作者:行者123 更新时间:2023-12-01 12:06:42 25 4
gpt4 key购买 nike

当我按 miALote 时,这段代码应该从我的主窗口创建一个新窗口...但事实并非如此,我不知道我做错了什么...如果我从 Controller ,它可以工作,但这不应该发生,所以......这是主 Controller 代码:

package Controller.GUI;

import GUI.AltaLote;
import GUI.MenuPrincipal;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller implements ActionListener{

MenuPrincipal ventanaControlada;

public Controller(MenuPrincipal win){
this.ventanaControlada=win;
}
public void actionPerformed(ActionEvent e){

if (e.getSource().equals(ventanaControlada.miALote)) {
AltaLote ventana=new AltaLote();
ControllerLote controlador=new ControllerLote(ventana);
ventana.addController(controlador);
ventana.setVisible(true);
}
}
}

主窗口代码:

package GUI;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import Controller.GUI.Controller;

public class MenuPrincipal extends JMenu{

private JFrame frame;
public JMenuItem miALote;
Controller controlador;
private JTable table;

public void addController (Controller mc){
controlador=mc;
}
/**
* Create the application.
*/
public MenuPrincipal() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
JMenuItem miALote = new JMenuItem("Dar de alta nuevo lote...");
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnLote = new JMenu("Lote");
menuBar.add(mnLote);
mnLote.add(miALote);
miALote.addActionListener(controlador);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0, 0, 390, 139);
frame.getContentPane().add(scrollPane);
frame.setSize(416, 274);
frame.setVisible(true);
frame.getContentPane().add(table, BorderLayout.WEST);
this.setVisible(true);
}
public JMenuItem getAgregarLote() {
return miALote;
}

public void setAgregarLote(JMenuItem miALote) {
this.miALote = miALote;
}

}

最佳答案

您的代码存在多个问题...

  • 当构造 MenuPrincipal 的实例时,您调用 initialize 并尝试将 controlador 注册为 ActionListener miALote,但controladornull...
  • 调用 addController 时,该方法不执行任何操作(将自身注册为 ActionListener)
  • MenuPrincipal 中,miALote 被声明为实例字段 (public JMenuItem miALote;),但您将其声明为本地变量在 intitalize 方法中 (JMenuItem miALote = new JMenuItem("Dar de alta nuevo lote..."); 表示实例字段为 null,因此当您执行 if (e.getSource().equals(ventanaControlada.miALote)) { 时,结果将始终为 false...
  • 为什么 MenuPrincipal 需要从 JMenu 扩展,而它所做的只是创建一个新的 JFrame
  • public void setAgregarLote(JMenuItem miALote) { 没有任何意义,因为您没有重建菜单来响应调用...
  • 您的 MVC 概念对我来说似乎非常扭曲。您应该避免将 UI 元素暴露给 Controller ,因为它不应该关心,它只想知道何时发生某些事情(基于商定的契约(Contract))并响应该事件
  • 坦白说,如果您不先提供可运行的代码示例,我不知道如何帮助您...

关于java - actionPerformed 和方法等于不与 Controller 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com