我一直在尝试在我的 JTextArea 好几天来,我想尽一切办法去做。我正在制作一个聊天客户端,我有一个空布局的JPanel设置。我试过使用布局管理器,但我不明白如何让它看起来像我想要的方式。

我只想在文本区域上有一个滚动条。我是一个初学者,对于我所读到的,不可能有一个空布局?我愿意帮助改变布局,如果这也更容易。

Chat Server Client

package guiserver;

import com.apple.eawt.Application;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class GUIserver extends JFrame implements ActionListener {

JFrame login = new JFrame();
JPanel panel = new JPanel();
JPanel main = new JPanel();
JFrame accept = new JFrame();
JPanel acpt = new JPanel();
JPanel buttonpanel = new JPanel();
JButton acceptinvite = new JButton("Accept Invite");
JButton denyinvite = new JButton("Deny Invite");
JLabel acceptlabel = new JLabel();
JLabel acceptname = new JLabel();
JPanel loginpanel = new JPanel();
JTextArea chat = new JTextArea(" Waiting to Start Client...");
JSplitPane chatsplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JTextField input = new JTextField();
JScrollPane pane = new JScrollPane(chat);
JButton send = new JButton("Send");
JLabel IP = new JLabel("IP:... ");
JButton start = new JButton("Start");
sendThread s1 = new sendThread();
fromThread f1 = new fromThread();
infoFromThread f2 = new infoFromThread();
infoToThread s2 = new infoToThread();
RPSpickTimer pt = new RPSpickTimer();
myCanvas contact = new myCanvas();
myCanvas logo = new myCanvas();
JButton rock = new JButton("Rock");
JButton paper = new JButton("Paper");
JButton scissors = new JButton("Scissors");
JTextField username = new JTextField();
JLabel loguser = new JLabel("Username:");
JLabel logpass = new JLabel("Password:");
JPasswordField password = new JPasswordField();
JButton signin = new JButton("Sign in");
String inputText = "0";
String chatCurrent;
String out1;
String in;
boolean pause = true;
boolean rps = false;
int P1pick = 0;
int P2pick = 0;
String user = "User 1";
String user2 = "Jacob Abthorpe";
String pass;
int badge;
int sendinvite = 0;
char answerinvite = '-';
boolean pauseInfo = true;
int winner = 0;

public GUIserver() {
    Application application = Application.getApplication();
    Image image = Toolkit.getDefaultToolkit().getImage("src/icon.png");
    application.setDockIconImage(image);

    login.setVisible(true);
    login.setSize(400, 500);
    login.setLocationRelativeTo(null);
    login.setTitle("Login");
    login.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    login.add(loginpanel);
    loginpanel.setLayout(null);
    loginpanel.add(username);
    loginpanel.add(password);
    loginpanel.add(signin);
    loginpanel.add(logpass);
    loginpanel.add(loguser);
    loginpanel.add(logo);
    login.setAlwaysOnTop(true);
    username.setHorizontalAlignment(JTextField.CENTER);
    password.setHorizontalAlignment(JTextField.CENTER);
    username.setBounds(50, 290, 300, 30);
    password.setBounds(50, 350, 300, 30);
    loguser.setBounds(55, 265, 300, 30);
    logpass.setBounds(55, 325, 300, 30);
    signin.setBounds(150, 400, 100, 30);
    logo.setBounds(110, 50, 150, 150);
    username.addActionListener(this);
    password.addActionListener(this);
    signin.addActionListener(this);

    setVisible(false);
    setSize(500, 600);
    setLocationRelativeTo(null);
    setTitle("Chat Server Client");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    panel.setLayout(null);
    panel.setLocation(300, 0);
    panel.add(pane);
    panel.add(input);
    panel.add(send);
    panel.add(IP);
    panel.add(start);
    panel.add(rock);
    panel.add(paper);
    panel.add(scissors);
    contact.picx = 120;
    contact.picy = 120;
    contact.setBackground(Color.white);
    contact.setBounds(370, 10, 120, 120);
    panel.add(contact);
    input.addActionListener(this);
    send.addActionListener(this);
    start.addActionListener(this);
    rock.addActionListener(this);
    paper.addActionListener(this);
    scissors.addActionListener(this);
    chat.setEditable(false);
    pane.setBounds(10, 10, 350, 450);
    input.setEditable(false);
    input.setBounds(10, 470, 350, 80);
    start.setBounds(370, 470, 115, 80);
    IP.setBounds(15, 520, 480, 80);
    rock.setVisible(false);
    paper.setVisible(false);
    scissors.setVisible(false);
    scissors.setBounds(370, 430, 115, 30);
    paper.setBounds(370, 400, 115, 30);
    rock.setBounds(370, 370, 115, 30);
    setContentPane(panel);


    accept.setVisible(false);
    accept.setSize(400, 125);
    accept.setLocationRelativeTo(null);
    accept.setTitle("Rock, Paper, Scissors");
    accept.setAlwaysOnTop(true);
    accept.setResizable(false);
    accept.add(acpt);
    acpt.setLayout(null);
    acpt.add(acceptlabel);
    acpt.add(buttonpanel);
    acpt.add(acceptname);
    buttonpanel.setBounds(0, 60, 400, 40);
    buttonpanel.setLayout(new FlowLayout());
    acceptlabel.setHorizontalAlignment(SwingConstants.CENTER);
    acceptlabel.setText("would like to play Rock, Paper, Scissors. Accept?");
    acceptlabel.setBounds(0, 25, 400, 40);
    acceptname.setHorizontalAlignment(SwingConstants.CENTER);
    acceptname.setText(user2 + "");
    acceptname.setBounds(0, 3, 400, 40);
    buttonpanel.add(acceptinvite);
    buttonpanel.add(denyinvite);
    acceptinvite.addActionListener(this);
    denyinvite.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == input) {
        if (input.getText().startsWith("/")) {
            if (input.getText().contentEquals("/")) {
                chat.append("\n\n >Type '/help' for list of all commands");
            }
            if (input.getText().contentEquals("/start RPS")) {
                chat.append("\n \n >Waiting for opponent to accept invite. . .");
                pauseInfo = false;
                sendinvite = 10;
            }
            if (input.getText().contentEquals("/help")) {
                chat.append("\n\n >Type '/help' for list of all commands");
                chat.append("\n >Type '/start RPS' to play Rock Paper Scissors");
                chat.append("\n >Type '/end' to quit current game");
            }
            if (input.getText().contentEquals("/end")) {
                if (rps == true) {
                    chat.append("\n\n >Quiting Rock, Paper, Scissors. . .");
                    rps = false;
                    rock.setVisible(false);
                    paper.setVisible(false);
                    scissors.setVisible(false);
                }
            }
            input.setText("");
        } else {
            if ((input.getText().contentEquals("")) && (input.getText().contentEquals(" "))) {
                Toolkit.getDefaultToolkit().beep();
            } else {
                pause = false;
                chat.append("\n \n " + user + " says: \n        " + input.getText());
                inputText = input.getText();
                input.setText("");
            }
        }
    }
    if (e.getSource() == send) {
        //chat.append("\n \n Dale Schmidt says: \n        " + inputText);
        //pause = false;
    }
    if (e.getSource() == username) {
        user = username.getText();
    }
    if (e.getSource() == signin) {
        login.setVisible(false);
        setVisible(true);
    }
    if (e.getSource() == start) {
        try {
            s1.start();
            f1.start();
            s2.start();
            f2.start();
            setAlwaysOnTop(false);
            chat.append("\n " + InetAddress.getLocalHost() + "\n Server started.");
            IP.setText("IP: " + InetAddress.getLocalHost());
            start.setVisible(false);
            send.setVisible(true);
            input.setEditable(true);
            send.setBounds(370, 470, 115, 80);
        } catch (Exception s) {
            System.out.print("YOLOerror starting server");
        }
    }
    if (e.getSource() == rock) {
        if ((rps == true) && (P1pick == 0)) {
            P1pick = 1;
            chat.append("\n\n >You picked 'Rock'");
            if (P2pick != 0) {
                pt.start();
            }
        }
    }
    if (e.getSource() == paper) {
        if ((rps == true) && (P1pick == 0)) {
            P1pick = 2;
            chat.append("\n\n >You picked 'Paper'");
            if (P2pick != 0) {
                pt.start();
            }
        }
    }
    if (e.getSource() == scissors) {
        if ((rps == true) && (P1pick == 0)) {
            P1pick = 3;
            chat.append("\n\n >You picked 'Scissors'");
            if (P2pick != 0) {
                pt.start();
            }
        }
    }
    if (e.getSource() == acceptinvite) {
        rps = true;
        rock.setVisible(true);
        paper.setVisible(true);
        scissors.setVisible(true);
        answerinvite = 'y';
        accept.dispose();
    }
    if (e.getSource() == denyinvite) {
        answerinvite = 'n';
        accept.dispose();
    }
}

class myCanvas extends Canvas {

    int x = 10, y = 10;
    int picx = 150, picy = 150;

    public void paint(Graphics g) {
        Image image1 = Toolkit.getDefaultToolkit().getImage("src/icon.png");
        g.drawImage(image1, x, y, picx, picy, this);
    }
}    

class sendThread extends Thread {

    ServerSocket s1;
    Socket sendclientSocket = null;

    public void run() {
        inputText = input.getText();
        try {
            System.err.println("Starting Send Server");
            System.out.println(InetAddress.getLocalHost());
            s1 = new ServerSocket(4444);
            sendclientSocket = s1.accept();
            System.err.println("Started Send Server");
        } catch (Exception s1) {
            System.exit(1);
        }
        while (true) {
            try {
                PrintWriter out = new PrintWriter(sendclientSocket.getOutputStream(), true);

                if (pause == false) {
                    badge = 0;
                    out.println(input.getText());
                    System.out.println("sent");
                    pause = true;
                }
            } catch (Exception e) {
                System.err.println("Error Sending.");
                System.err.println(e.getMessage());
                System.exit(2);
            }
        }
    }
}

class fromThread extends Thread {

    ServerSocket s2;
    Socket fromclientSocket = null;

    public void run() {
        try {
            System.err.println("Starting Recieve Server");
            s2 = new ServerSocket(4441);
            fromclientSocket = s2.accept();
            System.err.println("Started Recieve Server");
        } catch (Exception s2) {
            System.exit(1);
        }

        while (true) {
            try {

                BufferedReader in = new BufferedReader(new InputStreamReader(fromclientSocket.getInputStream()));

                String fromClient = in.readLine();

                if (fromClient.contentEquals("")) {

                } else {                        
                    Toolkit.getDefaultToolkit().beep();
                    chat.append("\n\n " + user2 + " says:\n        " + fromClient);
                    badge++;
                    Application.getApplication().setDockIconBadge(Integer.toString(badge));
                }

            } catch (Exception e) {
                System.err.println("Error Receiving.");
                System.err.println(e.getMessage());
                System.exit(3);
            }
        }
    }
}

class infoFromThread extends Thread {

    ServerSocket s3;
    Socket infoFromClientSocket = null;

    public void run() {
        try {
            s3 = new ServerSocket(4446);
            infoFromClientSocket = s3.accept();
        } catch (Exception s3) {
            System.exit(7);
        }

        while (true) {
            try {

                BufferedReader in = new BufferedReader(new InputStreamReader(infoFromClientSocket.getInputStream()));

                String infoFromClient = in.readLine();

                if (infoFromClient.contentEquals("10")) {
                    accept.setVisible(true);
                    sendinvite = 0;
                } else if (infoFromClient.contentEquals("y")) {
                    chat.append("\n >Invite Accepted");
                    rps = true;
                    rock.setVisible(true);
                    paper.setVisible(true);
                    scissors.setVisible(true);
                    answerinvite = '-';
                } else if (infoFromClient.contentEquals("n")) {
                    chat.append("\n\n >Invite Declined");
                    answerinvite = '-';
                } else if (infoFromClient.contentEquals("1")) {
                    chat.append("\n\n >Opponent made selection");
                    P2pick = 1;
                    if (P1pick != 0) {
                        pt.start();
                    }
                } else if (infoFromClient.contentEquals("2")) {
                    chat.append("\n\n >Opponent made selection");
                    P2pick = 2;
                    if (P1pick != 0) {
                        pt.start();
                    }
                } else if (infoFromClient.contentEquals("3")) {
                    chat.append("\n\n >Opponent made selection");
                    P2pick = 3;
                    if (P1pick != 0) {
                        pt.start();
                    }
                }

            } catch (Exception e) {
                System.err.println("Error Receiving Information.");
                System.err.println(e.getMessage());
                System.exit(3);
            }
        }
    }
}

class infoToThread extends Thread {

    ServerSocket s4;
    Socket infoToClientSocket = null;

    public void run() {
        inputText = input.getText();
        try {
            s4 = new ServerSocket(4447);
            infoToClientSocket = s4.accept();
        } catch (Exception s4) {
            System.exit(8);
        }
        while (true) {
            try {
                PrintWriter out = new PrintWriter(infoToClientSocket.getOutputStream(), true);

                if (sendinvite == 10) {
                    out.println(sendinvite);
                    sendinvite = 0;
                    pauseInfo = false;
                }
                if ((answerinvite == 'y') || (answerinvite == 'n')) {
                    out.println(answerinvite);
                    answerinvite = '-';
                }

                    if (winner == 1) {
                        out.println("Loser");
                    }
                    else if (winner == 2) {
                        out.println("Winner");
                    }
                    else if (winner == 3) {
                        out.println("Tie");
                    }
                    //winner = 0;

                //System.err.println("infoToClient");
            } catch (Exception e) {
                System.err.println("Error Sending Information.");
                System.err.println(e.getMessage());
                System.exit(2);
            }
        }
    }
}
class RPSpickTimer extends Thread {

    public void run() {
        while (true) {
            try {
                if (P1pick != 0) {
                if (P1pick == 1) {
                    if (P2pick == 1) {
                        //tie
                        chat.append("\n\n Tie!");
                        winner = 3;
                    } else if (P2pick == 2) {
                        //P2wins
                        chat.append("\n\n " + user2 + " wins!");
                        winner = 2;
                    } else if (P2pick == 3) {
                        //P1wins
                        chat.append("\n\n " + user + " wins!");
                        winner = 1;
                    }

                } else if (P1pick == 2) {
                    if (P2pick == 1) {
                        //P1wins
                        chat.append("\n\n " + user + " wins!");
                        winner = 1;
                    } else if (P2pick == 2) {
                        //tie
                        chat.append("\n\n Tie!");
                        winner = 3;
                    } else if (P2pick == 3) {
                        //P2wins
                        chat.append("\n\n " + user2 + " wins!");
                        winner = 2;
                    }

                } else if (P1pick == 3) {
                    if (P2pick == 1) {
                        //P2wins
                        chat.append("\n\n " + user2 + " wins!");
                        winner = 2;
                    } else if (P2pick == 2) {
                        //P1wins
                        chat.append("\n\n " + user + " wins!");
                        winner = 1;
                    } else if (P2pick == 3) {
                        //tie
                        chat.append("\n\n Tie!");
                        winner = 3;
                    }
                }
                P1pick = 0;
                P2pick = 0;
            }
            else {
                sleep(250);
            }  

            } catch (Exception e) {
                System.err.println("Error Picking Winner.");
                System.err.println(e.getMessage());
                System.exit(2);
            }
        }
    }
}

public static void main(String[] args) {
    GUIserver s = new GUIserver();

}`
有帮助吗?

解决方案

这条线:

panel.add(chat);

而不是添加 chat, ,添加 pane, ,像:

panel.add(pane);

其他提示

jtextarea 文档“java.awt.textarea内部处理滚动。jtextarea与它不管理滚动不同,但实现了摆动可滚动的界面。如果需要滚动行为,则允许它放置在JScrollPane内,并且如果不需要滚动直接使用。“

我认为这意味着这就是你想要的:

JTextArea chat = new JTextArea(" Waiting to Start Client...");
JScrollPane chatScrollPane = new JScrollPane(chat);

...

panel.setLocation(300, 0);
panel.add(chatScrollPane);
panel.add(input);
.

因为尼古拉斯卡洛指出,你已经拥有了jscrollpane,只需要添加它而不是jtextarea。基本上,Java UI系统(以及大多数UI系统)是一个小部件的巨大树。如果您不向树添加小部件,则它没有效果。在这种情况下,jscrollpane没有添加到树中,因此没有为您做任何事情。

这不是一个答案,但是如何使用LayoutManager如何用于实现OP的结果

所以,基于您的代码示例,我得到了这个输出...

我希望你注意到框架不足以保持内容,这是null布局的主要问题,它们永远不会在其他系统上相同...

并且通过利用世代odicetagcode和一个称为“化合物布局”的概念,我能够模拟...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ChatLayout {

    public static void main(String[] args) {
        new ChatLayout();
    }

    public ChatLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ChatPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ChatPane extends JPanel {

        private JTextArea chatWindow;
        private JTextField input;
        private JLabel ipLabel;

        private JButton rockBtn;
        private JButton paperBtn;
        private JButton scissorsBtn;
        private JButton startBtn;

        public ChatPane() {

            JPanel leftPane = new JPanel(new GridBagLayout());
            JPanel rightPane = new JPanel(new GridBagLayout());

            chatWindow = new JTextArea(20, 40);
            chatWindow.setText("Waiting to start chat...");

            input = new JTextField(10);
            ipLabel = new JLabel("IP...");

            rockBtn = new JButton("Rock");
            paperBtn = new JButton("Paper");
            scissorsBtn = new JButton("Scissors");
            startBtn = new JButton("Start");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weighty = 1;
            gbc.weightx = 1;
            leftPane.add(new JScrollPane(chatWindow), gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weighty = 0;
            gbc.weightx = 1;
            gbc.gridheight = 1;
            leftPane.add(input, gbc);

            gbc.gridy++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            leftPane.add(ipLabel, gbc);

            JPanel buttonsPane = new JPanel(new GridLayout(3, 1));
            buttonsPane.add(rockBtn);
            buttonsPane.add(paperBtn);
            buttonsPane.add(scissorsBtn);

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.SOUTH;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weighty = 1;            
            rightPane.add(buttonsPane, gbc);

            gbc.gridy++;
            gbc.insets = new Insets(8, 0, 0, 0);
            gbc.fill = GridBagConstraints.BOTH;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.weighty = 0;
            gbc.gridheight = 2;
            gbc.ipadx = 30;
            gbc.ipady = 30;
            rightPane.add(startBtn, gbc);

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(leftPane, gbc);

            gbc.gridx++;
            gbc.weightx = 0;
            gbc.weighty = 1;
            add(rightPane, gbc);
        }

    }

}
.

现在,这只是一个例子。您需要的不同可能是不同的布局管理人员或以不同的组合使用它们...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top