gridbaglayout怎么实现聊天界面??

时间:2022-10-01 19:35:39
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管理器,不是很理解,实现不了如图的效果。。。。。
请大家指点指点,要怎么去实现啊??   学习下完整的源代码。。。幸苦大家了!!!!

13 个解决方案

#1


界面效果:
[img=http://hi.csdn.net/space-6758047-do-album-picid-971982-goto-down.html][/img]

#2


http://hi.csdn.net/space-6758047-do-album-picid-971986.html
这是图片的链接地址

#3


急需大家帮帮忙,。,。。谢谢了、。。。。

#4


看了LZ发的界面原图,有一个问题:
是否必须使用GridBagLayout来进行整体布局?
因为根据图片,完全不必要用GridBagLayout来进行布局,当然,也不排除这是个练习GridBagLayout的例子。

#5


嗯。。。想用用GridBagLayout练习布局   。。。求代码。。。。。

#6


这个界面,最下面两个按钮不适合放在整体的GridBagLayout布局中

#7


一下代码看是否符合LZ的需求:

package test;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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.border.TitledBorder;

public class TestGridBagLayout {


/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocation(450,300);
Container c = frame.getContentPane();
JPanel panel=new JPanel();
panel.setBorder(new TitledBorder(""));
GridBagLayout bg=new GridBagLayout();
panel.setLayout(bg);
GridBagConstraints bgc=new GridBagConstraints();

Insets insets = new Insets(10,5,0,5);
bgc.insets = insets ;


JLabel label=new JLabel("显示消息"); 
bgc.gridx=0;
bgc.gridy=0;
bgc.weightx=0.7;
bgc.weighty=0.05;
panel.add(label,bgc);

JLabel label2=new JLabel("在线好友"); 
bgc.gridx=1;
bgc.gridy=0;
bgc.weightx=0.3;
panel.add(label2,bgc);

bgc.fill = GridBagConstraints.BOTH;
JTextArea area1=new JTextArea(); 
area1.setBorder(new TitledBorder(""));
area1.setLineWrap(true);
JScrollPane sp = new JScrollPane(area1); 
bgc.gridx=0;
bgc.gridy=1;
bgc.weightx=0.7;
bgc.weighty=0.6;
panel.add(area1,bgc);

String item[]={"ddd","eee","ttt","bbb"};
JList list=new JList(item); 
list.setBorder(new TitledBorder(""));
list.setPreferredSize(new Dimension(100,70));
bgc.gridx=1;
bgc.gridy=1;
bgc.weightx=0.3;
panel.add(list,bgc);

JTextArea area2=new JTextArea();
area2.setBorder(new TitledBorder(""));
area2.setLineWrap(true);
JScrollPane sp2=new JScrollPane(area2); 
bgc.gridx=0;
bgc.gridy=2;
bgc.gridwidth=2;
bgc.weighty=0.3;
panel.add(area2,bgc);

bgc.gridx=0;
bgc.gridy=3;
bgc.gridwidth=2;
bgc.weighty=0.05;
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
JButton button1=new JButton("sent"); 
JButton button2=new JButton("exit"); 
bottomPanel.add(button1);
bottomPanel.add(button2);
panel.add(bottomPanel,bgc);

c.add(panel);

frame.pack();
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

#8


谢谢哈。。。。。在你的基础上修改了一下、、、  但还是有一个功能无法实现。。希望在上下两个文本区域增加 分割条JSplitPane,用户可以上下拉动改变两个文本区域的大小。。。。   
是不是要分别设置上下两个面板?那原来的位置,特别下面文本区域的位置是否会改变啊?求代码。。。。

package swing;


import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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.border.TitledBorder;

public class chatwin {


    /**
    * @param args
    */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLocation(450,300);
        Container c = frame.getContentPane();
        JPanel panel=new JPanel();
        panel.setBorder(new TitledBorder(""));
        GridBagLayout bg=new GridBagLayout();
        panel.setLayout(bg);
        GridBagConstraints bgc=new GridBagConstraints();
    
        Insets insets = new Insets(10,5,0,5);
        bgc.insets = insets ;
        
        
        JLabel label=new JLabel("显示消息"); 
        bgc.gridx=0;
        bgc.gridy=0;
        //bgc.weightx=100;
        //bgc.weighty=100;
        //bgc.anchor=GridBagConstraints.NORTH;
        panel.add(label,bgc);
    
        JLabel label2=new JLabel("在线好友"); 
        bgc.gridx=1;
        bgc.gridy=0;
        //bgc.weightx=100;
        //bgc.weighty=0.7;
        //bgc.anchor=GridBagConstraints.NORTH;
        panel.add(label2,bgc);
    
        bgc.fill = GridBagConstraints.BOTH;
        JTextArea area1=new JTextArea(); 
        area1.setBorder(new TitledBorder(""));
        area1.setLineWrap(true);
        area1.setEditable(false);
        JScrollPane sp = new JScrollPane(area1); 
        bgc.gridx=0;
        bgc.gridy=1;
        bgc.weightx=100;
        bgc.weighty=100;
        panel.add(sp,bgc);
    
        String item[]={"ddd","eee","ttt","bbb"};
        JList list=new JList(item); 
        list.setBorder(new TitledBorder(""));
        list.setPreferredSize(new Dimension(100,70));
        JScrollPane pane=new JScrollPane(list);
        bgc.gridx=1;
        bgc.gridy=1;
        bgc.fill=GridBagConstraints.BOTH;
        bgc.weightx=0;
        bgc.weighty=0;
        panel.add(pane,bgc);
        
        JTextArea area2=new JTextArea();
        area2.setBorder(new TitledBorder(""));
        area2.setLineWrap(true);
        JScrollPane sp2=new JScrollPane(area2); 
        //JScrollPane pane1=new JScrollPane(sp2);
        bgc.gridx=0;
        bgc.gridy=2;
        bgc.gridwidth=2;
        bgc.weightx=0;
        bgc.weighty=0;
        panel.add(sp2,bgc);
        
        bgc.gridx=0;
        bgc.gridy=3;
        bgc.gridwidth=2;
        bgc.weighty=0.05;
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        JButton button1=new JButton("sent"); 
        JButton button2=new JButton("exit"); 
        bottomPanel.add(button1);
        bottomPanel.add(button2);
        panel.add(bottomPanel,bgc);
        
        c.add(panel);
    
        //frame.pack();
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


#9


要是用SplitPane分隔开上下两个panel的话,还是别用GridBagLayout了,属于自己找麻烦。

#10


GridBagLayout基本就可以不用 好多培训课程都说其他三个就够用了

#11


刚开始学这个、、、想用这个试试。。。。

#12


有木有给力点的。。。

#13


import java.awt.Color; 

import java.awt.Graphics;
import javax.swing.JFrame;


public class TankClient extends JFrame {

int x=80,y=80;

public void launch(){
//setLocation(400, 300);
setSize(700,800);
setLocationRelativeTo(null);  //jframe居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);  //jframe不可改变大小
setBackground(Color.YELLOW);
setVisible(true);
Thread t=new Thread(new RunTank());
t.start();
}

//画坦克
public void paint(Graphics g){
Color c=g.getColor();
System.out.println(g);
g.setColor(Color.red);
g.fillOval(x,y, 40, 40);
g.setColor(c);
System.out.println("**********************");
System.out.println(g);
y+=20;
}

//坦克运动
class RunTank implements Runnable{

public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


public static void main(String[] args) {

TankClient tan= new TankClient();
tan.launch();

}


}

#1


界面效果:
[img=http://hi.csdn.net/space-6758047-do-album-picid-971982-goto-down.html][/img]

#2


http://hi.csdn.net/space-6758047-do-album-picid-971986.html
这是图片的链接地址

#3


急需大家帮帮忙,。,。。谢谢了、。。。。

#4


看了LZ发的界面原图,有一个问题:
是否必须使用GridBagLayout来进行整体布局?
因为根据图片,完全不必要用GridBagLayout来进行布局,当然,也不排除这是个练习GridBagLayout的例子。

#5


嗯。。。想用用GridBagLayout练习布局   。。。求代码。。。。。

#6


这个界面,最下面两个按钮不适合放在整体的GridBagLayout布局中

#7


一下代码看是否符合LZ的需求:

package test;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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.border.TitledBorder;

public class TestGridBagLayout {


/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLocation(450,300);
Container c = frame.getContentPane();
JPanel panel=new JPanel();
panel.setBorder(new TitledBorder(""));
GridBagLayout bg=new GridBagLayout();
panel.setLayout(bg);
GridBagConstraints bgc=new GridBagConstraints();

Insets insets = new Insets(10,5,0,5);
bgc.insets = insets ;


JLabel label=new JLabel("显示消息"); 
bgc.gridx=0;
bgc.gridy=0;
bgc.weightx=0.7;
bgc.weighty=0.05;
panel.add(label,bgc);

JLabel label2=new JLabel("在线好友"); 
bgc.gridx=1;
bgc.gridy=0;
bgc.weightx=0.3;
panel.add(label2,bgc);

bgc.fill = GridBagConstraints.BOTH;
JTextArea area1=new JTextArea(); 
area1.setBorder(new TitledBorder(""));
area1.setLineWrap(true);
JScrollPane sp = new JScrollPane(area1); 
bgc.gridx=0;
bgc.gridy=1;
bgc.weightx=0.7;
bgc.weighty=0.6;
panel.add(area1,bgc);

String item[]={"ddd","eee","ttt","bbb"};
JList list=new JList(item); 
list.setBorder(new TitledBorder(""));
list.setPreferredSize(new Dimension(100,70));
bgc.gridx=1;
bgc.gridy=1;
bgc.weightx=0.3;
panel.add(list,bgc);

JTextArea area2=new JTextArea();
area2.setBorder(new TitledBorder(""));
area2.setLineWrap(true);
JScrollPane sp2=new JScrollPane(area2); 
bgc.gridx=0;
bgc.gridy=2;
bgc.gridwidth=2;
bgc.weighty=0.3;
panel.add(area2,bgc);

bgc.gridx=0;
bgc.gridy=3;
bgc.gridwidth=2;
bgc.weighty=0.05;
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
JButton button1=new JButton("sent"); 
JButton button2=new JButton("exit"); 
bottomPanel.add(button1);
bottomPanel.add(button2);
panel.add(bottomPanel,bgc);

c.add(panel);

frame.pack();
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

#8


谢谢哈。。。。。在你的基础上修改了一下、、、  但还是有一个功能无法实现。。希望在上下两个文本区域增加 分割条JSplitPane,用户可以上下拉动改变两个文本区域的大小。。。。   
是不是要分别设置上下两个面板?那原来的位置,特别下面文本区域的位置是否会改变啊?求代码。。。。

package swing;


import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

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.border.TitledBorder;

public class chatwin {


    /**
    * @param args
    */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLocation(450,300);
        Container c = frame.getContentPane();
        JPanel panel=new JPanel();
        panel.setBorder(new TitledBorder(""));
        GridBagLayout bg=new GridBagLayout();
        panel.setLayout(bg);
        GridBagConstraints bgc=new GridBagConstraints();
    
        Insets insets = new Insets(10,5,0,5);
        bgc.insets = insets ;
        
        
        JLabel label=new JLabel("显示消息"); 
        bgc.gridx=0;
        bgc.gridy=0;
        //bgc.weightx=100;
        //bgc.weighty=100;
        //bgc.anchor=GridBagConstraints.NORTH;
        panel.add(label,bgc);
    
        JLabel label2=new JLabel("在线好友"); 
        bgc.gridx=1;
        bgc.gridy=0;
        //bgc.weightx=100;
        //bgc.weighty=0.7;
        //bgc.anchor=GridBagConstraints.NORTH;
        panel.add(label2,bgc);
    
        bgc.fill = GridBagConstraints.BOTH;
        JTextArea area1=new JTextArea(); 
        area1.setBorder(new TitledBorder(""));
        area1.setLineWrap(true);
        area1.setEditable(false);
        JScrollPane sp = new JScrollPane(area1); 
        bgc.gridx=0;
        bgc.gridy=1;
        bgc.weightx=100;
        bgc.weighty=100;
        panel.add(sp,bgc);
    
        String item[]={"ddd","eee","ttt","bbb"};
        JList list=new JList(item); 
        list.setBorder(new TitledBorder(""));
        list.setPreferredSize(new Dimension(100,70));
        JScrollPane pane=new JScrollPane(list);
        bgc.gridx=1;
        bgc.gridy=1;
        bgc.fill=GridBagConstraints.BOTH;
        bgc.weightx=0;
        bgc.weighty=0;
        panel.add(pane,bgc);
        
        JTextArea area2=new JTextArea();
        area2.setBorder(new TitledBorder(""));
        area2.setLineWrap(true);
        JScrollPane sp2=new JScrollPane(area2); 
        //JScrollPane pane1=new JScrollPane(sp2);
        bgc.gridx=0;
        bgc.gridy=2;
        bgc.gridwidth=2;
        bgc.weightx=0;
        bgc.weighty=0;
        panel.add(sp2,bgc);
        
        bgc.gridx=0;
        bgc.gridy=3;
        bgc.gridwidth=2;
        bgc.weighty=0.05;
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout());
        JButton button1=new JButton("sent"); 
        JButton button2=new JButton("exit"); 
        bottomPanel.add(button1);
        bottomPanel.add(button2);
        panel.add(bottomPanel,bgc);
        
        c.add(panel);
    
        //frame.pack();
        frame.setSize(500,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


#9


要是用SplitPane分隔开上下两个panel的话,还是别用GridBagLayout了,属于自己找麻烦。

#10


GridBagLayout基本就可以不用 好多培训课程都说其他三个就够用了

#11


刚开始学这个、、、想用这个试试。。。。

#12


有木有给力点的。。。

#13


import java.awt.Color; 

import java.awt.Graphics;
import javax.swing.JFrame;


public class TankClient extends JFrame {

int x=80,y=80;

public void launch(){
//setLocation(400, 300);
setSize(700,800);
setLocationRelativeTo(null);  //jframe居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);  //jframe不可改变大小
setBackground(Color.YELLOW);
setVisible(true);
Thread t=new Thread(new RunTank());
t.start();
}

//画坦克
public void paint(Graphics g){
Color c=g.getColor();
System.out.println(g);
g.setColor(Color.red);
g.fillOval(x,y, 40, 40);
g.setColor(c);
System.out.println("**********************");
System.out.println(g);
y+=20;
}

//坦克运动
class RunTank implements Runnable{

public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


public static void main(String[] args) {

TankClient tan= new TankClient();
tan.launch();

}


}