繁体   English   中英

滚动条未出现在自定义面板上

[英]Scrollbar not appearing on custom Panel

我做了一个扩展JPanel的类RefreshablePanel

public class RefreshablePanel extends JPanel {

static String description="";
static int x=10;
static int y=11;

protected void paintComponent(Graphics g){
       super.paintComponent(g);
    for (String line : description.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
void updateDescription(String dataToAppend){
        description = description.concat("\n").concat(dataToAppend);
        System.out.println("The description is "+description);
   }   
}

然后像这样将其添加到我的GUI_class中

JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(32, 603, 889, 90);
frmToolToMigrate.getContentPane().add(scrollPane_2);

descriptionPanel = new RefreshablePanel();
scrollPane_2.setViewportView(descriptionPanel);
descriptionPanel.setBackground(Color.WHITE);
descriptionPanel.setLayout(null);

我在正在创建RefreshablePanel实例的类中添加了滚动条,但没有出现滚动条。 我尝试添加

@Override
public Dimension getPreferredSize() {
    return new Dimension(400, 1010);
}

到可刷新面板,但是字符串像这样消失了 在此处输入图片说明

当我向下滚动时什么也没出现

  1. 您使用的是null布局,因此没有任何效果可以正常工作。 Swing的核心设计是利用布局管理器。

  2. 您的RefreshablePanel没有明显的大小,这意味着当您添加到滚动窗格时,滚动窗格可能会简单地认为其大小应为0x0 RefreshablePanel需要提供某种大小提示给滚动窗格,最好通过getPreferredSize方法

  3. 您可以使用JLabel (文本包装在html中)或不可编辑的JTextArea来达到相同的结果

更新

快速检查您的代码。 您将xy值声明为static并在paintComponent方法中增加y

static int x=10;
static int y=11;

protected void paintComponent(Graphics g){
   super.paintComponent(g);
    for (String line : description.split("\n"))
        g.drawString(line, x, y += g.getFontMetrics().getHeight());
}

这意味着两件事。

  1. 如果您有多个RefreshablePanel实例,它们将共享相同的x / y值并更新它们
  2. y会不断更新到新的位置,因此,如果在第二个绘制上对面板进行了两次绘制,则y位置将从第一个呼叫退出时的最后一个位置开始。

请记住,您无法控制绘画过程。 系统决定需要时,可以在任何时候执行涂装周期...

使x / y值成为paintComponent方法的局部变量...

更新

如果可用空间允许,滚动窗格将尝试并匹配组件的首选大小。 这可能意味着滚动条可能要等到您调整窗口大小后才会出现...但是您使用的是null布局,因此对您不起作用...

要影响滚动窗格的大小,可以改用Scrollable界面...

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Scrollable01 {

    public static void main(String[] args) {
        new Scrollable01();
    }

    public Scrollable01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                RefreshablePanel pane = new RefreshablePanel();
                pane.updateDescription("1. You're using null layouts, so nothing is going to work the way it should. Swing is designed at the core to utilise layout managers.");
                pane.updateDescription("2. You're RefreshablePanel has no discernible size, meaning that when you add to the scroll pane, the scroll pane is likely to simply think it's size should 0x0. RefreshablePanel needs to provide some kind of size hint back to the scrollpane, preferably via the getPreferredSize method");
                pane.updateDescription("3. You could use a JLabel (with text wrapped in html) or a non-editable JTextArea to achieve the same results");

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(pane));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RefreshablePanel extends JPanel implements Scrollable {

        public String description = "";

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 1010);
        }

        @Override
        protected void paintComponent(Graphics g) {
            int x = 10;
            int y = 11;
            super.paintComponent(g);
            for (String line : description.split("\n")) {
                g.drawString(line, x, y += g.getFontMetrics().getHeight());
            }
        }

        void updateDescription(String dataToAppend) {
            description = description.concat("\n").concat(dataToAppend);
            System.out.println("The description is " + description);
            repaint();
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(400, 400);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 64;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }
    }
}

暂无
暂无

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

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