【问题标题】:Strange display issue with JFrameJFrame 出现奇怪的显示问题
【发布时间】:2012-01-30 10:01:55
【问题描述】:

在下面的简单代码中,我只是创建了一个 Frame,并在其中添加了 JPanelmenubar

public class MainFrame extends JFrame {
    private DrawPanel drawPanel;
    public MainFrame()
    {
        super("Coordinate Geometry Visualiser");
        drawPanel = new DrawPanel();
        add(drawPanel);

        JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');

        JMenuItem newItem = new JMenuItem("New");
        newItem.setMnemonic('N');
        fileMenu.add(newItem);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);

        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic('E');
        menuBar.add(editMenu);
    }
}

绘制面板代码-

public class DrawPanel extends JPanel {
    public DrawPanel()
    {
    }
    public void paintComponent(Graphics g) 
    {
        super.paintComponents(g);
        setBackground(Color.BLACK);
        g.setColor(Color.RED);
        g.drawLine(100, 50, 150, 100);
    }
}

最后是main()的应用程序

public class CGVApplication {
    public static void main(String[] args) {
        MainFrame appFrame = new MainFrame();
        appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        appFrame.setSize(300, 275);
        appFrame.setVisible(true);
    }
}

eclipse 中运行应用程序时,这就是我得到的 -

为什么是双 menubarline?这很烦人。在循环浏览应用程序时或出现弹出窗口时,重新绘制的窗口很好(右侧图像)。

在我的 DrawPanel paintComponent 我也将背景设置为黑色,但没有效果?

以上两个问题的原因是什么?请帮忙!

【问题讨论】:

  • 您通常在初始化组件时设置背景颜色,而不是在绘制方法中。尝试将该行移至构造函数。 - 至于倍增问题:这只是一个猜测,但请尝试在打开框架之前添加此行:System.setProperty("sun.java2d.noddraw", "true");
  • 嘿,谢谢,将这一行setBackground(Color.BLACK); 从绘图移动(或评论)到构造函数解决了双行和菜单的问题。但是面板仍然不是黑色的。也许您应该添加您的评论作为答案。顺便说一句 setProperty 没有帮助。

标签: java eclipse swing graphics paintcomponent


【解决方案1】:

您正在调用 Container.paintComponents() 方法。必须是super.paintComponent(g)

@Override
public void paintComponent(Graphics g) 
{
    super.paintComponent(g); //super.paintComponents(g);
    setBackground(Color.BLACK);
    g.setColor(Color.RED);
    g.drawLine(100, 50, 150, 100);
}

【讨论】:

  • 解决了问题!是的@JBNizet - 请取消删除您的帖子。
  • 我没有删除它。它提供的信息更多,但没有像你的那样正确解决问题。
  • @AVD:非常正确,如果 eclipse 没有让生活变得如此轻松,我不会犯这样的错误!
【解决方案2】:

javadoc 提到了这一点

  • isOpaque 必须为真
  • 根据 L&F,此属性可能会被忽略
  • JComponent 的子类必须重写paintComponent 以遵守此属性。

在构造函数中放入setBackground,在paintComponent中添加这两行代码(在画红线之前)使面板变黑。

g.setColor(getBackground());
g.fillRect(getX(), getY(), getWidth(), getHeight());

还请注意,应始终在 EDT 中创建和修改 Swing 组件。你的主要方法应该是这样的:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            MainFrame appFrame = new MainFrame();
            appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            appFrame.setSize(300, 275);
            appFrame.setVisible(true);
        }
    });
}

【讨论】:

  • 你是在建议多线程吗?嗯,一定要学。
  • 我不是在建议多线程。所有的 swing 组件只能由一个线程使用:事件调度线程。您的代码在主线程中使用组件。见docs.oracle.com/javase/6/docs/api/javax/swing/…,每个swing组件的javadoc都链接到它。
猜你喜欢
  • 2011-06-29
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2018-03-09
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多