Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义绘制的组件未在JScrollPane内绘制_Java_Jscrollpane_Jcomponent - Fatal编程技术网

Java 自定义绘制的组件未在JScrollPane内绘制

Java 自定义绘制的组件未在JScrollPane内绘制,java,jscrollpane,jcomponent,Java,Jscrollpane,Jcomponent,我使用的是JavaJRE1.6.7,有一个JComponent和一个JScrollPane。我无法获得双缓冲来处理这个问题,这总是导致闪烁。如果我使用画布,我会处理缓冲,但是当与JScrollPane结合使用时,这会导致问题 所以我下载了JRE 1.6.18,希望其中一个问题能够得到解决。现在,JScrollPane中的JComponent根本没有正确绘制。它只绘制JComponent的外部区域,就好像JScrollPane是在它上面绘制的,除了边框 下面是一个未绘制的代码示例。这将导致绘制区域

我使用的是JavaJRE1.6.7,有一个JComponent和一个JScrollPane。我无法获得双缓冲来处理这个问题,这总是导致闪烁。如果我使用画布,我会处理缓冲,但是当与JScrollPane结合使用时,这会导致问题

所以我下载了JRE 1.6.18,希望其中一个问题能够得到解决。现在,JScrollPane中的JComponent根本没有正确绘制。它只绘制JComponent的外部区域,就好像JScrollPane是在它上面绘制的,除了边框

下面是一个未绘制的代码示例。这将导致绘制区域的白色轮廓为1像素宽:

public void paint(Graphics arg0) {



Graphics2D graphics = (Graphics2D) arg0;

  graphics.setColor(Color.WHITE);
  graphics.fillRect(0, 0, (int) getWidth(), (int) getHeight());
非常感谢您的帮助!
-Craig

尝试从
paintComponent(Graphics g)
而不是
paint(Graphics g)
进行覆盖。 paintComponent是您必须为costum绘图替代的方法


你确定你能看到白色的矩形,试着用红色或其他你能看到的好的东西。

试着从
paintComponent(Graphics g)
而不是
paint(Graphics g)
进行覆盖。 paintComponent是您必须为costum绘图替代的方法


你确定你能看到白色的矩形,试着用红色或其他你能看到的好东西。

好的,我有一个简单的答案。 而不是打电话

scrollPane.add(containerCanvas);
我打电话来

new JScrollPane(containerCanvas);

这在某种意义上是可行的。然而,它现在不允许JScrollPane栏出现。我不知道这是为什么,但目前正在调查。但至少部件又开始绘图了。

好的,我已经想出了一个简单的答案。 而不是打电话

scrollPane.add(containerCanvas);
我打电话来

new JScrollPane(containerCanvas);

这在某种意义上是可行的。然而,它现在不允许JScrollPane栏出现。我不知道这是为什么,但目前正在调查。但至少组件又开始绘图了。

看起来您正在取得进展,但您可能也希望看到

Martijn Courtaux的分析是正确的:您应该覆盖
paintComponent()
。另外,混合AWT和Swing组件也是个坏主意。这两种观点在本文中都有讨论

滚动不应导致闪烁。下面是一个滚动组件网格并在背景上绘制棋盘的示例

import java.awt.*;
import javax.swing.*;

public class Scrolling extends JFrame {

    private static final int MAX = 8;
    private static final int SIZE = 480;
    private static final Color light = new Color(0x40C040);
    private static final Color dark  = new Color(0x408040);

    private static class MyPanel extends JPanel {

        public MyPanel() {
            super(true);
            this.setLayout(new GridLayout(MAX, MAX, MAX, MAX));
            this.setPreferredSize(new Dimension(SIZE, SIZE));
            for (int i = 0; i < MAX * MAX; i++) {
                this.add(new JLabel(String.valueOf(i), JLabel.HORIZONTAL));
            }
        }

        @Override
        public void paintComponent(final Graphics g) {
            int w = this.getWidth()/MAX;
            int h = this.getHeight()/MAX;
            for (int row = 0; row < MAX; row++) {
                for (int col = 0; col < MAX; col++) {
                    g.setColor((row + col) % 2 == 0 ? light : dark);
                    g.fillRect(col * w, row * h, w, h);
                }
            }
        }
    }

    public Scrolling() {

        this.setLayout(new BorderLayout());
        final MyPanel panel = new MyPanel();
        final JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        this.add(scrollPane, BorderLayout.CENTER);
        this.pack();
        this.setSize(SIZE - SIZE / 3, SIZE - SIZE / 3);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Scrolling().setVisible(true);
            }
        });
    }
}
import java.awt.*;
导入javax.swing.*;
公共类滚动扩展了JFrame{
专用静态最终整数最大值=8;
私有静态最终整数大小=480;
专用静态最终颜色灯=新颜色(0x40C040);
专用静态最终颜色暗=新颜色(0x408040);
私有静态类MyPanel扩展了JPanel{
公共事务委员会(){
超级(真);
这个.setLayout(新的GridLayout(MAX,MAX,MAX,MAX));
此.setPreferredSize(新维度(大小,大小));
对于(int i=0;i最终JScrollPane滚动窗格=新JScrollPane(面板);
scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
添加(滚动窗格,BorderLayout.CENTER);
这个包();
此.setSize(大小-大小/3,大小-大小/3);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此.setLocationRelativeTo(空);
}
公共静态void main(最终字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
新建滚动().setVisible(true);
}
});
}
}

看起来您正在取得进展,但您可能也希望看到进展

Martijn Courtaux的分析是正确的:您应该覆盖
paintComponent()
。另外,混合AWT和Swing组件也是个坏主意。这两种观点在本文中都有讨论

滚动不应导致闪烁。下面是一个滚动组件网格并在背景上绘制棋盘的示例

import java.awt.*;
import javax.swing.*;

public class Scrolling extends JFrame {

    private static final int MAX = 8;
    private static final int SIZE = 480;
    private static final Color light = new Color(0x40C040);
    private static final Color dark  = new Color(0x408040);

    private static class MyPanel extends JPanel {

        public MyPanel() {
            super(true);
            this.setLayout(new GridLayout(MAX, MAX, MAX, MAX));
            this.setPreferredSize(new Dimension(SIZE, SIZE));
            for (int i = 0; i < MAX * MAX; i++) {
                this.add(new JLabel(String.valueOf(i), JLabel.HORIZONTAL));
            }
        }

        @Override
        public void paintComponent(final Graphics g) {
            int w = this.getWidth()/MAX;
            int h = this.getHeight()/MAX;
            for (int row = 0; row < MAX; row++) {
                for (int col = 0; col < MAX; col++) {
                    g.setColor((row + col) % 2 == 0 ? light : dark);
                    g.fillRect(col * w, row * h, w, h);
                }
            }
        }
    }

    public Scrolling() {

        this.setLayout(new BorderLayout());
        final MyPanel panel = new MyPanel();
        final JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.getHorizontalScrollBar().setUnitIncrement(16);
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        this.add(scrollPane, BorderLayout.CENTER);
        this.pack();
        this.setSize(SIZE - SIZE / 3, SIZE - SIZE / 3);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
    }

    public static void main(final String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Scrolling().setVisible(true);
            }
        });
    }
}
import java.awt.*;
导入javax.swing.*;
公共类滚动扩展了JFrame{
专用静态最终整数最大值=8;
私有静态最终整数大小=480;
专用静态最终颜色灯=新颜色(0x40C040);
专用静态最终颜色暗=新颜色(0x408040);
私有静态类MyPanel扩展了JPanel{
公共事务委员会(){
超级(真);
这个.setLayout(新的GridLayout(MAX,MAX,MAX,MAX));
此.setPreferredSize(新维度(大小,大小));
对于(int i=0;i最终JScrollPane滚动窗格=新JScrollPane(面板);
scrollPane.getHorizontalScrollBar().setUnitIncremen