1

嗨,我在 Java 中遇到了 swixml 的问题。我需要创建简单的应用程序,但在 MVC 中,因为应用程序的形式很少。

不幸的是,试图ActionListener在控制器中添加查看按钮最终缺乏响应。

在 View 类中,我有JButton与 XML 中的 id 按钮同名的属性。

我控制器添加了代码:

 actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent){
     System.exit(0);
     // menuItemControl();                
    }
 };

    view.app_close.addActionListener(actionListener);

视图是:

public class View extends WindowAdapter{
public JMenuItem menuItem;
public JMenuBar menuBar;
public JMenu menu;
public JPanel film_form;
public JLabel label;
public JTextField textField;
public JTextArea textArea;
public JButton app_close;
public SwingEngine se;

public View() throws Exception {
   se = new SwingEngine(this);
   se.render("xml/view.xml");
  }
}

当我单击 app_close - 应用程序没有反应。

有什么想法可以帮助我吗?谢谢


编辑

感谢您的回答,更多代码:

主班

    package pl.kramarczykrent;

import javax.swing.SwingUtilities;
import pl.kramarczykrent.controllers.Controller;
import pl.kramarczykrent.views.*;
public class Kramarczykrent {

       public static void main(String[] args) {           
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {                                           
                //Model model = new Model(0);
                try{
                    View view = new View();
                    view.se.getRootComponent().setVisible(true);
                     Controller controller = new Controller(view);
                controller.control();
                } catch(Exception e)
                {
                        }



            }
        });  
    }
}

模型在这个时候是空的

查看课程

    package pl.kramarczykrent.views;
import org.swixml.SwingEngine;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;

public class View extends WindowAdapter{
    public JMenuItem menuItem;
    public JMenuBar menuBar;
    public JMenu menu;
    public JPanel film_form;
    public JLabel label;
    public JTextField textField;
    public JTextArea textArea;
    public JButton app_close;
    public SwingEngine se;

    public View() throws Exception {
       se = new SwingEngine(this);
       se.render("xml/view.xml");


}

}

视图.xml

 <?xml version="1.0" encoding="UTF-8"?>
<frame id="frame" title="Wypożyczalnia dzikiego złesiora" size="1000,720" defaultCloseOperation="JFrame.EXIT_ON_CLOSE">
    <menubar id="mb">
        <menu id="m_film" text="Filmy">
            <menuitem id="film_new" text="Nowy"></menuitem>
            <menuitem id="film_list" text="Lista"></menuitem>
            <menuitem id="film_rent" text="Wypożycz"></menuitem>
            <menuitem id="film_return" text="Zwrot"></menuitem>
            <menuitem id="film_type" text="Gatunki"></menuitem>
        </menu>
        <menu id="m_klient" text="Klienci">
            <menuitem id="klient_new" text="Nowy"></menuitem>
            <menuitem id="klient_list" text="Lista"></menuitem>
        </menu>
        <menu id="m_pracownik" text="Pracownicy">
            <menuitem id="pracownik_new" text="Nowy"></menuitem>
            <menuitem id="pracownik_list" text="Lista"></menuitem>
            <menuitem id="pracownik_type" text="Stanowiska"></menuitem>0
        </menu>
        <menu id="m_program" text="Program">
            <menuitem id="app_close" text="Zamknij"></menuitem>
        </menu>
    </menubar>
    <panel id="film_form" visible="false">
        <vbox>
            <label text="Tytuł filmu"></label>
            <label text="Język"></label>
            <label text="Reżyser"></label>
            <label text="Rok produkcji"></label>
            <label text="Opis"></label>
            <label text="Rodzaj nośnika"></label>
            <label text="Jakość"></label>
            <label text="Czas trwania w minutach"></label>
            <label text="Ilość płyt w zestawie"></label>
            <label text="Cena za wypożyczenie"></label>
            <label text="Wiek od"></label>
            <label text="ID"></label>
        </vbox>
        <vbox>
            <textfield columns="20" text="" id="film_tytul"></textfield>
            <textfield columns="20" text="" id="film_jezyk"></textfield>
            <textfield columns="20" text="" id="film_rezyser"></textfield>
            <textfield columns="20" text="" id="film_rok_produkcji"></textfield>

        </vbox>


    </panel>
</frame>

控制器

    package pl.kramarczykrent.controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.swixml.SwingEngine;
import pl.kramarczykrent.views.*;

public class Controller {
    private View view;
    private ActionListener actionListener;

    public Controller( View view)
    {
        this.view=view;

    }

    public void control(){

    actionListener = new ActionListener() {
    public void actionPerformed(ActionEvent actionEvent){
     System.exit(0);
     // menuItemControl();

    }


        };

    view.app_close.addActionListener(actionListener);

    }

    private void menuItemControl(){
        System.exit(0);
    }
}

每个文件都在其他包中,但导入正常。

以上是现在使用的所有类。

雷德加斯

4

0 回答 0