2016-04-13 89 views
0

我試圖創建一個應用程序,當按鈕被按下時將增加LocalDateTime對象中的月份。基於從包含的JPanel按鈕更改JFrame組件

顯示月份名稱的LocalDateTime對象和JLabel存儲在擴展JFrame的Main類中。

有一個ActionListener的JButton在按下時將LocalDateTime對象的月份加1,它存儲在一個名爲Panel1的擴展JPanel的獨立類中。

Panel1類被添加到JFrame中。我應該怎麼做才能讓JLabel在按下按鈕時反映對LocalDateTime對象所做的更改?

主類:

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的類別:

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

我應該怎麼做才能讓這個選擇JLabel將反映到LocalDateTime對象所做的更改時,按鈕何苦呢?

這裏是:

  • 聲明monthLabelprotected static

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

} 

「月」屬性它知道在下面的代碼中,你的主要類觸發事件:

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

我比較喜歡這種解決方案比Swing組件連接在一起或者使它們變得靜止。