我正在尝试将JPanel添加到当前存在的JPanel中。当前存在的JPanel上面有大量的图像,我正在尝试加载另一个JPanel并将其放在我想要的坐标和我想要的大小的屏幕上。我该怎么做呢?这是我的代码:
当我将chatPanel添加到JFrame时,Demo类运行正常。但是,当我添加到我的面板时,它甚至都没有出现。
主要课程 -
panel.add(Demo.chatPanel);
frame.add(panel);
new ResetEntities();
frame.repaint();
frame.setVisible(true);
frame.setSize(1200, 800);
//Demo.chatPanel.setLayout(null);
panel.setLayout(null);
我要添加的面板......一个名为Demo的类
package level;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Demo {
public static JPanel chatPanel = new JPanel();
public Demo() {
final JTextArea textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(380, 100));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final JTextField userInputField = new JTextField(30);
userInputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String fromUser = userInputField.getText();
if (fromUser != null) {
textArea.append("Nic: " + fromUser + "\n");
textArea.setCaretPosition(textArea.getDocument()
.getLength());
userInputField.setText("");
}
}
});
chatPanel.add(userInputField, SwingConstants.CENTER);
chatPanel.add(scrollPane, SwingConstants.CENTER);
}
}