JFrame窗口栏块内容

时间:2014-12-24 13:25:15

标签: 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, 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();
    }
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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();
    }
}