我正在尝试制作一个可滚动的 JTextArea。我不太确定我的代码有什么问题......当我创建这个 GUI 时,它不会创建chatBox
package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import Client.Client;
@SuppressWarnings("serial")
public class GUI extends JFrame implements KeyListener {
public static JPanel contentPane;
public static JTextField usernameBox;
public static JTextField inputBox;
public static JTextField ipBox;
public static JLabel usernameLabel;
public static JButton connectButton;
public static JButton disconnectButton;
public static JLabel usersLabel;
public static JTextArea usersBox;
public static JTextArea chatBox;
public static JButton sendButton;
public static JLabel ipLabel;
public static JMenuBar menuBar;
public static JMenu file;
public static JMenuItem about;
public static JMenuItem connect;
public static JMenuItem disconnect;
public static JMenuItem setDefaultUsername;
public static JMenuItem setDefaultIP;
public static String setUser;
public static String setIP;
public static String title;
public static JScrollPane scroll = new JScrollPane(chatBox);
@SuppressWarnings("unused")
private static About aboutFrame;
public GUI() {
System.out.println("Creating new client...");
addItems();
System.out.println("DONE");
}
public void addItems() {
// Frame
title = "title";
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 760, 355);
setTitle(title);
setSize(770, 385);
setResizable(false);
// Panel
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
// Username Box
usernameBox = new JTextField();
usernameBox.setBounds(88, 6, 117, 28);
usernameBox.setColumns(10);
contentPane.add(usernameBox);
// Username Label
usernameLabel = new JLabel("Username");
usernameLabel.setBounds(17, 12, 72, 16);
contentPane.add(usernameLabel);
// Connect Button
connectButton = new JButton("Connect");
connectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Connect");
Client.Connect();
}
});
connectButton.setBounds(365, 7, 117, 29);
contentPane.add(connectButton);
// Disconnect Button
disconnectButton = new JButton("Disconnect");
disconnectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Disconnect");
Client.Disconnect();
}
});
disconnectButton.setBounds(493, 7, 117, 29);
contentPane.add(disconnectButton);
// Users Label
usersLabel = new JLabel("Users");
usersLabel.setBounds(664, 12, 61, 16);
contentPane.add(usersLabel);
// Users Box
usersBox = new JTextArea();
usersBox.setEditable(false);
usersBox.setBounds(622, 40, 122, 282);
contentPane.add(usersBox);
// Chat Box
chatBox = new JTextArea();
chatBox.setEditable(false);
chatBox.setBounds(17, 40, 593, 226);
scroll = new JScrollPane(chatBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
contentPane.add(scroll);
// Input Box
inputBox = new JTextField();
inputBox.setBounds(17, 274, 506, 48);
contentPane.add(inputBox);
inputBox.setColumns(10);
inputBox.addKeyListener(this);
inputBox.setEditable(false);
// Send Button
sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Send");
Client.Send();
}
});
sendButton.setBounds(526, 274, 84, 48);
contentPane.add(sendButton);
// ipLabel
ipLabel = new JLabel("IP");
ipLabel.setBounds(215, 12, 17, 16);
contentPane.add(ipLabel);
// ipBox
ipBox = new JTextField();
ipBox.setBounds(236, 6, 117, 28);
contentPane.add(ipBox);
ipBox.setColumns(10);
// Set Pane
setContentPane(contentPane);
// send disconnect on close of window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Client.Disconnect();
}
});
menuBar = new JMenuBar();
setJMenuBar(menuBar);
file = new JMenu("File");
menuBar.add(file);
about = new JMenuItem("About");
file.add(about);
connect = new JMenuItem("Connect");
file.add(connect);
disconnect = new JMenuItem("Disconnect");
file.add(disconnect);
setDefaultUsername = new JMenuItem("Set default username.");
file.add(setDefaultUsername);
setDefaultIP = new JMenuItem("Set default IP address.");
file.add(setDefaultIP);
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
aboutFrame = new About();
}
});
setDefaultUsername.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
promptDefaultUser();
}
});
setDefaultIP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
promptDefaultIP();
}
});
connect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Client.Connect();
}
});
disconnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Client.Disconnect();
}
});
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
Client.Send();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}