【问题标题】:JFrame window bar block contentsJFrame窗口栏块内容
【发布时间】:2014-12-24 13:25:15
【问题描述】:

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

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

【问题讨论】:

  • Oracle 教程 Graphics2D 的基础知识在这里每天被问 10-15 次

标签: java swing frame paint


【解决方案1】:

1)阅读更多关于custom paintings in swing的信息。

2)对于绘画最好使用JPanel而不是JFramepaintComponent()方法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);
        }
    }
}

【讨论】:

  • 加一但 g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE); == 获取身高/体重
【解决方案2】:

使用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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-21
    • 2011-06-13
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多