gridbaglayout怎么实现聊天界面??
package swing;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout.Constraints;
public class chatwin {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
Container c=frame.getContentPane();
JPanel panel=new JPanel();
GridBagLayout bg=new GridBagLayout();
panel.setLayout(bg);
GridBagConstraints bgc=new GridBagConstraints();
JLabel label=new JLabel("显示消息");
bgc.gridx=0;
bgc.gridy=0;
//bgc.setAnchor();
//bgc.gridwidth=2;
bgc.weightx=100;
bgc.weighty=100;
panel.add(label,bgc);
JLabel label2=new JLabel("在线好友");
bgc.gridx=1;
bgc.gridy=0;
//bgc.setAnchor();
//bgc.gridwidth=2;
bgc.weightx=100;
bgc.weighty=100;
panel.add(label2,bgc);
JTextArea area1=new JTextArea();
area1.setLineWrap(true);
JScrollPane sp=new JScrollPane(area1);
bgc.gridx=0;
bgc.gridy=1;
//bgc.setAnchor();
bgc.weightx=100;
bgc.weighty=100;
panel.add(area1,bgc);
String item[]={"ddd","eee","ttt","bbb"};
JList list=new JList(item);
bgc.gridx=1;
bgc.gridy=1;
//bgc.setAnchor();
bgc.fill=GridBagConstraints.BOTH;
//bgc.gridwidth=2;
bgc.weightx=100;
bgc.weighty=100;
panel.add(list,bgc);
JTextArea area2=new JTextArea();
area2.setLineWrap(true);
JScrollPane sp2=new JScrollPane(area2);
bgc.gridx=0;
bgc.gridy=2;
//bgc.setAnchor();
bgc.gridwidth=2;
bgc.weightx=100;
bgc.weighty=100;
bgc.fill=GridBagConstraints.BOTH;
panel.add(area2,bgc);
JButton button1=new JButton("sent");
bgc.gridx=0;
bgc.gridy=3;
//bgc.setAnchor();
bgc.gridwidth=2;
/* bgc.weightx=100;
bgc.weighty=100;*/
panel.add(button1,bgc);
JButton button2=new JButton("exit");
bgc.gridx=0;
bgc.gridy=3;
//bgc.setAnchor();
bgc.gridwidth=2;
/* bgc.weightx=100;
bgc.weighty=100;*/
panel.add(button2,bgc);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
用gridbaglayout布局管理器生成一个简单的聊天界面,(用手写代码)第一次用gridbaglayout管理器,不是很理解,实现不了如图的效果。。。。。
请大家指点指点,要怎么去实现啊?? 学习下完整的源代码。。。幸苦大家了!!!!
------解决方案--------------------
看了LZ发的界面原图,有一个问题:
是否必须使用GridBagLayout来进行整体布局?
因为根据图片,完全不必要用GridBagLayout来进行布局,当然,也不排除这是个练习GridBagLayout的例子。
------解决方案--------------------
要是用SplitPane分隔开上下两个panel的话,还是别用GridBagLayout了,属于自己找麻烦。