在JScrollPanel

时间:2017-03-26 13:14:11

标签: java swing paintcomponent

我实际上是在坚持一些奇怪的事情。我有一个带有JScrollPane的JFrame,其中jPanel远大于实际屏幕。我在colums中绘制正方形,我希望这些正方形越过jPanel的右边界。 (这样当它向右滚动时它们就会出现。)但是用paintComponents方法绘制的方块只停留在JScrollPane的可见ViewPort上。

这是我在JFrame中的JScrollPane的代码:

public void initComponents(){

    mainPanel = new DrawPanel(dim);
    this.getContentPane().setLayout(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridheight = 1;
    gbc.gridwidth = 1;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weighty = 1;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.BOTH;


    JScrollPane jsp = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jsp.setLayout(new ScrollPaneLayout());
    jsp.setViewportView(mainPanel);
    jsp.getVerticalScrollBar().setUnitIncrement(20);
    jsp.setBorder(BorderFactory.createEmptyBorder());
    jsp.setPreferredSize(new Dimension(dim.width,dim.height -taskBarSize));
    jsp.setMinimumSize(new Dimension(dim.width,dim.height -taskBarSize));
    jsp.setMaximumSize(new Dimension(dim.width,dim.height -taskBarSize));
    this.getContentPane().add(jsp, gbc);
    this.getContentPane().revalidate();
    this.getContentPane().repaint();




}

这是我的JPanel类:

公共类DrawPanel扩展了JPanel {

private Dimension dim;
private Integer numberPanels = 7;
private Double startPointX;
private Double startPointY;
private Double heightRow;
private Double heightPanel;


public DrawPanel(Dimension d) {
    this.dim = d;
    //this.setBackground(Color.BLACK);
    calculateStartPoint();
}

public void calculateStartPoint() {


    startPointX = (dim.getWidth() / 10) * 1;
    startPointY = (dim.getHeight() / 10) * 1;
    heightRow = (dim.getHeight() * 0.8) / numberPanels;
    heightPanel = heightRow - 10;
    double colums = 366/numberPanels;
    this.setPreferredSize(new Dimension((int)(heightRow *((int)colums + 1)), dim.height ));
    this.setMinimumSize(new Dimension((int)(heightRow *((int)colums + 1)), dim.height ));

}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.setColor(Color.GRAY);
    for (int i = 1; i <= 366; i++) {

        // Si c'est le dernier d'une colonne
        if (i%numberPanels == 0 && i != 0) {
            g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
                    heightPanel.intValue());
            startPointX = startPointX + heightRow;
            startPointY = startPointY - ((numberPanels -1) * heightRow);

            // Si c'est encore dans la meme colonne
        } else {
            g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
                    heightPanel.intValue());
            startPointY = startPointY + heightRow;
        }

    }


}

}

启动时:

enter image description here

当我移动scrollPane时:

enter image description here

此外,关于调整所有问题的大小。此外,我不得不看到,当向后滚动时,已经画过的方块消失了,仿佛屏幕上的所有内容都消失了。

感谢任何有时间的人。

1 个答案:

答案 0 :(得分:2)

您的问题是每次完成绘画时都需要重新计算起点。否则变量会不必要地增加。所以添加两行:

@Override
protected void paintComponent(Graphics g) { // should be protected
    super.paintComponent(g);

    // need to re-initialize variables within this
    startPointX = (dim.getWidth() / 10) * 1;
    startPointY = (dim.getHeight() / 10) * 1;

仅供参考,将来,请将MCVE与您的问题一起发布。例如,这是我用你的代码制作的MCVE,现在可以由任何人复制,粘贴和运行的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;

@SuppressWarnings("serial")
public class Foo02 extends JPanel {
    private DrawPanel mainPanel;
    private Dimension dim = new Dimension(200, 200);

    public Foo02() {
        initComponents();
    }

    public void initComponents() {
        mainPanel = new DrawPanel(dim);
        // !! this.getContentPane().setLayout(new GridBagLayout());
        setLayout(new GridBagLayout()); // !!
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weighty = 1;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.BOTH;
        JScrollPane jsp = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jsp.setLayout(new ScrollPaneLayout());
        jsp.setViewportView(mainPanel);
        jsp.getVerticalScrollBar().setUnitIncrement(20);
        jsp.setBorder(BorderFactory.createEmptyBorder());
        jsp.setPreferredSize(new Dimension(dim.width, dim.height));
        jsp.setMinimumSize(new Dimension(dim.width, dim.height));
        jsp.setMaximumSize(new Dimension(dim.width, dim.height));
        add(jsp, gbc);
        revalidate();
        repaint();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        Foo02 mainPanel = new Foo02();
        JFrame frame = new JFrame("Foo02");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

@SuppressWarnings("serial")
class DrawPanel extends JPanel {
    private Dimension dim;
    private Integer numberPanels = 7;
    private Double startPointX;
    private Double startPointY;
    private Double heightRow;
    private Double heightPanel;

    public DrawPanel(Dimension d) {
        this.dim = d;
        // this.setBackground(Color.BLACK);
        calculateStartPoint();
    }

    public void calculateStartPoint() {
        startPointX = (dim.getWidth() / 10) * 1;
        startPointY = (dim.getHeight() / 10) * 1;
        heightRow = (dim.getHeight() * 0.8) / numberPanels;
        heightPanel = heightRow - 10;
        double colums = 366 / numberPanels;
        this.setPreferredSize(new Dimension((int) (heightRow * ((int) colums + 1)), dim.height));
        this.setMinimumSize(new Dimension((int) (heightRow * ((int) colums + 1)), dim.height));
    }

    @Override
    protected void paintComponent(Graphics g) { // should be protected
        super.paintComponent(g);
        // need to re-initialize variables within this
        startPointX = (dim.getWidth() / 10) * 1;
        startPointY = (dim.getHeight() / 10) * 1;

        g.setColor(Color.GRAY);
        for (int i = 1; i <= 366; i++) {
            // Si c'est le dernier d'une colonne
            if (i % numberPanels == 0 && i != 0) {
                g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
                        heightPanel.intValue());
                startPointX = startPointX + heightRow;
                startPointY = startPointY - ((numberPanels - 1) * heightRow);
                // Si c'est encore dans la meme colonne
            } else {
                g.fillRect(startPointX.intValue(), startPointY.intValue(), heightPanel.intValue(),
                        heightPanel.intValue());
                startPointY = startPointY + heightRow;
            }
        }
    }
}