java - JPanel重绘问题

标签 java swing jframe jpanel

我有一个 JFrame,它在 BorderLayout 中包含 2 个 JPanel 子类和 2 个 JLabel。其中一个 JPanel 包含 JButton,另一个用于显示图形。 JLabel 位于南北,按钮 JPanel 位于西边,显示 JPanel 位于中间。

显示 JPanel 需要不断刷新,所以我通过 Swing 计时器生成的 Action 事件调用它的 repaint() 方法。我还覆盖了它的 paintComponent() 方法来绘制。

不是显示我绘制的内容,而是将“JFrame 的内容”绘制到显示 JPanel 上。我知道我可以在绘制之前使用 g.fillRect() 或 super.paintComponent() 简单地“清除”显示 JPanel。

我很好奇为什么会这样。

我正在使用 jdk 1.6u27。下面是我的代码:

package test;

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

public class Main {

public static void main(String[] args) {
    Simulation sim = new Simulation();

    }
}

class Simulation extends JFrame {

    public JLabel state;
    private JLabel id;
    private ButtonPanel control;
    private Display display;

    public Simulation() {
        id = new JLabel("Test");
        state = new JLabel("Test");
        control = new ButtonPanel();
        display = new Display(this);

        this.setLayout(new BorderLayout());
        this.add(id, BorderLayout.NORTH);
        this.add(control, BorderLayout.WEST);
        this.add(display, BorderLayout.CENTER);
        this.add(state, BorderLayout.SOUTH);

        this.setSize(500, 600);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public ButtonPanel getControl() {
        return this.control;
    }
}

class ButtonPanel extends JPanel implements ActionListener {

    public JButton b[] = new JButton[8];
    public boolean bp[] = new boolean[8];

    public ButtonPanel() {
        this.setLayout(new GridLayout(8, 1));

        for (int i = 0; i < b.length; i++) {
            b[i] = new JButton(""+i);
            b[i].addActionListener(this);
            bp[i] = false;
            this.add(b[i]);
        }
    }

    public void actionPerformed(ActionEvent e) {
        //do something
    }
}

class Display extends JPanel implements ActionListener {

    private Timer tm;
    private int yco;
    private Simulation sim;

    public Display(Simulation sim) {
        tm = new Timer(100, this);
        tm.start();

        yco = 0;

        this.sim = sim;
    }

    @Override
    public void paintComponent(Graphics g) {
        //draw something
        g.drawLine(0, yco, 100, 100);
    }

    public void actionPerformed(ActionEvent e) {
        yco ++;
        this.repaint();
    }
}

screenshot

最佳答案

如果没有 super.paintComponent(g),结果取决于平台默认的 opacity JPanel UI 委托(delegate)的属性,PanelUI。我的恰好是 true,但您可以按照下面的建议在您的平台上进行试验。

附录:“如果您不遵守 opaque property,您可能会看到视觉 artifacts。”— paintComponent() . artifact您观察到的情况会因平台而异,但并非非典型。实际上,您违背了绘制每个像素的 promise ,您会看到某个缓冲区中剩余的内容。

Main

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Simulation sim = new Simulation();
            }
        });
    }
}

class Simulation extends JFrame {

    public JCheckBox state;
    private JLabel id;
    private ButtonPanel control;
    private Display display;

    public Simulation() {
        id = new JLabel("Test");
        state = new JCheckBox("Opaque");
        control = new ButtonPanel();
        display = new Display(this);

        this.setLayout(new BorderLayout());
        this.add(id, BorderLayout.NORTH);
        this.add(control, BorderLayout.WEST);
        this.add(display, BorderLayout.CENTER);
        this.add(state, BorderLayout.SOUTH);
        state.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                display.setOpaque(e.getStateChange() == ItemEvent.SELECTED);
            }
        });
        state.setSelected(true);

        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public ButtonPanel getControl() {
        return this.control;
    }
}

class ButtonPanel extends JPanel {

    private static final int N = 8;
    private List<JToggleButton> list = new ArrayList<JToggleButton>(N);

    public ButtonPanel() {
        this.setLayout(new GridLayout(0, 1));
        for (int i = 0; i < N; i++) {
            final JToggleButton b = new JToggleButton(String.valueOf(i));
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //System.out.println(b.isSelected());
                }
            });
            list.add(b);
            this.add(b);
        }
    }
}

class Display extends JPanel {

    private Simulation sim;
    private Timer tm;
    private int yco;

    public Display(Simulation sim) {
        this.setPreferredSize(new Dimension(320, 320));
        this.setOpaque(true);
        this.sim = sim;
        tm = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                yco++;
                repaint();
            }
        });
        tm.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        //super.paintComponent(g);
        g.drawLine(0, yco, getWidth() / 2, getHeight() / 2);
    }
}

关于java - JPanel重绘问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7213178/

相关文章:

java - 将 GUI Designer JPanel 添加到 JFrame

java - 选项卡组件消耗鼠标,因此选项卡不会更改

java - 如何从声明 JFrame 的类之外的类中关闭 JFrame

java - 如何将 JPanel 作为参数传递给方法?

java - 如何禁用 JFrame 窗口菜单栏中的图标化按钮?

java - 使用 Java 将 JSON 数据推送到文件

java - 小程序无法再打电话回家

java - 使用JDK 1.7编译Android项目

java - 使用 JavaFX 显示 JTable

java - 如何在 Swing 应用程序的后台加载图像?