簡體   English   中英

添加滾動Jpanel時不可見的文本區域

[英]Invisible textarea when I add scroll Jpanel

當我添加滾動條時,我看不到聊天 output 和輸入欄。 如果我刪除滾動條,一切正常。 我不知道為什么...滾動必須是垂直的。 還有其他方法可以在聊天中顯示垂直文本嗎? 這是我的代碼:

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");
}
        
}

怎么了? 看來問題是jpanel滾動。 這是 java 中的簡單聊天機器人

添加了一個布局並將 JTextArea 分配給 JScrollpane。 希望能幫助到你。

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