2010-05-05 4 views
1

스마트 배터리의 UDP 패킷을 수신하고 JFrame 내의 JPanel 내부의 JTextFields에 표시하는 응용 프로그램을 Swing에 작성했습니다.스윙 창 나누기 최소화 및 복원

어떤 이유로 애플리케이션을 최소화 한 다음 복원하면 메인 프레임 중앙의 모든 텍스트를 부드럽게 처리하고 JTextField가 화면에 그려지는 것을 방지합니다.

왜 이런 일이 발생하는지 (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

+0

알았습니다. 팁 고마워. – cuttcards

답변

3

최상위 수준에서 GridBagConstraints에있는 개체의 다른 속성을 설정하는 것이 좋습니다.

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

또한, 귀하의 BatteryPanel에, 같은 GridBagConstraints 개체를 다시 사용할 수 있습니다 단지 값을 변경 :에 난보고 싶은데. 자세한 내용은 GridBagLayout tutorial을 확인하십시오.

레이아웃을 수정하고 나면 예상대로 동작을 찾을 수있을 것입니다.

+2

GridBagConstraints에 weightx 및 weighty 값이있는 구성 요소가 하나 이상 필요합니다. 그렇지 않으면 모든 구성 요소가 컨테이너 중간에 클러스터됩니다. – willcodejavaforfood

+0

좋은 캐치, 감사합니다. 추가됨. – justkt

+0

위대한 - 그것이 그 것이다. 나는 그 라인을 상단의 GridBagConstraints에 추가했고, 변경된 각각의 JTextField에 weightx = .5 fill = GridBagConstraints.HORIZONTAL 을 추가했습니다. – cuttcards