Java JPanel 未显示在我的 JFrame 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18872287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:48:42  来源:igfitidea点击:

JPanel is not displaying in my JFrame

javaswingjpanel

提问by Michael Nana

so I'm playing with JPanels and JFrames and I'm noticing that the JPanel I created is not showing displaying when I add it to a Jframe object. Note, that when I created a JPanel in my Jframe constructor giving the jpanel parameters before being added to the Jframe, it worked. However now I'm using a JPanel object I created and it's not working anymore. This is what I have done.

所以我在玩 JPanels 和 JFrames,我注意到当我将它添加到 Jframe 对象时,我创建的 JPanel 没有显示。请注意,当我在我的 Jframe 构造函数中创建一个 JPanel 时,在添加到 Jframe 之前给出 jpanel 参数,它起作用了。但是现在我正在使用我创建的 JPanel 对象,它不再工作了。这就是我所做的。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyGui extends JFrame {

    MyMouseListener listen = new MyMouseListener();

    public MyGui() {
        setSize(500, 500);
        //setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        getContentPane().setBackground(Color.WHITE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        Panel panel = new Panel();
        add(panel, BorderLayout.WEST);
        //setVisible(true);
        show();
    }

    public static void main(String[] a) {
        MyGui gui = new MyGui();
    }
}

class Panel extends JPanel {

    MyMouseListener listen = new MyMouseListener();

    public Panel() {
        setPreferredSize(new Dimension(300, 300));
        addMouseListener(listen);
        setBackground(Color.YELLOW);
        setLayout(new GridLayout(3, 1));
    }

    public void paint(Graphics g) {
        super.paintComponents(g);
        g.drawOval((int) Math.round(listen.p.getX()),
                (int) Math.round(listen.p.getX()), 1, 1);
    }
}

class MyMouseListener implements MouseListener {

    Point p = new Point();

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("Mouse was clicked");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        p = e.getPoint();
        System.out.println(p);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}

EDIT: Actually I think I've found the error. The JPanel has it's paint method which when deleted allows the Jframe to show the panel. However I need to be able to draw stuff on the JPanel.

编辑:其实我想我已经找到了错误。JPanel 有它的绘制方法,删除时允许 Jframe 显示面板。但是我需要能够在 JPanel 上绘制东西。

采纳答案by Insane Coder

its

它的

super.paintComponent(g);

super.paintComponent(g);

Advice:

建议:

1)You are making things unnecessarily complex. e.g to close the window you should use

1)你让事情变得不必要地复杂。例如关闭你应该使用的窗口

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

instead of the call to System.exit(0);and using window listeners

而不是调用System.exit(0);和使用窗口侦听器

2)As said by @mKorbel , you should use SwingUtilities.invokeLaterto start your gui as Java GUIs are supposed to run on EDT(Event Dispatch Thread) and should not run on main thread.

2)正如@mKorbel 所说,您应该使用SwingUtilities.invokeLater来启动您的 gui,因为 Java GUI 应该在 EDT(事件调度线程)上运行,而不应该在主线程上运行。

回答by Hugo G.

Did you try to set the layout manager and add the panel to the contentPane instead of the JFrame itself ?

您是否尝试设置布局管理器并将面板添加到 contentPane 而不是 JFrame 本身?

getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.WEST);

回答by Antoniossss

Default Layout manager for frame is FlowLayoutnot BorderLayout. Try to setLayout(new BorderLayout())in your MyGuicontructor.

框架的默认布局管理器FlowLayout不是BorderLayout. 尝试setLayout(new BorderLayout())在您的MyGui构造函数中。

回答by Sorontur

you didn't set a Layout to your content pane. try someting like getContentPane.setLayout(new Layout())

您没有为内容窗格设置布局。尝试像getContentPane.setLayout(new Layout())

View the oracle docs for details about layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html

有关布局管理器的详细信息,请查看 oracle 文档:http: //docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html

hope this helpgs

希望这有帮助

回答by mKorbel

1) super.paintComponents(g); inside paint()could be

1) super.paintComponents(g); 里面paint()可能是

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

    ....
}

2) don't to set any size setSize(500,500); or setPreferredSize(new Dimension(300, 300));, to use pack()and then (uncomment) setVisible(true)for JFrame and to override getPreferredSize()for JPanel

2)不要设置任何尺寸setSize(500,500);或者setPreferredSize(new Dimension(300, 300));,使用pack()和然后(取消注释)setVisible(true)为JFrame的和重写getPreferredSize()JPanel

3) MyGui gui=new MyGui();inside public static void main(String []a){, should be wrapped into invokeLater, more see in Oracle tutorial Initial Thread

3) MyGui gui=new MyGui();里面public static void main(String []a){,要包裹进去invokeLater,更见里面Oracle tutorial Initial Thread