2010-05-05 1 views
1

J'ai écrit une application dans Swing qui écoute les paquets UDP d'une batterie intelligente et les affiche dans JTextFields à l'intérieur d'un JPanel dans un JFrame. Pour une raison quelconque, la minimisation de l'application, puis sa restauration, aplanit tout le texte au centre du cadre principal et empêche les mises à jour des JTextField d'être dessinées à l'écran.Minimiser et restaurer les sauts de fenêtre Swing Layout et Repaint

Je ne sais pas pourquoi cela se produit (Swing newb) ou comment le réparer. Vous trouverez ci-dessous des extraits de code avec le code correspondant.

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  
} 

J'apprécierais grandement l'aide de quelqu'un avec ceci.

+0

Assurez-vous que vous initiez l'interface graphique sur EventDispatchThread (EDT) et que le médiateur met également à jour l'interface graphique sur l'EDT. – willcodejavaforfood

+0

J'ai compris. Merci pour le conseil. – cuttcards

Répondre

3

Sur votre GridBagConstraints au niveau supérieur, avez-vous envisagé de définir les autres propriétés de l'objet? Je regarde dans:

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

Aussi, dans votre BatteryPanel, vous pouvez réutiliser le même objet GridBagConstraints et juste changer les valeurs. Consultez le GridBagLayout tutorial pour plus d'informations. Une fois que vous avez réparé votre mise en page, je pense que vous trouverez le comportement comme prévu.

+2

Vous avez également besoin d'au moins un composant qui a une valeur weightx et weighty dans ses GridBagConstraints sinon tous les composants seront regroupés au milieu du conteneur – willcodejavaforfood

+0

Bonne prise, merci. Ajoutée. – justkt

+0

Super - ça le fait. J'ai ajouté ces lignes à la GridBagConstraints haut, et a également ajouté: weightx = .5 fill = GridBagConstraints.HORIZONTAL à chacun des JTextFields changeants. – cuttcards

Questions connexes