Java JTextPane JScrollPane Display Issue

后端 未结 2 1852
别那么骄傲
别那么骄傲 2021-01-23 23:59

The following class implements a chatGUI. When it runs okay the screen looks like this:

Fine ChatGUI http://img21.imageshack.us/img21/7177/rightchat.jpg

The prob

相关标签:
2条回答
  • 2021-01-24 00:29

    The problem might be the layout manager you're using.

    You have a GridBagLayout for all the components. Try adding the bottom component in their own panel instead of align them with the history text area.

    Something like:

    JScrollPane history = new JScrollPane( new JTextPane() );
    
    JPanel inputClearSavePane = new JPanel();
    // layout the input, clear save button
    
    getContentPane().add( history );
    getContentPane().add( inputClearSavePane, BorderLayout.SOUTH );
    

    And see if that helps. edit

    BTW, it works on Mac

    Linux

    And windows:

    0 讨论(0)
  • 2021-01-24 00:36

    Lots of general comments first:

    a) Post a SSCCE when you post code. If you don't know what a SSCCE is the search the forum or web. We only have limited time to look at code and 300 lines is way too much. For example:

    • the code is set the dialog icon is irrelevant to the problem and does not run since we don't have access to your resource file
    • the code to perform the save is irrelevant since that is not the proble you are trying to solve
    • as mentioned earlier the main() method is missing

    b) use proper Java naming conventions. Variable names start with a lower case character. "JBSave" and "JBClear" are not standard names and it makes your code confusing to read.

    c) I also agree the the Gridbaglayout is complicated and other layout managers (like the BorderLayout approach given earlier) are easier to use. In specific your understanding of the gridx and gridy is incorrect. They should be used to indicate "sequential" row and column positions. That is in your case you should use (0, 0), (0, 1), (1, 1), (2, 1). You jumped your gridy to 10. The 10 does not reflect a relative size. So you are missing rows, 1, 2, 3, 4, 5, 6, 7... Yes, it may work, but it is confusing to understand when reading the code.

    d) Don't use a KeyListener to handle the Enter key in the textpane. Swing was designed to use "Key Bindings". Read the section from the Swing tutorial on the same topic for more information.

    Finally the fix to your code is simply:

        textPaneHome.setToolTipText("Home Chat Message Window");
    //    textPaneHome.setPreferredSize(new Dimension(200,50));
        textPaneHome.addKeyListener(new MyKeyAdapter());
    
        scrollPaneHomeText.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPaneHomeText.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPaneHomeText.setPreferredSize(new Dimension(200,50));
    

    In general, you should never set the preferred size of a component added to a scrollpane. In this case when you set the text to null the preferred size gets reset to 0 and the layout of all the components appears to be redone.

    0 讨论(0)
提交回复
热议问题