簡體   English   中英

JScrollPane滾動可見但不起作用

[英]JScrollPane scroll visible but don't work

您能告訴我為什么滾動條不起作用嗎? 它是可見的,但不起作用,您可以在此處查看該段代碼。 可能缺少的部分是什么?

// GUI elements
private JTextField textSend = new JTextField(20);
private JTextArea textArea = new JTextArea(5, 20);

private JScrollPane scroll = new JScrollPane(textArea);

private JButton buttonConnect = new JButton("Connect");
private JButton buttonSend = new JButton("Send");
private JButton buttonDisconnect = new JButton("Disconnect");
private JButton buttonQuit = new JButton("Quit");

private JPanel leftPanel = new JPanel();
private JPanel rightPanel = new JPanel();

private JLabel empty = new JLabel("");

ChessHeroChatClient() {
    setTitle("ChessHero Chat Client");
    setLocationRelativeTo(null);
    setSize(500, 500);
    setResizable(false);

    leftPanel.setLayout(new BorderLayout());
    rightPanel.setLayout(new GridLayout(6, 1));

    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    leftPanel.add(textSend, BorderLayout.NORTH);
    leftPanel.add(textArea, BorderLayout.WEST);
    leftPanel.add(scroll, BorderLayout.EAST);

    add(leftPanel, BorderLayout.CENTER);
    add(rightPanel, BorderLayout.EAST);

    textArea.setEditable(false);
private JTextArea textArea = new JTextArea(5, 20);
private JScrollPane scroll = new JScrollPane(textArea);

您使用很好的textArea創建scrollPane。

//leftPanel.add(textArea, BorderLayout.WEST); // this is wrong
leftPanel.add(scroll, BorderLayout.EAST);

但是,然后將textArea添加到WEST並將滾動添加到EAST,這是錯誤的。 Swing組件只能有一個父級,因此只需將textArea保留下來,然后將scrollPane添加到EAST或WEST。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM