正在触发 Java GUI 更新
Triggering Java GUI to update
可能有更好的 question/answer 来解决这个问题,但我发现没有解决,而且我在为 google 查询措辞问题时遇到了问题。基本上我有一个带有几个面板和组件的 JFrame,它们从 xml 文件中提取数据。我使用 JFrame 的实例变量 private Date focusDate = new Date();
来存储我希望每个面板显示哪一天的信息,到目前为止一切顺利。
现在我的问题来了,我正在尝试将导航组件的各种操作设置为在更改 'focusDate' 后进行更新。我在 JPanel NavButtons navPanel = new NavButtons(focusDate);
中有一个工具栏,我将其设置为内部 Class 并且控制台报告 focusDate
正在更改但是我无法将 JFrame 设置为 validate(), repaint(), etc...
当我调用我的 setFocus(Date d)
方法。
如果有帮助,我可以包含更多我的代码,但这是有问题的方法:
public void setFocus(Date d) throws IOException {
focusDate = d;
dispose();
// validate(); //Tried revalidate too, but DisplayView extends JFrame
// repaint();
// revalidate();
// pack();
// DisplayView view = new DisplayView(focusDate);
setVisible(true); }
下面是我在构造函数中设置 ActionListener 的方式:
public NavButtons(Date d) {
newDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d));
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
newDate = newDate.plusDays(-7);
try { setFocus(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (IOException e) {
e.printStackTrace(); }
//validate();
//repaint();
}
});
我对 swing 不是很熟悉,所以我确定这是我没有得到的一些小细节,但是如果有人可以解释如何重新触发参数传递并更新的子组件框架给业余爱好者,将不胜感激。
更新
这是整个 JFrame
package interfaceComponents;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;
import java.text.*;
import java.util.*;
import java.time.*;
public class DisplayView extends JFrame {
//instance variables
private Date focusDate = new Date();
//constructor
public DisplayView(Date d) throws IOException {
DisplayMenus menus = new DisplayMenus();
setJMenuBar(menus);
JPanel body = new JPanel();
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
body.add(new DayView(focusDate));
LocalDate focusNextDay = LocalDate.now();
body.add(new DayView(Date.from(focusNextDay.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant())));
add(new JScrollPane(body), BorderLayout.CENTER);
JPanel footer = new JPanel();
NavButtons navPanel = new NavButtons(focusDate);
JLabel focusPoint = new JLabel(new SimpleDateFormat("E, dd MMM yyyy").format(focusDate).toString());
focusPoint.setForeground(Color.RED);
footer.setLayout(new BorderLayout());
footer.add(focusPoint, BorderLayout.CENTER);
footer.add(navPanel, BorderLayout.EAST);
footer.setBackground(Color.BLACK);
add(footer, BorderLayout.SOUTH);
pack(); }
public DisplayView() throws IOException { this(new Date()); }
public void setFocus(Date d) throws IOException {
focusDate = d;
SwingUtilities.updateComponentTreeUI(this);
// dispose();
// invalidate();
// validate(); //Tried revalidate too, but DisplayView extends JFrame
repaint();
// revalidate();
// pack();
// DisplayView view = new DisplayView(focusDate);
// setVisible(true);
}
public Date getFocus() { return focusDate; }
class NavButtons extends JPanel {
private JToolBar toolBar = new JToolBar("Navigation");
private JButton weekBack = new JButton("<<");
private JButton dayBack = new JButton("<");
private JButton returnToday = new JButton("Today");
private JButton nextDay = new JButton(">");
private JButton nextWeek = new JButton(">>");
private JButton calendar = new JButton("L");
private LocalDate newDate = LocalDate.now();
public NavButtons(Date d) {
newDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d));
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
newDate = newDate.plusDays(-7);
try { setFocus(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (IOException e) {
e.printStackTrace(); }
// invalidate();
// validate();
// repaint();
}
});
dayBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(-1); }
});
returnToday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = LocalDate.now(); }
});
nextDay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(1); }
});
nextWeek.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(7); }
});
toolBar.add(weekBack);
toolBar.add(dayBack);
toolBar.add(returnToday);
toolBar.add(nextDay);
toolBar.add(nextWeek);
toolBar.add(new GalileoMode());
toolBar.add(calendar);
add(toolBar); }
}
}
正如@MadProgrammer 指出的那样,我应该将日期存储在一个单独的对象中(此处显示):
package interfaceComponents;
import java.util.*;
public class FocusDate {
//instance variables
Date focus = new Date();
//constructors
//intentionally blank: public FocusDate() {}
//methods:
public void setFocus(Date d) {
focus = d; }
public Date getFocus() {
return focus; }
}
我这样编辑了框架:
public class DisplayView extends JFrame {
//instance variables
FocusDate focus = new FocusDate();
// private Date focusDate = new Date();
//constructor
public DisplayView(Date d) throws IOException {
// focusDate = d;
Date focusDate = focus.getFocus();
...
public void setFocus(Date d) throws IOException {
// focusDate = d;
focus.setFocus(d);
虽然还是没有做对...
因此,基本思想是将 "current" 日期值包装在一个可观察的模式中,并将其传递给每个感兴趣的方。这允许导航更改日期值,但不需要了解程序的任何其他部分,将其解耦并允许更灵活的结果。
所以,我从两个基本合同开始...
public interface DateModel {
public LocalDate getDate();
public void addObserver(Observer o);
public void removeObserver(Observer o);
}
public interface MutableDateModel extends DateModel {
public void setDate(LocalDate date);
}
一个是不可变的(对于那些不需要更改日期的程序部分),一个是可变的,对于那些需要更改日期的程序部分(比如导航)
然后我创建了一个 "default" 模型实现...
public class DefaultDateModel extends Observable implements MutableDateModel {
private LocalDate date;
public DefaultDateModel(LocalDate date) {
this.date = date;
}
@Override
public void setDate(LocalDate date) {
this.date = date;
setChanged();
notifyObservers();
}
@Override
public LocalDate getDate() {
return date;
}
@Override
public void removeObserver(Observer o) {
// I like the "remove" ;)
deleteObserver(o);
}
}
我比较懒,用了java.util
中的Observer
和Observable
API,你可以根据自己的需要创建自己的,但这只适合这个例子。
这意味着,我可以创建一个 DefaultDateModel
的实例,并将其传递给程序中需要 DateModel
的部分和需要 MutableDateModel
的部分,而无需创建单独的实例,整洁。
因为我们正在努力实现接口契约,那些只需要 DateModel
的程序部分将永远只能访问接口中定义的方法(如果转换它,那么它们做错事了)...
举个例子...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DisplayView extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DisplayView view = new DisplayView();
view.setDefaultCloseOperation(EXIT_ON_CLOSE);
view.pack();
view.setLocationRelativeTo(null);
view.setVisible(true);
}
});
}
//constructor
public DisplayView(LocalDate d) {
// DisplayMenus menus = new DisplayMenus();
// setJMenuBar(menus);
DefaultDateModel model = new DefaultDateModel(d);
JPanel body = new JPanel();
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
DayView nowView = new DayView();
nowView.setModel(model);
DayView nextView = new DayView(1);
nextView.setModel(model);
body.add(nowView);
body.add(nextView);
add(new JScrollPane(body), BorderLayout.CENTER);
JPanel footer = new JPanel();
NavButtons navPanel = new NavButtons(model);
JLabel focusPoint = new JLabel(DateTimeFormatter.ISO_DATE.format(model.getDate()));
focusPoint.setForeground(Color.RED);
footer.setLayout(new BorderLayout());
footer.add(focusPoint, BorderLayout.CENTER);
footer.add(navPanel, BorderLayout.EAST);
footer.setBackground(Color.BLACK);
add(footer, BorderLayout.SOUTH);
pack();
}
public DisplayView() {
this(LocalDate.now());
}
public interface DateModel {
public LocalDate getDate();
public void addObserver(Observer o);
public void removeObserver(Observer o);
}
public interface MutableDateModel extends DateModel {
public void setDate(LocalDate date);
}
public class DefaultDateModel extends Observable implements MutableDateModel {
private LocalDate date;
public DefaultDateModel(LocalDate date) {
this.date = date;
}
@Override
public void setDate(LocalDate date) {
this.date = date;
setChanged();
notifyObservers();
}
@Override
public LocalDate getDate() {
return date;
}
@Override
public void removeObserver(Observer o) {
// I like the "remove" ;)
deleteObserver(o);
}
}
public class DayView extends JPanel implements Observer {
private JLabel dateLabel;
private DateModel model;
private int offset;
public DayView(int offset) {
this.offset = offset;
dateLabel = new JLabel("...");
setLayout(new GridBagLayout());
add(dateLabel);
}
public DayView() {
this(0);
}
public void setModel(DateModel value) {
if (model != null) {
model.removeObserver(this);
}
this.model = value;
if (model != null) {
model.addObserver(this);
}
updateLabel();
}
public DateModel getModel() {
return model;
}
protected void updateLabel() {
DateModel model = getModel();
if (model != null) {
LocalDate offsetDate = model.getDate().plusDays(offset);
dateLabel.setText(DateTimeFormatter.ISO_DATE.format(offsetDate));
} else {
dateLabel.setText("...");
}
}
@Override
public void update(Observable o, Object arg) {
updateLabel();
}
}
class NavButtons extends JPanel implements Observer {
private JToolBar toolBar = new JToolBar("Navigation");
private JButton weekBack = new JButton("<<");
private JButton dayBack = new JButton("<");
private JButton returnToday = new JButton("Today");
private JButton nextDay = new JButton(">");
private JButton nextWeek = new JButton(">>");
private JButton calendar = new JButton("L");
private MutableDateModel model;
public NavButtons(MutableDateModel model) {
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().minusDays(7);
model.setDate(newDate);
}
}
});
dayBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().minusDays(1);
model.setDate(newDate);
}
}
});
returnToday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = LocalDate.now();
model.setDate(newDate);
}
}
});
nextDay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().plusDays(1);
model.setDate(newDate);
}
}
});
nextWeek.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().plusDays(7);
model.setDate(newDate);
}
}
});
toolBar.add(weekBack);
toolBar.add(dayBack);
toolBar.add(returnToday);
toolBar.add(nextDay);
toolBar.add(nextWeek);
// toolBar.add(new GalileoMode());
toolBar.add(calendar);
add(toolBar);
setModel(model);
}
public void setModel(MutableDateModel value) {
if (model != null) {
model.removeObserver(this);
}
this.model = value;
if (model != null) {
model.addObserver(this);
}
}
public MutableDateModel getModel() {
return model;
}
@Override
public void update(Observable o, Object arg) {
// models data has change!!
}
protected void setFocus(LocalDate newDate) {
// No idea what this is suppose to do...
}
}
}
哦,如果可以的话,我会尽量避免在 java.util.Date
和 java.time.LocalDate
之间移动,为了您的目的,我会坚持使用 LocalDate
,但我就是这样 ;)
可能有更好的 question/answer 来解决这个问题,但我发现没有解决,而且我在为 google 查询措辞问题时遇到了问题。基本上我有一个带有几个面板和组件的 JFrame,它们从 xml 文件中提取数据。我使用 JFrame 的实例变量 private Date focusDate = new Date();
来存储我希望每个面板显示哪一天的信息,到目前为止一切顺利。
现在我的问题来了,我正在尝试将导航组件的各种操作设置为在更改 'focusDate' 后进行更新。我在 JPanel NavButtons navPanel = new NavButtons(focusDate);
中有一个工具栏,我将其设置为内部 Class 并且控制台报告 focusDate
正在更改但是我无法将 JFrame 设置为 validate(), repaint(), etc...
当我调用我的 setFocus(Date d)
方法。
如果有帮助,我可以包含更多我的代码,但这是有问题的方法:
public void setFocus(Date d) throws IOException {
focusDate = d;
dispose();
// validate(); //Tried revalidate too, but DisplayView extends JFrame
// repaint();
// revalidate();
// pack();
// DisplayView view = new DisplayView(focusDate);
setVisible(true); }
下面是我在构造函数中设置 ActionListener 的方式:
public NavButtons(Date d) {
newDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d));
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
newDate = newDate.plusDays(-7);
try { setFocus(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (IOException e) {
e.printStackTrace(); }
//validate();
//repaint();
}
});
我对 swing 不是很熟悉,所以我确定这是我没有得到的一些小细节,但是如果有人可以解释如何重新触发参数传递并更新的子组件框架给业余爱好者,将不胜感激。
更新 这是整个 JFrame
package interfaceComponents;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;
import java.text.*;
import java.util.*;
import java.time.*;
public class DisplayView extends JFrame {
//instance variables
private Date focusDate = new Date();
//constructor
public DisplayView(Date d) throws IOException {
DisplayMenus menus = new DisplayMenus();
setJMenuBar(menus);
JPanel body = new JPanel();
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
body.add(new DayView(focusDate));
LocalDate focusNextDay = LocalDate.now();
body.add(new DayView(Date.from(focusNextDay.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant())));
add(new JScrollPane(body), BorderLayout.CENTER);
JPanel footer = new JPanel();
NavButtons navPanel = new NavButtons(focusDate);
JLabel focusPoint = new JLabel(new SimpleDateFormat("E, dd MMM yyyy").format(focusDate).toString());
focusPoint.setForeground(Color.RED);
footer.setLayout(new BorderLayout());
footer.add(focusPoint, BorderLayout.CENTER);
footer.add(navPanel, BorderLayout.EAST);
footer.setBackground(Color.BLACK);
add(footer, BorderLayout.SOUTH);
pack(); }
public DisplayView() throws IOException { this(new Date()); }
public void setFocus(Date d) throws IOException {
focusDate = d;
SwingUtilities.updateComponentTreeUI(this);
// dispose();
// invalidate();
// validate(); //Tried revalidate too, but DisplayView extends JFrame
repaint();
// revalidate();
// pack();
// DisplayView view = new DisplayView(focusDate);
// setVisible(true);
}
public Date getFocus() { return focusDate; }
class NavButtons extends JPanel {
private JToolBar toolBar = new JToolBar("Navigation");
private JButton weekBack = new JButton("<<");
private JButton dayBack = new JButton("<");
private JButton returnToday = new JButton("Today");
private JButton nextDay = new JButton(">");
private JButton nextWeek = new JButton(">>");
private JButton calendar = new JButton("L");
private LocalDate newDate = LocalDate.now();
public NavButtons(Date d) {
newDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d));
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
newDate = newDate.plusDays(-7);
try { setFocus(Date.from(newDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (IOException e) {
e.printStackTrace(); }
// invalidate();
// validate();
// repaint();
}
});
dayBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(-1); }
});
returnToday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = LocalDate.now(); }
});
nextDay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(1); }
});
nextWeek.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) { newDate = newDate.plusDays(7); }
});
toolBar.add(weekBack);
toolBar.add(dayBack);
toolBar.add(returnToday);
toolBar.add(nextDay);
toolBar.add(nextWeek);
toolBar.add(new GalileoMode());
toolBar.add(calendar);
add(toolBar); }
}
}
正如@MadProgrammer 指出的那样,我应该将日期存储在一个单独的对象中(此处显示):
package interfaceComponents;
import java.util.*;
public class FocusDate {
//instance variables
Date focus = new Date();
//constructors
//intentionally blank: public FocusDate() {}
//methods:
public void setFocus(Date d) {
focus = d; }
public Date getFocus() {
return focus; }
}
我这样编辑了框架:
public class DisplayView extends JFrame {
//instance variables
FocusDate focus = new FocusDate();
// private Date focusDate = new Date();
//constructor
public DisplayView(Date d) throws IOException {
// focusDate = d;
Date focusDate = focus.getFocus();
...
public void setFocus(Date d) throws IOException {
// focusDate = d;
focus.setFocus(d);
虽然还是没有做对...
因此,基本思想是将 "current" 日期值包装在一个可观察的模式中,并将其传递给每个感兴趣的方。这允许导航更改日期值,但不需要了解程序的任何其他部分,将其解耦并允许更灵活的结果。
所以,我从两个基本合同开始...
public interface DateModel {
public LocalDate getDate();
public void addObserver(Observer o);
public void removeObserver(Observer o);
}
public interface MutableDateModel extends DateModel {
public void setDate(LocalDate date);
}
一个是不可变的(对于那些不需要更改日期的程序部分),一个是可变的,对于那些需要更改日期的程序部分(比如导航)
然后我创建了一个 "default" 模型实现...
public class DefaultDateModel extends Observable implements MutableDateModel {
private LocalDate date;
public DefaultDateModel(LocalDate date) {
this.date = date;
}
@Override
public void setDate(LocalDate date) {
this.date = date;
setChanged();
notifyObservers();
}
@Override
public LocalDate getDate() {
return date;
}
@Override
public void removeObserver(Observer o) {
// I like the "remove" ;)
deleteObserver(o);
}
}
我比较懒,用了java.util
中的Observer
和Observable
API,你可以根据自己的需要创建自己的,但这只适合这个例子。
这意味着,我可以创建一个 DefaultDateModel
的实例,并将其传递给程序中需要 DateModel
的部分和需要 MutableDateModel
的部分,而无需创建单独的实例,整洁。
因为我们正在努力实现接口契约,那些只需要 DateModel
的程序部分将永远只能访问接口中定义的方法(如果转换它,那么它们做错事了)...
举个例子...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DisplayView extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DisplayView view = new DisplayView();
view.setDefaultCloseOperation(EXIT_ON_CLOSE);
view.pack();
view.setLocationRelativeTo(null);
view.setVisible(true);
}
});
}
//constructor
public DisplayView(LocalDate d) {
// DisplayMenus menus = new DisplayMenus();
// setJMenuBar(menus);
DefaultDateModel model = new DefaultDateModel(d);
JPanel body = new JPanel();
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
DayView nowView = new DayView();
nowView.setModel(model);
DayView nextView = new DayView(1);
nextView.setModel(model);
body.add(nowView);
body.add(nextView);
add(new JScrollPane(body), BorderLayout.CENTER);
JPanel footer = new JPanel();
NavButtons navPanel = new NavButtons(model);
JLabel focusPoint = new JLabel(DateTimeFormatter.ISO_DATE.format(model.getDate()));
focusPoint.setForeground(Color.RED);
footer.setLayout(new BorderLayout());
footer.add(focusPoint, BorderLayout.CENTER);
footer.add(navPanel, BorderLayout.EAST);
footer.setBackground(Color.BLACK);
add(footer, BorderLayout.SOUTH);
pack();
}
public DisplayView() {
this(LocalDate.now());
}
public interface DateModel {
public LocalDate getDate();
public void addObserver(Observer o);
public void removeObserver(Observer o);
}
public interface MutableDateModel extends DateModel {
public void setDate(LocalDate date);
}
public class DefaultDateModel extends Observable implements MutableDateModel {
private LocalDate date;
public DefaultDateModel(LocalDate date) {
this.date = date;
}
@Override
public void setDate(LocalDate date) {
this.date = date;
setChanged();
notifyObservers();
}
@Override
public LocalDate getDate() {
return date;
}
@Override
public void removeObserver(Observer o) {
// I like the "remove" ;)
deleteObserver(o);
}
}
public class DayView extends JPanel implements Observer {
private JLabel dateLabel;
private DateModel model;
private int offset;
public DayView(int offset) {
this.offset = offset;
dateLabel = new JLabel("...");
setLayout(new GridBagLayout());
add(dateLabel);
}
public DayView() {
this(0);
}
public void setModel(DateModel value) {
if (model != null) {
model.removeObserver(this);
}
this.model = value;
if (model != null) {
model.addObserver(this);
}
updateLabel();
}
public DateModel getModel() {
return model;
}
protected void updateLabel() {
DateModel model = getModel();
if (model != null) {
LocalDate offsetDate = model.getDate().plusDays(offset);
dateLabel.setText(DateTimeFormatter.ISO_DATE.format(offsetDate));
} else {
dateLabel.setText("...");
}
}
@Override
public void update(Observable o, Object arg) {
updateLabel();
}
}
class NavButtons extends JPanel implements Observer {
private JToolBar toolBar = new JToolBar("Navigation");
private JButton weekBack = new JButton("<<");
private JButton dayBack = new JButton("<");
private JButton returnToday = new JButton("Today");
private JButton nextDay = new JButton(">");
private JButton nextWeek = new JButton(">>");
private JButton calendar = new JButton("L");
private MutableDateModel model;
public NavButtons(MutableDateModel model) {
weekBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().minusDays(7);
model.setDate(newDate);
}
}
});
dayBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().minusDays(1);
model.setDate(newDate);
}
}
});
returnToday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = LocalDate.now();
model.setDate(newDate);
}
}
});
nextDay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().plusDays(1);
model.setDate(newDate);
}
}
});
nextWeek.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
LocalDate newDate = model.getDate().plusDays(7);
model.setDate(newDate);
}
}
});
toolBar.add(weekBack);
toolBar.add(dayBack);
toolBar.add(returnToday);
toolBar.add(nextDay);
toolBar.add(nextWeek);
// toolBar.add(new GalileoMode());
toolBar.add(calendar);
add(toolBar);
setModel(model);
}
public void setModel(MutableDateModel value) {
if (model != null) {
model.removeObserver(this);
}
this.model = value;
if (model != null) {
model.addObserver(this);
}
}
public MutableDateModel getModel() {
return model;
}
@Override
public void update(Observable o, Object arg) {
// models data has change!!
}
protected void setFocus(LocalDate newDate) {
// No idea what this is suppose to do...
}
}
}
哦,如果可以的话,我会尽量避免在 java.util.Date
和 java.time.LocalDate
之间移动,为了您的目的,我会坚持使用 LocalDate
,但我就是这样 ;)