如何制作TextArea和列表填充面板?

时间:2019-08-04 18:34:17

标签: java awt layout-manager cardlayout border-layout

我正在创建一个Java聊天应用程序,并且客户端GUI中的TextArea和列表存在问题。我正在使用cardlayout在一个面板上进行登录,而在另一个面板上进行聊天。 但是由于某种原因,TextArea和List并未分别填充面板的中心和东部,在它们下面一直到我拥有的按钮面板一直留有很多额外的空间。

我试图弄乱TextArea和列表的borderlayout,但没有成功。

我很感谢您的帮助。

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;

public class ChatFrame extends Frame{
    public ChatFrame(){
        //setTitle("Chat Frame");
        setSize(700,700);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        add(new ChatPanel(), BorderLayout.CENTER);
        setVisible(true);
    }
    public static void main(String[] args){
        ChatFrame ch = new ChatFrame();
    }
}
class ChatPanel extends Panel implements ActionListener, Runnable{

    Button connect;
    Button disconnect;
    int currentCard = 1;
    CardLayout cl;
    Panel cardPanel;
    TextField tf2;
    String name;
    List l1 = new List();
    TextField tf;
    TextArea ta;
    Socket s;
    BufferedReader in;
    PrintWriter out;
    Thread t;

    public ChatPanel(){
        setLayout(new BorderLayout());
        cardPanel = new Panel();
        cl = new CardLayout();
        cardPanel.setLayout(cl);

        tf = new TextField("                                                     ");
        tf.addActionListener(this);
        add(tf, BorderLayout.NORTH);
        ta = new TextArea();
        add(ta, BorderLayout.CENTER);

        connect = new Button("Connect");
        connect.addActionListener(this);
        disconnect = new Button ("Disconnect");
        disconnect.addActionListener(this);
        disconnect.setEnabled(false);
        Panel buttonPanel = new Panel();
        buttonPanel.add(connect);
        buttonPanel.add(disconnect);
        add(buttonPanel, BorderLayout.SOUTH);

        l1.addActionListener(this);
        //l1.setSize(new Dimension(150, 400));
        add(l1,BorderLayout.EAST);

        Panel p1 = new Panel();
        Panel p2 = new Panel();
        tf2 = new TextField("          ");
        tf2.addActionListener(this);
        add(tf2, BorderLayout.NORTH);
        p1.add(tf2);
        p2.add(tf);
        p2.add(ta);
        p2.add(l1);
        cardPanel.add(p1, BorderLayout.CENTER);
        cardPanel.add(p2, BorderLayout.CENTER);
        add(cardPanel, BorderLayout.CENTER);

    }
    public void actionPerformed(ActionEvent ae){
        if((ae.getSource()==connect)){
            try{
            s = new Socket("localhost", 3000);
            in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            out = new PrintWriter(s.getOutputStream(), true);
            t = new Thread(this,"Whatever");
            t.start();
        }catch(UnknownHostException uhe){
            System.out.println(uhe.getMessage());
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }

        connect.setEnabled(false);
        disconnect.setEnabled(true);
        cl.last(cardPanel);
        name = tf2.getText();
        out.println(name + " has entered the chat");
        ChatFrame ch = (ChatFrame) SwingUtilities.getWindowAncestor(this);
        ch.setTitle(name);
        //l1.add(name);
        }

        else if(ae.getSource()== disconnect){
            out.println(name + " has left the chat");
            try{
            s.close();
            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }
            disconnect.setEnabled(false);
            connect.setEnabled(true);
            cl.first(cardPanel);
        }
        else if (ae.getSource()== tf){
            String temp = tf.getText();
            tf.setText("");
            out.println(name + ": " + temp);

        }

    }
    public void run(){
        try{
            for(;;){
                ta.append(in.readLine() + "\n");
            }
        }catch(IOException ioe){
            System.out.println(ioe.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这部分代码不正确。

with cte as (select 
        unnest( a_list::text[] )::integer as a_list, 
        unnest( b_list::text[] )::integer as b_list, 
        unnest( c_list::text[] )::integer as c_list, 
        unnest( d_list::text[] )::integer as d_list,
        (select count(type) from t) as type
    from t 
    where type = 30
)
select array_agg(distinct a_list),array_agg(distinct b_list)
,array_agg(distinct c_list),array_agg(distinct d_list),type from cte group by type ;

一个组件("{1,2,3,4,5}";"{2,4,NULL}";"{1,4,NULL}";"{2,4,5}";3 )只能有一个父容器。尝试为每个面板添加背景颜色,以更轻松地检查是否已将组件添加到具有预期布局的容器中。