JFrame 的奇怪显示问题

发布于 2024-12-23 05:45:56 字数 1767 浏览 0 评论 0原文

在下面的简单代码中,我只是创建一个框架,并向其添加一个 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 内运行应用程序,这就是我得到的 - 在此处输入图像描述 在此处输入图像描述

为什么有双菜单栏线条?这很烦人。在循环应用程序或出现弹出窗口时,重新绘制的窗口很好(右侧图像)。

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

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

In the following simple code I simply create a Frame, and add a JPanel and menubar to it.

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);
    }
}

Draw Panel code -

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);
    }
}

and finally the application with 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);
    }
}

On running the application inside eclipse, this is what i get -
enter image description here enter image description here

Why the double menubar and line? This is very annoying. On cycling through applications or when a popup comes the redrawn window is fine (Right side image).

Also in my DrawPanel paintComponent i have set background as black, but no effect?

What is the reason for above two issues? Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-12-30 05:45:56

您正在调用 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);
}

You are calling Container.paintComponents() method. It must be 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);
}
忆悲凉 2024-12-30 05:45:56

javadoc 提到

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

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

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

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

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);
        }
    });
}

The javadoc mentions that

  • isOpaque must be true
  • that depending on the L&F, this property may be ignored
  • that subclasses of JComponent must override paintComponent to honor this property.

Putting setBackground in the constructor, and adding these two lines of code in paintComponent (before painting the red line) makes the panel black.

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

Also note that Swing components should always be created and modified in the EDT. Your main method should be like this:

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);
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文