JPanel未添加新组件。

huangapple go评论140阅读模式
英文:

JPanel not adding new components

问题

我正在构建一个GUI,需要动态向JPanel添加一些标签,但出于某种原因代码无法正常工作:

public class ChatClientGUI extends javax.swing.JFrame {

    /**
     * Creates new form ChatClientGUI
     * @param server : server remoto a cui connettersi
     */
    public ChatClientGUI(ChatServerIF server) {
        initComponents();
        ...    
        messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
        messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
        // 这些代码可以正常工作,实际上我看到了两个"Mex:"标签被添加到了JPanel中
    }
    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            System.out.println("MEX SENT"); // 我在终端中看到这一行
            messagesPanel.add(new JLabel("New mex sent", SwingConstants.LEFT), BorderLayout.PAGE_START);
            // 这个代码不起作用,没有将任何内容添加到JPanel中
            server.sendMessage(client, username.getSelectedItem(), messageText.getText());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

希望这可以帮助您解决问题。

英文:

I'm building a GUI where i need to add dynamically to a JPanel some Label, but the code is not working for some reason:

public class ChatClientGUI extends javax.swing.JFrame {

    /**
     * Creates new form ChatClientGUI
     * @param server : server remoto a cui connettersi
     */
    public ChatClientGUI(ChatServerIF server) {
        initComponents();
        ...    
        messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
        messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
        // those works, infact i see 2 "Mex:" label added to the JPanel
    }
    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try {
            System.out.println("MEX SENT"); // I see this line in the terminal
            messagesPanel.add(new JLabel("New mex sent", SwingConstants.LEFT), BorderLayout.PAGE_START);
            // this does not work, nothing is added to the JPanel
            server.sendMessage(client, username.getSelectedItem(), messageText.getText());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } 

</details>


# 答案1
**得分**: 2

&gt; 我需要动态添加...

当您动态向面板添加组件时,需要调用:

1. `revalidate()` 和
2. `repaint()`(有时需要)

在面板上。

默认情况下,组件的大小为(0,0),因此没有可绘制的内容。

`revalidate()` 将调用布局管理器,`repaint()` 确保整个面板被重新绘制

```java
messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
messagesPanel.add(new JLabel("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);

指定 BorderLayout.PAGE_START 似乎没有意义。在 BorderLayout 中,您只能向任何区域添加一个组件。如果看到多个组件,则您的面板必须使用其他布局(而不是 BorderLayout)。因此,该约束是不必要的。

英文:

> i need to add dynamically...

When you add components dynamically to a panel you need to invoke:

  1. revalidate() and
  2. repaint() (sometimes needed)

on the panel.

By default a component has a size of (0, 0) so there is nothing to paint.

The revalidate() will invoke the layout manager and the repaint() makes sure the entire panel is repainted

messagesPanel.add(new JLabel(&quot;Mex:&quot;, SwingConstants.LEFT), BorderLayout.PAGE_START);
messagesPanel.add(new JLabel(&quot;Mex:&quot;, SwingConstants.LEFT), BorderLayout.PAGE_START);

Specifying the BorderLayout.PAGE_START doesn't seem to make sense. You can only add a single component to any area in the BorderLayout. Your panel must be using some other layout (not the BorderLayout) if you see multiple comonents. Therefore that constraint is not needed.

huangapple
  • 本文由 发表于 2020年10月1日 03:49:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64144761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定