2014-12-18 2 views
0

이 코드는 miALote를 누르면 메인 윈도우에서 새 창을 생성하기로되어 있습니다 ...하지만 잘못하지는 않습니다. . 내가 컨트롤러, 작동에서 경우 삭제하지만이 때문에 일이 안이라면 ...이 메인 컨트롤러 코드 :actionPerformed와 메소드가 컨트롤러와 작동하지 않습니다.

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; 
} 

} 
+0

'초기화 '를 호출 할 때'controlador'는'null'입니다 ... – MadProgrammer

+0

'MenuPrincipal'이'JMenu'를 확장해야하는 이유는 무엇입니까? ? 그리고 왜 그것은'initialize' 메소드 내에서 새로운'JFrame'을 생성합니까? 네가 행동 청취자보다 큰 문제가 있다고 생각해. – MadProgrammer

답변

1

코드에 여러 가지 문제가 있습니다 ...

를 호출 할 때이 방법은 아무것도합니다 ( ActionListener로 자신을 등록 없음) 않는
  • ... MenuPrincipal의 인스턴스를 생성자 때
  • , 당신은 miALote에 대한 ActionListenercontrolador을 등록 initialize과 시도를 부르지 만 controladornull입니다
  • MenuPrincipal에서 miALote는 인스턴스 필드 (public JMenuItem miALote;)로 선언되어 있지만 JMenuItem miALote = new JMenuItem("Dar de alta nuevo lote...");합니다 (intitalize methd의 인스턴스 필드가 w 있도록 null 것을 의미 내에서 지역 변수로 선언 암탉 if (e.getSource().equals(ventanaControlada.miALote)) {, 결과는 항상 false입니다 ...
  • MenuPrincipalJMenu에서 연장해야하는 이유는 무엇입니까? JFrame을 새로 만드는 이유는 무엇입니까?
  • public void setAgregarLote(JMenuItem miALote) { 전화에 대한 응답으로 메뉴를 다시 작성하지 않으므로 아무런 의미가 없습니다.
  • MVC에 대한 개념이 나에게 매우 비뚤어졌습니다. UI 요소를 컨트롤러에 노출시키지 않도록주의해야합니다. 합의 된 계약을 기반으로 상황이 발생했을 때만 알고 싶어하고 이벤트에 응답해야합니다.
  • 솔직히 말해서 어떻게 도와야하는지 잘 모릅니다. 코드의 실행 가능한 예제를 제공하지 않고서는 ...
  • 관련 문제