使用JScrollPane时遇到一些麻烦

时间:2013-10-24 18:19:35

标签: java swing jscrollpane

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

    }
}

3 个答案:

答案 0 :(得分:1)

不要使用空布局!!!

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

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

答案 1 :(得分: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);
    

答案 2 :(得分: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. 您在哪里设置JSCrollPane的界限,以便在null layout的面板中显示?
  2. 您正在使用null布局!您应该使用LayoutManager来为您布置GUI组件。开始学习它们。
  3. JScrollPane在其视图下使用组件的首选大小来滚动它。好吧,现在不要急于设置chatBox的首选大小。 LayoutManager应该为您完成。
  4. 开始学习:A Visual Guide to Layout Managers