我有两个窗口同时打开,所以现在点击第一个窗口框架中的Jbutton我的Jlabel值应该更新或更改第二个窗口框架....我的问题是我在我的情况下使用观察者模式代码工作得很好我能够捕获最新的更新值但是在更新标签时给出了settext它没有得到更新
1]这是我的函数(当临时值改变时调用,我在第一帧中单击更新按钮)..我在Observable类中使用的这个函数 -
public void updatetemp(int temp) {
System.out.println("when temp is updated "+temp);
this.temp = temp;
setChanged(); // it says that we have changed somethoing so you need to notify observers
System.out.println("uiyh");
notifyObservers(temp); //notify
System.out.println("notified");
}
2]现在它会通知观察者这里温度有变化,我期待着我的 curTempLbl.setText(“”+ curTempupd);在代码中应更新第二帧中更改的值...但它没有得到更新... 注意:如果你看到我在设置jlabel之前和之后给出了system.out.println,我能够捕获正确的更新值...但是它没有在Jlabel中获得更新请帮助
public void update(Observable obj, Object arg1) {
// TODO Auto-generated method stub
if(arg1 instanceof Integer){
int curTempupd = ((Integer)arg1).intValue();
System.out.println("inside update value --------"+curTempupd);
WeatherTablePanel frame = new WeatherTablePanel(weatherDb);
frame.setVisible(true);
curTempLbl.setText("" + curTempupd);
//curTempLbl.repaint();
System.out.println("after update value --------"+curTempupd);
}
这是框架的代码,我希望我的Jlabel得到更新
public WeatherTableFrame(WeatherDb weatherdb) {
weatherTblUI = new WeatherTableUI(weatherdb);
WindowListener exitListener = new FrameTerminator();
addWindowListener(exitListener);
setTitle("Weather Table");
setSize(575,265);
add(weatherTblUI);
}
// another class
public WeatherTableUI(WeatherDb weatherDb) {
weatherTblPanel = new WeatherTablePanel(weatherDb);
addNewLocPanel = new AddNewLocPanel(weatherDb, weatherTblPanel);
setLayout(new BorderLayout());
add(weatherTblPanel, "Center");
add(addNewLocPanel, "South");
removeBtnPanel.add(removeLocBtn);
add(removeBtnPanel, "East");
removeLocBtn.addActionListener(this);
}
3]在weatherTblPanel里面我试图更新Jlabel值
我在更新功能代码中使用了线程,但仍未更新请帮助
@Override
public void update(Observable obj, Object arg1) {
// TODO Auto-generated method stub
if(arg1 instanceof Integer){
int curTempupd = ((Integer)arg1).intValue();
System.out.println("inside update value --------"+curTempupd);
//
new Thread(){
public void run(){
try{Thread.sleep(2000);
}catch(InterruptedException ie){ie.printStackTrace();}
SwingUtilities.invokeLater(new Runnable(){
public void run(){
curTempLbl.setText("" + 89);
}
});
}
}.start();
//
//setVisible(true);
//curTempLbl.repaint();
System.out.println("after update value --------"+curTempupd);
}
答案 0 :(得分:1)
猜测:您正在更新错误对象中的JLabel,即您已经创建了同一类的新对象,并且正在更新未显示的新对象中的JLabel,旧物体不变。
注意:如果您想要的答案不仅仅是一个疯狂的猜测,那么,我们应该强烈考虑为我们创建和发布minimal code example program,以便我们了解您的问题。
修改强>
是的,你实际上是创建一个全新的对象并在新对象上更改JLabel:
public void update(Observable obj, Object arg1) {
if(arg1 instanceof Integer){
int curTempupd = ((Integer)arg1).intValue();
WeatherTablePanel frame = new WeatherTablePanel(weatherDb); // ***** here *****
frame.setVisible(true);
curTempLbl.setText("" + curTempupd);
}
}
解决方案:不要在更新方法中创建新的WeatherTablePanel。而是更改已经可见 WeatherTablePanel实例的状态。如何执行此操作取决于更多未显示的代码。
另一个问题:你在哪里用你的Observable连接Observer,你在哪里调用Observable上的.addObserver(...)
?
例如,
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
import javax.swing.event.*;
public class TestObserver {
private static void createAndShowGui() {
MyObservable observable = new MyObservable();
MyObserver observer = new MyObserver();
observable.addObserver(observer); // ********* Most important line of code!! *********
JFrame frame = new JFrame("Observable");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(observable.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JDialog dialog = new JDialog(frame, "Observer", ModalityType.MODELESS);
dialog.add(observer.getMainPanel());
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MyObservable extends Observable {
private int number;
private JPanel mainPanel = new JPanel();
private JSpinner numberSpinner = new JSpinner(new SpinnerNumberModel(50, 0,
100, 1));
public MyObservable() {
mainPanel.add(new JLabel("Change Value:"));
mainPanel.add(numberSpinner);
mainPanel.setPreferredSize(new Dimension(400, 350));
numberSpinner.addChangeListener(new NumberChangeListener());
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
setChanged();
notifyObservers(Integer.valueOf(number));
}
public JPanel getMainPanel() {
return mainPanel;
}
private class NumberChangeListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
setNumber(((Integer) numberSpinner.getValue()).intValue());
}
}
}
class MyObserver implements Observer {
private static final String LABEL_TEXT = "Number is: ";
private JPanel mainPanel = new JPanel();
private JLabel label = new JLabel(LABEL_TEXT + 50);
public MyObserver() {
mainPanel.add(label);
mainPanel.setPreferredSize(new Dimension(150, 100));
}
@Override
public void update(Observable o, Object arg) {
int value = ((Integer) arg).intValue();
label.setText(LABEL_TEXT + value);
}
public JPanel getMainPanel() {
return mainPanel;
}
}