简体   繁体   English

JButton 在 JScrollPane 上

[英]JButton over JScrollPane

I have such piece of code which shoud add button over JTextArea placed in JScrollPane .我有这样一段代码,它应该在JTextArea添加按钮,放在JScrollPane Button isn't inside scroll pane!按钮不在滚动窗格内!

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setBounds(10, 340, 375, 242);
    contentPane.add(scrollPane);

    JButton btnClean = new JButton();
    btnClean.setBounds(340, 341, 26, 23);
    contentPane.add(btnClean);

    taLog = new JTextArea();
    taLog.setEditable(false);
    taLog.setLineWrap(true);
    taLog.setFont(new Font("Arial", Font.PLAIN, 12));
    scrollPane.setViewportView(taLog);

    epInfo = new JEditorPane();
    epInfo.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
    epInfo.setFont(new Font("Tahoma", Font.PLAIN, 12));
    epInfo.setContentType("text/html");
    epInfo.setEditable(false);
    epInfo.setBackground(null);
    epInfo.setBorder(null);
    epInfo.setBounds(10, 241, 375, 37);
    setInfoText();
    contentPane.add(epInfo);

Problem is whenever JTextArea changes its value button is not refreshed - it just dissapears - until I will drag mouse cursor over it.问题是每当JTextArea更改其值按钮时都不会刷新 - 它只是消失 - 直到我将鼠标光标拖到它上面。 I suppose then some repaint() is launched.我想然后启动了一些repaint()
I figured out I can add DocumentListener to JTextArea and there manually refresh button but it works until scroll bar appears in JScrollPane .我发现我可以将DocumentListener添加到JTextArea并在那里手动刷新按钮,但它可以工作,直到滚动条出现在JScrollPane I can also use我也可以用

scrollPane.getViewport().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e)
{}});

but it is invoked many times and all those refreshes are not neccesary.但它被多次调用,所有这些刷新都不是必需的。 Is there any reasonable (and efficient) way to check if JScrollPane changed?是否有任何合理(且有效)的方法来检查JScrollPane更改?

在此处输入图片说明

Null layout is useful in this case because main window is fixed空布局在这种情况下很有用,因为主窗口是固定的

No it is not useful.不,它没有用。

Any time you code uses random numbers (ie to set the size/location) of components) the code is not easily maintainable.任何时候您的代码使用随机数(即设置组件的大小/位置)时,代码都不容易维护。

Also I use WindowBuilder我也使用 WindowBuilder

Code generated by an IDE is not maintainable in another IDE. IDE 生成的代码无法在另一个 IDE 中维护。

Learn to use layout managers.学习使用布局管理器。 The point of using layout managers is to avoid hard coding of numbers and to make the code maintainable in any environment.使用布局管理器的目的是避免数字的硬编码并使代码在任何环境中都可维护。

Problem is whenever JTextArea changes its value button is not refreshed问题是每当 JTextArea 更改其值按钮不会刷新

contentPane.add(scrollPane);
...
contentPane.add(btnClean);

Swing is designed to display components in two dimension when added to the same container. Swing 旨在在添加到同一容器时以二维方式显示组件。

If you want components displayed in two dimension then you need to have a parent / child relationship:如果您希望组件以二维方式显示,那么您需要具有父/子关系:

- content pane
    - text area
        button

So the code should be something like:所以代码应该是这样的:

textArea.setLayout( new FlowLayout(FlowLayout.RIGHT) );
//contentPane.add(btnClean);
textArea.add(btnClean);

Note: you can configure the FlowLayout to remove the spacing on the top and right.注意:您可以配置 FlowLayout 以去除顶部和右侧的间距。

The problem with this is that the button will cover any text that displayed in the text area.这样做的问题是按钮将覆盖文本区域中显示的任何文本。

So the better solution is to make the button external to the text area.所以更好的解决方案是将按钮设置在文本区域之外。

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

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