2010-05-05 55 views
1

我已經在Swing中編寫了一個應用程序,用於偵聽來自智能電池的UDP數據包,並將它們顯示在JFrame中的JPanel內的JTextFields中。最小化和恢復擺動窗口中斷佈局和重新打印

出於某種原因,最小化應用程序然後恢復它會清除主框架中心內的所有文本,並防止更新JTextFields以便在屏幕上繪製。

我不知道爲什麼會發生這種情況(Swing newb)或者確切地說如何解決它。以下是帶有相關代碼的代碼片段。

public class Initializer { 

public void initialize() { 

      //The mediator performs all updates of the BatteryPanel 
      mediator = Mediator.getInstance(); 

      //BatteryService listens for UDP packets and uses mediator to update panel 
      bService = new BatteryService(); 

    createGUI(); 

    bService.start(); 
} 

public void createGUI() { 

    bPanel = new BatteryPanel(); 
    frame = new JFrame(); 

      //For spacing between the BatteryPanel and the edge of the window 
    frame.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.insets = new Insets(6,8,8,6); 

    frame.getContentPane().add(bPanel, gbc); 
    frame.setResizable(false); 

    mediator.setBatteryPanel(bPanel); 
    frame.pack(); 
    frame.setVisible(true); 

} 
} 

public class BatteryService { 
    private Mediator mediator; 
    ... 
     //This is inside a SwingWorker - we have data now update the panel 
     protected void process(List<BatteryUpdateBean> bBeans) { 
      ... 
      mediator.setBatteryStatus(status); 
      mediator.setTemperature(temperature); 
      mediator.setLastConnected(lastConnected); 
     } 
     } 
    } 
} 

public class BatteryPanel extends JPanel { 

private static final int AFTER_LABEL_SPACE = 8; 
private static final int AFTER_TITLE_SPACE = 8; 
private static final int BETWEEN_ROWS_SPACE = 3; 

private JTextField statusField; 
private JTextField temperatureField; 
private JTextField lastConnectedField; 

public BatteryPanel() {  
    initComponents(); 
} 

    //get textfield methods snipped 
    ... 

private void initComponents() { 
    JLabel titleLabel = new JLabel("Battery"); 
    titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); 

    JLabel lastConnectedLabel = new JLabel("Last connected:"); 
    JLabel statusLabel = new JLabel("Status:"); 
    JLabel temperatureLabel = new JLabel("Temperature:"); 

    temperatureField= new JTextField("NO CONNECTION    "); 
    temperatureField.setOpaque(false); 
    temperatureField.setEditable(false); 
    temperatureField.setBorder(BorderFactory.createEmptyBorder()); 

    statusField= new JTextField("         "); 
    statusField.setOpaque(false); 
    statusField.setEditable(false); 
    statusField.setBorder(BorderFactory.createEmptyBorder()); 

    powerField = new JTextField("        "); 
    powerField.setOpaque(false); 
    powerField.setEditable(false); 
    powerField.setBorder(BorderFactory.createEmptyBorder()); 


    setLayout(new GridBagLayout()); 

    GridBagConstraints titleC = new GridBagConstraints(); 
    GridBagConstraints lastConnectedLabelC = new GridBagConstraints(); 
    GridBagConstraints statusLabelC = new GridBagConstraints(); 
    GridBagConstraints temperatureLabelC = new GridBagConstraints(); 
    GridBagConstraints statusFieldC = new GridBagConstraints(); 
    GridBagConstraints temperatureFieldC = new GridBagConstraints(); 
    GridBagConstraints lastConnectedFieldC = new GridBagConstraints(); 

    titleC.gridx = 0; titleC.gridy = 0; titleC.gridwidth = 2; 
    titleC.anchor = GridBagConstraints.FIRST_LINE_START; 
    titleC.insets = new Insets(0, 0, AFTER_TITLE_SPACE, 0); 

    lastConnectedLabelC.gridx = 0; lastConnectedLabelC.gridy = 1; 
    lastConnectedLabelC.anchor = GridBagConstraints.LINE_START; 
    lastConnectedLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE); 

    lastConnectedFieldC.gridx = 1; lastConnectedFieldC.gridy = 1; 
    lastConnectedFieldC.anchor = GridBagConstraints.LINE_START; 
    lastConnectedFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0); 

    statusLabelC.gridx = 0; statusLabelC.gridy = 2; 
    statusLabelC.anchor = GridBagConstraints.LINE_START; 
    statusLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE); 

    statusFieldC.gridx = 1; statusFieldC.gridy = 2; 
    statusFieldC.anchor = GridBagConstraints.LINE_START; 
    statusFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0); 
    statusFieldC.fill = GridBagConstraints.HORIZONTAL; 

    temperatureLabelC.gridx = 0; temperatureLabelC.gridy = 3; 
    temperatureLabelC.anchor = GridBagConstraints.LINE_START; 
    temperatureLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE); 


    temperatureFieldC.gridx = 1; temperatureFieldC.gridy = 3; 
    temperatureFieldC.anchor = GridBagConstraints.LINE_START; 
    temperatureFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0); 

    ... 
      //add (item, constraints) snipped  
} 

我非常感謝任何人的幫助。

+0

確保您初始化EventDispatchThread(EDT)上的GUI,並且中介器也更新EDT上的GUI。 – willcodejavaforfood 2010-05-05 13:32:51

+0

明白了。謝謝你的提示。 – cuttcards 2010-05-05 14:28:25

回答

3

在您的GridBagConstraints頂層,您是否考慮過設置對象的其他屬性?我會考慮:

gbc.fill = GridBagConstraints.BOTH; 
gbc.weightx = 1; 
gbc.weighty = 1; 

而且,在你BatteryPanel,你可以重新使用相同的GridBagConstraints對象並更改值。查看GridBagLayout tutorial瞭解更多信息。

修復佈局後,我認爲您會發現行爲符合預期。

+2

您還需要至少一個在其GridBagConstraints中具有weightx和weighty值的組件,否則所有組件都將聚集在容器中間 – willcodejavaforfood 2010-05-05 13:31:52

+0

好的,謝謝。添加。 – justkt 2010-05-05 13:36:45

+0

太棒了 - 就是這樣。我將這些行添加到頂部的GridBagConstraints,並且還添加了: weightx = .05 fill = GridBagConstraints.HORIZONTAL 給每個不斷變化的JTextFields。 – cuttcards 2010-05-05 14:19:49