2016-12-14 44 views
0

大家好,我试图创建一个聊天会话GUI。我设法按照正确的顺序排列所有组件。唯一的问题是,框架没有响应,每当我尝试调整窗口的大小时,组件保持相同的尺寸,而且当我在JtextArea中键入文本时,它们将边框放大,并占据框架中的任何其他组件。我试过使用JScrollPane或设置最大尺寸,但它不起作用。谁能帮我。这是我的代码。JScrollPane和响应GUI与gridbaglayout

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); 

    } 

} 

回答

2

你问题的文本区域是你放panel.add(scroll)。删除这一行。另外,您应该添加滚动窗格而不是文本区域到面板。将panel.add(chat,gbc)更改为panel.add(scroll,gbc)

+0

非常感谢,它的工作! – Werokk

+0

你知道如何让用户调整窗口大小来响应组件吗? – Werokk

+0

@Werokk你能澄清你的意思吗?我似乎注意到这个框架也很奇怪。我删除了两行'fr.setMinimumSize()'和'fr.setSize()',并在代码的末尾添加了'fr.pack()'。 我强烈建议重新构建代码以分隔面板和框架配置,以便于进行故障排除。 – KyleKW