java JFrame 图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13075417/
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-10-31 11:24:29  来源:igfitidea点击:

java JFrame graphics

javaswinggraphicsjframepaint

提问by aditya parikh

I have the following simple code in a JFrame constructor

我在 JFrame 构造函数中有以下简单代码

    super(name);
    setBounds(0,0,1100,750);
    setLayout(null);


    setVisible(true);

    g = this.getGraphics();
    int[] x =new int[]{65,  122,  77,  20, };
    int[] y =new int[]{226,  258, 341,  310};
    g.setColor(Color.RED);  
    g.drawPolygon (x, y, x.length);
    System.out.println(g);

I get the output on console as:

我在控制台上得到的输出为:

sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0]]

sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0 ]]

But no red polygon drawn on JFrame but just the blank JFrame.

但是在 JFrame 上没有绘制红色多边形,而只是空白的 JFrame。

Why ??

为什么 ??

回答by David Kroukamp

  • Dont overridepaint(..)in JFrame

  • Rather add customJPanelwith overridden paintComponent(Graphics g)to JFrame

  • Dont use Null/AbsoluteLayoutuse an appropriate LayoutManager

  • Dont call setBounds(..)on JFrameinstance (not that its not allowed but cant see it being relevant in this application)

  • Dont forget to use EDTfor creating and changing GUI components:

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
               public void run() {
                    Test test = new Test();
               }
    });
    
  • 不要覆盖paint(..)JFrame

  • 而是添加自定义JPanel与覆盖paintComponent(Graphics g)JFrame

  • 不要使用Null/AbsoluteLayout使用适当的LayoutManager

  • 不要调用setBounds(..)上的JFrame实例(均不表明其不允许的,但不能看到它是在该应用程序相关的)

  • 不要忘记使用EDT来创建和更改 GUI 组件:

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
               public void run() {
                    Test test = new Test();
               }
    });
    

you would then do something like this:

然后你会做这样的事情:

public class Test {

    /**
     * Default constructor for Test.class
     */
    public Test() {
        initComponents();
    }

    public static void main(String[] args) {

        /**
         * Create GUI and components on Event-Dispatch-Thread
         */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test test = new Test();
            }
        });
    }

    /**
     * Initialize GUI and components (including ActionListeners etc)
     */
    private void initComponents() {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jFrame.add(new MyPanel());

        //pack frame (size JFrame to match preferred sizes of added components and set visible
        jFrame.pack();
        jFrame.setVisible(true);
    }
}

class MyPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int[] x = new int[]{65, 122, 77, 20};
        int[] y = new int[]{226, 258, 341, 310};
        g.setColor(Color.RED);
        g.drawPolygon(x, y, x.length);
    }

    //so our panel is the corerct size when pack() is called on Jframe
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }
}

which produces:

它产生:

enter image description here

在此处输入图片说明

回答by Dan D.

You should better override paint(Graphics g)or paintComponent(Graphics g)than the approach you are trying. Add the lines below and remove the lines after setVisiblein your code.

您应该更好地覆盖paint(Graphics g)paintComponent(Graphics g)比您正在尝试的方法。添加下面的行并删除setVisible代码中的后面的行。

public void paint(Graphics g) {
  int[] x =new int[]{65,  122,  77,  20};
  int[] y =new int[]{226,  258, 341,  310};
  g.setColor(Color.RED);  
  g.drawPolygon (x, y, x.length);
}