简体   繁体   English

添加滚动Jpanel时不可见的文本区域

[英]Invisible textarea when I add scroll Jpanel

When I add the scrollbar, I can't see the chat output and inputbar.当我添加滚动条时,我看不到聊天 output 和输入栏。 If I delete the scrollbar, everything is ok.如果我删除滚动条,一切正常。 I don't know why... Scroll must be vertical.我不知道为什么...滚动必须是垂直的。 Are there any other ways to display vertical text in chat?还有其他方法可以在聊天中显示垂直文本吗? This is my code:这是我的代码:

public class ChatBot extends JFrame{

private JTextField inputBar = new JTextField();  
private JTextArea chatOutput = new JTextArea();
private JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

public ChatBot() {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(600, 600);
    frame.setLocationRelativeTo(null);  
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLayout(null);
    frame.setTitle("ChatBot");
    frame.add(inputBar);
    frame.add(chatOutput);
    frame.setContentPane(scroll);
    frame.setVisible(true);
    
    frame.addWindowListener(new WindowListener() { 
        
        @Override
        public void windowClosing(WindowEvent e) {  
            not_open = true;
        }
        
        @Override
        public void windowOpened(WindowEvent e) {
        }
        @Override
        public void windowClosed(WindowEvent e) {
        }
        @Override
        public void windowIconified(WindowEvent e) {
        }
        @Override
        public void windowDeiconified(WindowEvent e) {
        }
        @Override
        public void windowActivated(WindowEvent e) {
        }
        @Override
        public void windowDeactivated(WindowEvent e) {
        }
        
    });
           
    inputBar.setLocation(2, 500);
    inputBar.setSize(540, 30);
    
    chatOutput.append("- ChatBot: Hello");
    
    inputBar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
            String userInput = inputBar.getText();        
            chatOutput.append("- You: " + userInput + "\n");
            
            if(userInput.contains("Chat")){
                    botOutput("Connected...");
            }else{
                    botOutput("Invalid command");
            }
            inputBar.setText("");
        }
    });
    
    //Chat output
    chatOutput.setSize(538, 493);
    chatOutput.setLocation(2, 2);
    chatOutput.setBackground(Color.white);
    chatOutput.setLineWrap(true);
    chatOutput.setEditable(false);

}

public void botOutput(String s){
    chatOutput.append("- ChatBot: " + s + "\n");
}
        
}

What's wrong?怎么了? It seems the problem is jpanel scroll.看来问题是jpanel滚动。 This is a simple chatbot in java这是 java 中的简单聊天机器人

Added a layout and assigned the JTextArea to the JScrollpane.添加了一个布局并将 JTextArea 分配给 JScrollpane。 Hope it helps.希望能帮助到你。

public class ChatBot {

    private JTextField inputBar = new JTextField();
    private JTextArea chatOutput = new JTextArea();
    private JScrollPane scroll = new JScrollPane(chatOutput, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    public ChatBot() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setLayout(new BorderLayout());
        frame.setTitle("ChatBot");
        inputBar.setPreferredSize(new Dimension(1, 30));
        frame.add(inputBar, BorderLayout.SOUTH);
        frame.add(scroll, BorderLayout.CENTER);
        frame.setVisible(true);

        frame.addWindowListener(new WindowListener() {

            @Override
            public void windowClosing(WindowEvent e) {
                 //not_open = true;
            }

            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }

        });

        chatOutput.append("- ChatBot: Hello");

        inputBar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String userInput = inputBar.getText();
                chatOutput.append("- You: " + userInput + "\n");

                if (userInput.contains("Chat")) {
                    botOutput("Connected...");
                } else {
                    botOutput("Invalid command");
                }
                inputBar.setText("");
            }
        });

        //Chat output
        chatOutput.setBackground(Color.white);
        chatOutput.setLineWrap(true);
        chatOutput.setEditable(false);
    }

    public void botOutput(String s) {
        chatOutput.append("- ChatBot: " + s + "\n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM