-1

我正在尝试制作一个可滚动的 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) {

    }
}
4

3 回答 3

1

不要使用空布局!!!

您的滚动窗格没有合适的大小,因此没有绘制。即使它确实被涂上了。Swing 被设计为与布局管理器一起使用,原因有很多,无法在此列出。

阅读 JTextArea API 并点击 Swing 教程的链接,您将在其中找到在滚动窗格中使用文本区域的正确方法的工作示例。

于 2013-10-24T19:43:44.303 回答
0

您需要将 JTextArea 和 JScrollPane 放置在正确的位置,然后一切正常。

  • JTextArea 位于 JScrollPane 内,不需要调用setBounds(...)它。这是因为滚动窗格内的 JTextArea 的原点应该是 (0,0)。
  • JScrollPane 需要放置在您放置 JTextArea 的位置。

    chatBox = new JTextArea();   
    chatBox.setEditable(false);    
    
    scroll = new JScrollPane(chatBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setBounds(17,40,593,226);
    contentPane.add(scroll);
    
于 2013-10-24T19:47:59.820 回答
0
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

问题

  1. 你在哪里设置 BoundJSCrollPane在面板中变得可见null layout
  2. 您正在使用空布局!您应该使用LayoutManager它将为您布置 GUI 组件。开始学习它们。
  3. JScrollPane 在它的视图下使用组件的首选大小来滚动它。好吧,现在不要急于设置 chatBox 的首选大小。LayoutManager应该为你做。

开始学习:布局管理器的可视化指南

于 2013-10-24T19:51:41.920 回答