2016-04-13 8 views
0

ボタンを押したときにLocalDateTimeオブジェクトの月をインクリメントするアプリケーションを作成しようとしています。ボタンの押下に基づいてJFrameコンポーネントを変更するJPanel

月名を表示するLocalDateTimeオブジェクトとJLabelは、JFrameを拡張するMainクラスに格納されます。

ActionListenerを持つJButtonは、押したときにLocalDateTimeオブジェクトの月を1だけインクリメントし、JPanelを拡張するPanel1という別のクラスに格納されます。

Panel1クラスがJFrameに追加されました。ボタンを押したときにLocalDateTimeオブジェクトに加えられた変更をJLabelが反映するようにするにはどうすればよいですか?

メインクラス:

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(); 
    } 

} 

パネル1クラス:私はJLabelのはLocalDateTimeをオブジェクトに加えられた変更を反映するように、ボタンがあるときにそれを作るために何をすべき

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); 
     } 
    } 

} 

答えて

0

押された?

ここで何:

  • protected staticとしてmonthLabelを宣言します。

    protected static JLabel monthLabel; 
    

    このようにして、パッケージ内の他のクラスに表示されます。

  • actionPerformed()の方法でテキストを変更する行を追加します。

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

あなたがオブザーバーパターンが必要な場合は、SwingコンポーネントにPropertyChangeListenerを使用することができます。

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); 

が、私はむしろ、一緒にスイングコンポーネントをリンクするよりも、このソリューションを好む:あなたパネル1は、プロパティ

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()); 
     } 
    } 

} 

自分のメインクラスで、それは、次のコードで意識だ「月」プロパティにイベントを発生すべきですまたはそれらを静的にする。

関連する問題