嗨,我需要创建一个scrollabe区域以垂直放置标签

时间:2019-01-20 16:35:30

标签: java user-interface jpanel jscrollpane

我试图将聊天机器人创建为一个简单的项目,所以我需要将对话作为一个简单的聊天气泡,我创建了一个聊天气泡,但是滚动的问题却无法解决,这是设计的主要代码,chattingPane.layout应该为null。

有关更多演示,botMessage是我组织聊天标签的方式

谢谢。

private void setDesign() {

    // setting design
    // panel
    designPanel = new JPanel();
    designPanel.setLayout(null);
    designPanel.setBounds(0, 0, 500, 600);
    designPanel.setBackground(Color.WHITE);
    this.add(designPanel);

    //for chatting

    chattingPane = new JPanel();
    chattingPane.setBounds(0,0, 500,500);
    chattingPane.setLayout(null);

    chattingScroll = new JScrollPane(chattingPane);
    chattingScroll.setBounds(0, 0, 500, 500);

    designPanel.add(chattingScroll);

    // user input
    userInput = new JTextField();
    userInput.setBounds(10, 510, 380, 30);
    userInput.requestFocusInWindow();
    userInput.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    designPanel.add(userInput);
    // send Button
    sendButton = new JButton("Send");
    sendButton.setBounds(393, 510, 70, 30);
    sendButton.setBackground(Color.GRAY);
    designPanel.add(sendButton);

}//end setDesign

private void botMessage(String m) {

    createVoiceFile(m);
    readVoiceFile();

    JLabel MyBot = new JLabel(m);
    Dimension size = MyBot.getPreferredSize();
    MyBot.setIcon(new ImageIcon("robot2.jpg"));
    MyBot.setBounds(5, bY, (int)size.getWidth() + 35, 30);

    botLabel = new JLabel(new ImageIcon("botChatBg.jpg"));
    botLabel.setBounds(35, bY, (int)size.getWidth() + 35, 30);

    chattingPane.add(MyBot);
    chattingPane.add(botLabel);

    SwingUtilities.updateComponentTreeUI(this);

    bY += 45;
    uY += 45;

}// userMessage

0 个答案:

没有答案