忽略该功能。我只对图形用户界面感兴趣。
我使用了一个名为mainPanel的JPanel组件,并添加了一个名为receivingWindow的JTextArea和一个名为sendWindow的JTextField。
接收窗口和发送窗口占用了整个帧空间,尽管我在以下行中指定了它们的大小:
receivingWindow = new JTextArea(50,50);
sendWindow = new JTextField(30);
如何根据自己的喜好设置它们的大小?
以下是完整的代码:
package hf;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChatClient
{
private JFrame frame;
private JTextArea receivingWindow;
private JTextField sendWindow;
private JButton sendButton;
private JPanel mainPanel;
private Socket connectionSocket;
private final static int serverPortNumber = 5000;
private PrintWriter printWriter;
private BufferedReader incomingReader;
private InputStreamReader incomingInputStreamReader;
private JScrollPane scrollPane;
public static void main(String[] args)
{
ChatClient chatClient = new ChatClient();
chatClient.startUp();
}
public void startUp()
{
setUpGui();
setUpNetworking();
handleEvents();
setUpListenerThread();
displayFrame();
}
private void setUpGui()
{
frame = new JFrame("Chat Client");
receivingWindow = new JTextArea(50,50);
sendWindow = new JTextField(30);
sendButton = new JButton("Send");
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
scrollPane = new JScrollPane(receivingWindow);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
mainPanel.add(scrollPane);
mainPanel.add(sendWindow);
mainPanel.add(sendButton);
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
}
private void handleEvents()
{
sendButton.addActionListener(new MySendButtonListener());
}
private void setUpNetworking()
{
try
{
connectionSocket = new Socket("127.0.0.1",serverPortNumber);
printWriter = new PrintWriter(connectionSocket.getOutputStream());
incomingInputStreamReader = new InputStreamReader(connectionSocket.getInputStream());
incomingReader = new BufferedReader(incomingInputStreamReader);
}
catch(Exception ex)
{
System.out.println("Error in connecting to the server.");
}
}
private class MySendButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String message = sendWindow.getText();
printWriter.println(message);
printWriter.flush();
sendWindow.setText("");
sendWindow.requestFocus();
}
}
private void setUpListenerThread()
{
try
{
Runnable incomingThread = new IncomingMessageRunnable();
Thread thread = new Thread(incomingThread);
thread.start();
}
catch(Exception ex)
{
System.out.println("Error in setting up thread for listening to incoming menssages.");
}
}
private class IncomingMessageRunnable implements Runnable
{
public void run()
{
String incomingMessage = null;
try
{
while((incomingMessage = incomingReader.readLine())!=null)
{
receivingWindow.append(incomingMessage+"n");
}
}
catch(Exception e)
{
System.out.println("Error in receiving incoming messages");
}
}
}
private void displayFrame()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
您在代码中指定的实际上不是面板上的大小,而是您的JTextField
和JTextArea
分别有多少列和多少行或列。来自 Oracle Docs:
JTextArea(int rows, int columns)
Constructs a new empty TextArea with the specified number of rows and columns.
JTextField(int columns)
Constructs a new empty TextField with the specified number of columns.
您的组件占用了整个屏幕空间,因为您的BoxLayout
适合添加到面板的每个组件,并在尊重其最大尺寸的同时将它们垂直对齐,并且由于您没有设置任何组件,它们似乎正在填满整个空间。
无论如何,我都不会尝试存档像素完美的 GUI,而是找到一个更好的LayoutManager
来存档您的想法。我建议您查看布局管理器的可视化指南,看看哪种布局最适合您的需求。