【问题标题】:JScrollPane not showing in my JTextAreaJScrollPane 未显示在我的 JTextArea 中
【发布时间】:2017-08-03 04:01:09
【问题描述】:

我有一个名为 txtChat 的 JTextArea,它用作我的聊天机器人中的聊天区域。我创建了一个 JScrollPane scroll = new JScrollPane(txtChat) 并将我的 txtChat 添加到 JScrollPane 中。我的 JTextArea 被放置在具有 BorderLayout 的面板中,因此我已将滚动添加到包含我的 JTextArea chatPanel.add(scroll, BorderLayout.East) 的面板中。但它不工作!就像当我的谈话时间更长时,卷轴不会出现! 对不起,代码很长,所以我只是在这里粘贴了重要的部分。

public class GUI_V3 extends JFrame {

//mainPanel into Frame
private JPanel mainPanel = new JPanel();

//clock component
private JTextField timeF = new JTextField(8);

//Button component
private Box btnBox = Box.createHorizontalBox();
private JButton button1 = new JButton("Add Keywords");
private JButton button2 = new JButton("Help");

//Top menu bar
private JPanel topPanel = new JPanel();

//Typing Area
private JTextField txtEnter = new JTextField();
//Typing Panel
private JPanel typingPanel = new JPanel();

//Chat Area
private JTextArea txtChat = new JTextArea();

private JPanel chatPanel = new JPanel();

//Scroll
private JScrollPane scroll = new JScrollPane(txtChat);

private String name;

private Conversation_V3 convo = new Conversation_V3();

public GUI_V3(){
    //Frame attributes
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000,1000);
    this.setVisible(true);
    //this.setResizable(false);
    this.setLocationRelativeTo(null);
    this.setTitle("Welcome to Haidilao");

    //clock attributes
    timeF.setEditable(false);
    timeF.setFont(new Font("Arial",Font.BOLD,48));
    timeF.setBackground(new Color(228,224,199));
    timeF.setHorizontalAlignment(SwingConstants.RIGHT);

    //button attributes
    button1.setFont(new Font("Arial",Font.PLAIN,38));
    button2.setFont(new Font("Arial",Font.PLAIN,38));
    btnBox.add(button1);
    btnBox.add(Box.createRigidArea(new Dimension(10,70)));
    btnBox.add(button2);

    //Add ActionListener to buttons
    Button1Handler handlerB1 = new Button1Handler();
    button1.addActionListener(handlerB1);
    Button2Handler handlerB2 = new Button2Handler();
    button2.addActionListener(handlerB2);

    //Top menu bar
    topPanel.setLayout(new BorderLayout());
    topPanel.add(btnBox, BorderLayout.WEST);
    topPanel.add(timeF, BorderLayout.EAST);
    topPanel.setBackground(new Color(228,224,199));

    //Typing Area Attribute
    txtEnter.setFont(new Font("DejaVu Sans",Font.PLAIN,30));
    typingPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20));
    typingPanel.setLayout(new BorderLayout());
    typingPanel.setBackground(new Color(228,224,199));
    typingPanel.add(txtEnter);

    //Add action listener for typing area
    TypingHandler handlerT = new TypingHandler();
    txtEnter.addActionListener(handlerT);



    //Chat Area Attribute
    txtChat.setEditable(false);
    txtChat.setFont(new Font("DejaVu Sans",Font.PLAIN,30));
    txtChat.setLineWrap(true);
    txtChat.setWrapStyleWord(true);

    chatPanel.setBorder(BorderFactory.createEmptyBorder(5,20,10,20));
    chatPanel.setLayout(new BorderLayout());
    chatPanel.setBackground(new Color(228,224,199));
    chatPanel.add(txtChat, BorderLayout.CENTER);

    chatPanel.add(scroll, BorderLayout.EAST);

    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);


    //mainPanel Layout
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);
    mainPanel.add(typingPanel, BorderLayout.SOUTH);
    mainPanel.add(chatPanel, BorderLayout.CENTER);



    //Add components to the JFrame
    this.add(mainPanel);
    //Add actionListener to clock
    ClockHandler handlerC = new ClockHandler();
    Timer t = new Timer(500, handlerC);
    t.start();

    //display greetings and ask for name
    name = greetings();
    addText(botSays("Welcome! " + name + ", you can ask me anything about the menu by providing keywords."));
}

//Action handler for txtEnter
private class TypingHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //grab input
        String input = txtEnter.getText();
        //add to chat area
        txtChat.append(name + ": " + input + "\n");
        //set input field to empty
        txtEnter.setText("");
        SoundEffect.music();
        convo.setInput(input);
        addText(botSays(convo.getReply()));
        //set the chat area to bottom of scroll
        txtChat.setCaretPosition(txtChat.getDocument().getLength());
    }
}

【问题讨论】:

  • txtChat 应该在滚动窗格中。为什么要将它添加到布局中? chatPanel.add(txtChat, BorderLayout.CENTER);你不应该那样做,就用chatPanel.add(scroll, BorderLayout.CENTER);你为什么要向EAS​​T添加滚动? ScrollPane 将包含您的 txtChat。
  • 哇,这解决了问题。所以我对 JScrollPane 的理解是完全错误的?它是一种包含我的 txtChat 的容器?然后它会在需要时显示滚动?
  • 是的,根据tutorial

标签: java jscrollpane swingx


【解决方案1】:

请找到一个可行的解决方案并根据您的要求进行调整。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TestFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame frame = new TestFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        JTextArea textArea = new JTextArea();
        scrollPane.setViewportView(textArea);
    }
}

希望这会有所帮助。 :-)

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 2015-09-12
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多