为什么相同的操作在ActionListener中起作用但在@schedule方法中不起作用?

时间:2019-02-21 09:50:47

标签: java spring swing spring-scheduled

在以下代码段中,actionPerformed()方法和aggiornaAutomaticamente()方法执行相同的操作。为什么在GUI中,我看到由JButton调用的actionPerformed()进行了更改,但是通过aggiornaAutomaticamente()@scheduled方法进行了更改,即使执行了操作,更改也没有出现?

由于操作正确,因此我不会粘贴所有代码,而只会粘贴其中的一部分。

@Configuration
@EnableScheduling
public class Appoggio {

    private JFrame frmClientPrestazioniChat;
    private DateTimePicker dateTimePickerDa;
    private DateTimePicker dateTimePickerA;
    private JCheckBox chckbxAggiornaAutomaticamente;
    private JPanel pnlFiltri;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        // Comandi per usare spring
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(Appoggio.class);
        ctx.refresh();

    }


    /**
     * Create the application.
     */
    public Appoggio() {
        initialize();
    }


    /**
     * Initialize the contents of the frame.
     */
    @SuppressWarnings("serial")
    private void initialize() {
        frmClientPrestazioniChat = new JFrame();
        frmClientPrestazioniChat.setTitle("Client Prestazioni Chat");
        frmClientPrestazioniChat.setBounds(100, 100, 1209, 563);
        frmClientPrestazioniChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmClientPrestazioniChat.setExtendedState(frmClientPrestazioniChat.getExtendedState() | JFrame.MAXIMIZED_BOTH);

        JSplitPane splitPaneMain = new JSplitPane();
        splitPaneMain.setResizeWeight(0.2);
        frmClientPrestazioniChat.getContentPane().add(splitPaneMain, BorderLayout.CENTER);

        JScrollPane scrollPaneOpzioniFiltri = new JScrollPane();
        splitPaneMain.setLeftComponent(scrollPaneOpzioniFiltri);

        JPanel pnlOpzioniFiltri = new JPanel();
        scrollPaneOpzioniFiltri.setViewportView(pnlOpzioniFiltri);
        pnlOpzioniFiltri.setLayout(new BoxLayout(pnlOpzioniFiltri, BoxLayout.Y_AXIS));

        JPanel pnlOpzioni = new JPanel();
        pnlOpzioniFiltri.add(pnlOpzioni);
        pnlOpzioni.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Opzioni", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        pnlOpzioni.setLayout(new BoxLayout(pnlOpzioni, BoxLayout.X_AXIS));

        chckbxAggiornaAutomaticamente = new JCheckBox("Aggiorna Automaticamente");
        chckbxAggiornaAutomaticamente.setSelected(true);
        pnlOpzioni.add(chckbxAggiornaAutomaticamente);
        chckbxAggiornaAutomaticamente.setVerticalAlignment(SwingConstants.TOP);

        JButton btnAggiorna = new JButton("Aggiorna");
        btnAggiorna.addActionListener(new AggiornaListener());
        pnlOpzioni.add(btnAggiorna);

        Component verticalStrut_1 = Box.createVerticalStrut(20);
        pnlOpzioniFiltri.add(verticalStrut_1);

        pnlFiltri = new JPanel();
        pnlOpzioniFiltri.add(pnlFiltri);
        pnlFiltri.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Filtri", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
        pnlFiltri.setLayout(new BoxLayout(pnlFiltri, BoxLayout.Y_AXIS));

        JLabel lblDa = new JLabel("Da");
        pnlFiltri.add(lblDa);

        dateTimePickerDa = new DateTimePicker();
        dateTimePickerDa.setDateTimePermissive(LocalDateTime.now().withHour(8).withMinute(0));
        dateTimePickerDa.addDateTimeChangeListener(new DateTimeChangeListener() {

            @Override
            public void dateOrTimeChanged(DateTimeChangeEvent event) {
                aggiorna(dateTimePickerDa.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")), dateTimePickerA.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")));              
            }

        });
        pnlFiltri.add(dateTimePickerDa);

        Component verticalStrut = Box.createVerticalStrut(20);
        pnlFiltri.add(verticalStrut);

        JLabel lblA = new JLabel("A");
        pnlFiltri.add(lblA);
        dateTimePickerA = new DateTimePicker();
        dateTimePickerA.setDateTimePermissive(LocalDateTime.now());
        dateTimePickerA.addDateTimeChangeListener(new DateTimeChangeListener() {

            @Override
            public void dateOrTimeChanged(DateTimeChangeEvent event) {
                aggiorna(dateTimePickerDa.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")), dateTimePickerA.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")));              
            }

        });
        pnlFiltri.add(dateTimePickerA);

        Component verticalStrut_2 = Box.createVerticalStrut(20);
        pnlOpzioniFiltri.add(verticalStrut_2);

        aggiorna(dateTimePickerDa.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")), dateTimePickerA.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")));      

    }


    private class AggiornaListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dateTimePickerDa.setDateTimePermissive(LocalDateTime.now().withHour(8).withMinute(0));
            dateTimePickerA.setDateTimePermissive(LocalDateTime.now());
            aggiorna(dateTimePickerDa.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")), dateTimePickerA.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")));
        }

    }


    @Scheduled(fixedDelay = 60000)
    private void aggiornaAutomaticamente() {
        if (chckbxAggiornaAutomaticamente.isSelected()) {
            System.out.println("Aggiorno automaticamente");
            dateTimePickerDa.setDateTimePermissive(LocalDateTime.now().withHour(8).withMinute(0));
            dateTimePickerA.setDateTimePermissive(LocalDateTime.now());
            aggiorna(dateTimePickerDa.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")), dateTimePickerA.getDateTimePermissive().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")));
        }
    }
}

预先感谢您的帮助。

0 个答案:

没有答案