英文:
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
> 我需要动态添加...
当您动态向面板添加组件时,需要调用:
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:
revalidate()
andrepaint()
(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("Mex:", SwingConstants.LEFT), BorderLayout.PAGE_START);
messagesPanel.add(new JLabel("Mex:", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论