JScrollPane 和带有 gridbaglayout 的响应式 GUI
JScrollPane and responsive GUI with gridbaglayout
大家好,我正在尝试创建聊天会话 GUI。我设法以正确的顺序放置所有组件。唯一的问题是框架没有响应,每当我尝试调整 window 的大小时,组件保持相同的尺寸,而且当我在 JtextArea 中键入文本时,它们的边框会放大并接管中的任何其他组件框架。我试过使用 JScrollPane 或设置最大尺寸,但它不起作用。谁能帮我。这是我的代码。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;
public class ClientGUI extends JPanel {
public ClientGUI() {
Dimension size = getPreferredSize();
size.width = 500;
setPreferredSize(size);
setBorder(BorderFactory.createTitledBorder("Peron"));
GridBagConstraints gbc = new GridBagConstraints();
JTextArea chat, list;
JTextField wm;
JButton sm, sf, pm, lo;
JFrame fr = new JFrame("FRAME");
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(200, 200);
fr.setMinimumSize(new Dimension(1400, 1000));
JPanel panel = new JPanel(new GridBagLayout());
fr.add(panel);
gbc.insets = new Insets(40, 40, 40, 40);
chat = new JTextArea("Welcome to the chat room");
// chat.setEditable(false);
JScrollPane scroll = new JScrollPane(chat); // place the JTextArea in a
// scroll pane
panel.add(scroll);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 5;
gbc.gridheight = 7;
// gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.ipady = 400;
gbc.ipadx = 200;
panel.add(chat, gbc);
wm = new JTextField("Insert message", 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.ipady = 150;
gbc.ipadx = 300;
gbc.gridx = 0;
gbc.gridy = 10;
panel.add(wm, gbc);
list = new JTextArea("User online");
gbc.gridx = 5;
gbc.gridy = 2;
gbc.ipady = 400;
gbc.ipadx = 300;
panel.add(list, gbc);
sm = new JButton("Send");
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 200;
panel.add(sm, gbc);
pm = new JButton("Private message");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 4;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 20;
panel.add(pm, gbc);
lo = new JButton("LOGOUT");
gbc.gridx = 5;
gbc.gridy = 1;
gbc.ipady = 20;
panel.add(lo, gbc);
sf = new JButton("Send File");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 5;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 20;
panel.add(sf, gbc);
}
}
文本区域的问题在于放置 panel.add(scroll)
的位置。删除此行。此外,您应该将滚动窗格而不是文本区域添加到面板。将 panel.add(chat,gbc)
更改为 panel.add(scroll,gbc)
大家好,我正在尝试创建聊天会话 GUI。我设法以正确的顺序放置所有组件。唯一的问题是框架没有响应,每当我尝试调整 window 的大小时,组件保持相同的尺寸,而且当我在 JtextArea 中键入文本时,它们的边框会放大并接管中的任何其他组件框架。我试过使用 JScrollPane 或设置最大尺寸,但它不起作用。谁能帮我。这是我的代码。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;
public class ClientGUI extends JPanel {
public ClientGUI() {
Dimension size = getPreferredSize();
size.width = 500;
setPreferredSize(size);
setBorder(BorderFactory.createTitledBorder("Peron"));
GridBagConstraints gbc = new GridBagConstraints();
JTextArea chat, list;
JTextField wm;
JButton sm, sf, pm, lo;
JFrame fr = new JFrame("FRAME");
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(200, 200);
fr.setMinimumSize(new Dimension(1400, 1000));
JPanel panel = new JPanel(new GridBagLayout());
fr.add(panel);
gbc.insets = new Insets(40, 40, 40, 40);
chat = new JTextArea("Welcome to the chat room");
// chat.setEditable(false);
JScrollPane scroll = new JScrollPane(chat); // place the JTextArea in a
// scroll pane
panel.add(scroll);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 5;
gbc.gridheight = 7;
// gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.ipady = 400;
gbc.ipadx = 200;
panel.add(chat, gbc);
wm = new JTextField("Insert message", 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridheight = 2;
gbc.ipady = 150;
gbc.ipadx = 300;
gbc.gridx = 0;
gbc.gridy = 10;
panel.add(wm, gbc);
list = new JTextArea("User online");
gbc.gridx = 5;
gbc.gridy = 2;
gbc.ipady = 400;
gbc.ipadx = 300;
panel.add(list, gbc);
sm = new JButton("Send");
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 200;
panel.add(sm, gbc);
pm = new JButton("Private message");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 4;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 20;
panel.add(pm, gbc);
lo = new JButton("LOGOUT");
gbc.gridx = 5;
gbc.gridy = 1;
gbc.ipady = 20;
panel.add(lo, gbc);
sf = new JButton("Send File");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 5;
gbc.gridy = 10;
gbc.ipady = 20;
gbc.ipadx = 20;
panel.add(sf, gbc);
}
}
文本区域的问题在于放置 panel.add(scroll)
的位置。删除此行。此外,您应该将滚动窗格而不是文本区域添加到面板。将 panel.add(chat,gbc)
更改为 panel.add(scroll,gbc)