Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame窗口栏块内容_Java_Swing_Frame_Paint - Fatal编程技术网

Java JFrame窗口栏块内容

Java JFrame窗口栏块内容,java,swing,frame,paint,Java,Swing,Frame,Paint,我试图在左上角画一个圆圈。圆的某些部分与窗口的边框重叠?如何避免这种情况 public class Yard extends JFrame { public static final int WIDTH = 15; public static final int HEIGHT = 15; private static final int BLOCK_SIZE = 30; public void launch() { this.setLocation(200, 2

我试图在左上角画一个圆圈。圆的某些部分与窗口的边框重叠?如何避免这种情况

public class Yard extends JFrame {
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private static final int BLOCK_SIZE = 30;

public void launch() {
    this.setLocation(200, 200);
    this.setSize(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    this.setVisible(true);
}

@Override
public void paint(Graphics g) {
    Color c = g.getColor();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
    g.setColor(c);
    g.fillOval(0, 0, 100, 100);
}

    public static void main(String Args[]) {
        new Yard().launch();
    }
}
1) 阅读更多关于

2) 对于绘画,最好使用
JPanel
而不是
JFrame
paintComponent()
方法
JComponent
而不是
paint()

简单的例子:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

    public static void main(String... s){
        new TestFrame();
    }

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        add(new DrawPanel());
    }

    private class DrawPanel extends JPanel {

        public static final int WIDTH = 15;
        public static final int HEIGHT = 15;
        private static final int BLOCK_SIZE = 30;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Color c = g.getColor();
            g.setColor(Color.GRAY);
            g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
            g.setColor(c);
            g.fillOval(0, 0, 100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        }
    }
}
1) 阅读更多关于

2) 对于绘画,最好使用
JPanel
而不是
JFrame
paintComponent()
方法
JComponent
而不是
paint()

简单的例子:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

    public static void main(String... s){
        new TestFrame();
    }

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        add(new DrawPanel());
    }

    private class DrawPanel extends JPanel {

        public static final int WIDTH = 15;
        public static final int HEIGHT = 15;
        private static final int BLOCK_SIZE = 30;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Color c = g.getColor();
            g.setColor(Color.GRAY);
            g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
            g.setColor(c);
            g.fillOval(0, 0, 100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        }
    }
}

使用
JPanel
而不是
JFrame
来创建组件。下面是一个示例代码,您可以使用它开始

public class Yard extends JPanel {
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private static final int BLOCK_SIZE = 30;

    public void launch() {
        JFrame frame = new JFrame("Yard");
        frame.setContentPane(this);
        frame.setLocation(200, 200);
        frame.setSize(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color c = g.getColor();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        g.setColor(c);
        g.fillOval(WIDTH, HEIGHT, 100, 100);
    }

    public static void main(String Args[]) {
        new Yard().launch();
    }
}

使用
JPanel
而不是
JFrame
来创建组件。下面是一个示例代码,您可以使用它开始

public class Yard extends JPanel {
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private static final int BLOCK_SIZE = 30;

    public void launch() {
        JFrame frame = new JFrame("Yard");
        frame.setContentPane(this);
        frame.setLocation(200, 200);
        frame.setSize(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color c = g.getColor();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        g.setColor(c);
        g.fillOval(WIDTH, HEIGHT, 100, 100);
    }

    public static void main(String Args[]) {
        new Yard().launch();
    }
}

Oracle tutorial Graphics 2D中的基本内容在这里每天询问10-15次Oracle tutorial Graphics 2D中的基本内容在这里每天询问10-15次,加上一次,但g.fillRect(0,0,高度*块大小,宽度*块大小);==getHeight/Weightplus 1,但g.fillRect(0,0,高度*块大小,宽度*块大小);=身高/体重