最小化和恢复 Swing 窗口会破坏布局并重新绘制

发布于 2024-08-31 13:08:12 字数 4810 浏览 10 评论 0原文

我在 Swing 中编写了一个应用程序,它侦听来自智能电池的 UDP 数据包,并将它们显示在 JFrame 内的 JPanel 内的 JTextFields 中。

由于某种原因,最小化应用程序然后恢复它会平滑主框架中心内的所有文本,并阻止将 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       
}

我非常感谢任何人对此的帮助。

I've written an application in Swing that listens for UDP packets from a smart battery and displays them in JTextFields inside a JPanel inside a JFrame.

For some reason, minimizing the application and then restoring it smooshes all the text inside the center of the main frame and prevents updates to the JTextFields to be drawn to the screen.

I'm not sure why this is happening (Swing newb) or exactly how to fix it. Below are code snippets with the relevant code.

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       
}

I'd greatly appreciate anyone's help with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

德意的啸 2024-09-07 13:08:12

在顶层的 GridBagConstraints 上,您是否考虑过设置对象的其他属性?我会研究一下:

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

此外,在 BatteryPanel 中,您可以重复使用相同的 GridBagConstraints 对象,只需更改值即可。有关详细信息,请查看 GridBagLayout 教程

一旦你修复了布局,我想你会发现行为符合预期。

On your GridBagConstraints at the top level, have you considered setting the other properties of the object? I'd look into:

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

Also, in your BatteryPanel, you can re-use the same GridBagConstraints object and just change the values. Check out the GridBagLayout tutorial for more information.

Once you fix your layout, I think you'll find the behavior to be as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文