これにはおそらくより良い質問/回答がありますが、私が見つけたことがうまくいかず、Googleクエリの質問の言い回しに問題がありました. 基本的に、xml ファイルからデータを取得するいくつかのパネルとコンポーネントを含む JFrame があります。JFrame のインスタンス変数を使用して、private Date focusDate = new Date();
各パネルに表示する日付の情報を格納します。これまでのところ、問題ありません。
私の問題は、「focusDate」を変更した後、ナビゲーション コンポーネントのさまざまなアクションを更新するように設定しようとしているということです。NavButtons navPanel = new NavButtons(focusDate);
内部クラスとして設定した JPanel にツールバーがあり、コンソールが変更を報告していますが、メソッドを呼び出すときに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();
}
});
私はスイングにあまり詳しくないので、これは私が得ていない小さな詳細だと確信していますが、引数の受け渡しを再トリガーし、フレームの子コンポーネントをアマチュアに更新する方法を誰かが説明できる場合よろしくお願いします。
更新 ここに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);
まだ正しいことをしていませんが...