简体   繁体   English

根据所包含的JPanel中的按钮按下来更改JFrame组件

[英]Altering JFrame component based on button press from contained JPanel

I am trying to create an application which will increment the month in a LocalDateTime object when a button is pressed. 我正在尝试创建一个应用程序,当按下按钮时,该应用程序将在LocalDateTime对象中增加月份。

The LocalDateTime object and JLabel displaying the month name is stored in the Main class which extends JFrame. 显示月份名称的LocalDateTime对象和JLabel存储在扩展JFrame的Main类中。

The JButton which has an ActionListener, incrementing the month of the LocalDateTime object by 1 when it is pressed, is stored in a separate class called Panel1 which extends JPanel. 具有ActionListener的JButton,在按下该按钮时,将LocalDateTime对象的月份增加1,该按钮存储在称为Panel1的单独类中,该类扩展了JPanel。

The Panel1 class is added to the JFrame. Panel1类已添加到JFrame。 What should I do to make it so that the JLabel will reflect the changes made to the LocalDateTime object when the button is pressed? 我应该怎么做才能使JLabel在按下按钮时反映对LocalDateTime对象所做的更改?

Main Class: 主类:

import javax.swing.*;
import java.awt.*;
import java.time.LocalDateTime;

public class Main extends JFrame {

    private LocalDateTime currentTime = LocalDateTime.now();
    private JLabel monthLabel;

    public Main() {
        super();
        setLayout(new BorderLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        monthLabel = new JLabel(currentTime.getMonth().name());

        add(monthLabel, BorderLayout.NORTH);
        add(new Panel1(currentTime), BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        Main main = new Main();
    }

}

Panel1 Class: Panel1类别:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;

public class Panel1 extends JPanel implements ActionListener {

    private LocalDateTime time;
    private JButton incrementMonth;

    public Panel1(LocalDateTime time) {
        this.time = time;

        incrementMonth = new JButton(">");
        incrementMonth.addActionListener(this);
        add(incrementMonth);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == incrementMonth) {
            time = time.plusMonths(1);
        }
    }

}

If you want an observer pattern, you can use PropertyChangeListener on swing components. 如果需要观察者模式,则可以在回转组件上使用PropertyChangeListener。 Your Panel1 should fire an event on a property 您的Panel1应该在属性上触发一个事件

public class Panel1 extends JPanel implements ActionListener {

    private LocalDateTime time;
    private JButton incrementMonth;

    public Panel1(LocalDateTime time) {
        this.time = time;

        incrementMonth = new JButton(">");
        incrementMonth.addActionListener(this);
        add(incrementMonth);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == incrementMonth) {
            LocalDateTime oldValue = time;
            time = time.plusMonths(1);
            this.firePropertyChange("month", oldValue.getMonth().name(), time.getMonth().name());
        }
    }

}

The "month" property it's aware in the following code, in your Main class: 在您的Main类中的以下代码中可以识别出“ month”属性:

super();
    setLayout(new BorderLayout());
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    monthLabel = new JLabel(currentTime.getMonth().name());

    add(monthLabel, BorderLayout.NORTH);
    Panel1 panel1 = new Panel1(currentTime);
    panel1.addPropertyChangeListener("month", new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            monthLabel.setText(evt.getNewValue().toString());
        }
    });
    add(panel1, BorderLayout.SOUTH);

    pack();
    setVisible(true);

I rather prefer this solution than linking swing components together or making them static. 我更喜欢这种解决方案,而不是将摆动组件链接在一起或使其静止。

What should I do to make it so that the JLabel will reflect the changes made to the LocalDateTime object when the button is pressed? 我应该怎么做才能使JLabel在按下按钮时反映对LocalDateTime对象所做的更改?

Here's what: 内容如下:

  • Declare monthLabel as protected static . 声明monthLabelprotected static

     protected static JLabel monthLabel; 

    This way, it will be visible to other classes in the package. 这样,它将对包中的其他类可见。

  • Add the line to change the text in you actionPerformed() method. 添加该行以更改actionPerformed()方法中的文本。

     public void actionPerformed(ActionEvent e) { if (e.getSource() == incrementMonth) { time = time.plusMonths(1); Main.monthLabel.setText(time.getMonth().name()); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM