椭圆形的五个顶行被擦除。为什么? [英] the five top rows of the oval are erased. why?

查看:126
本文介绍了椭圆形的五个顶行被擦除。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑,我试着将椭圆设置在红色的JPanel里面,而椭圆形的像素太低了5个像素。我试图将y1更改为-5,所以它将完全在JPanel中,椭圆形移动到正确的位置,但椭圆形的五个顶行被擦除了为什么会发生这些事情?如何将椭圆放置在JPanel中间?

I'm really confused, I tried setting the oval to be exactly inside the red JPanel and the oval went 5 pixels too low. I tried to change y1 to -5 so it would be exactly in the JPanel, the oval moved to the right place but the five top rows of the oval were erased why did these things happen? and how do I place the oval in the middle of the JPanel?

package il.co.atlantis;

import javax.swing.*;

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


public class PanelExample_Extended{

public static final int WID = 20, HEI = 20;
public static int x1 = 0, y1 = -5;

public class MyGraphics extends JComponent {

    private static final long serialVersionUID = 7526472295622776147L;

    MyGraphics() {
        setPreferredSize(new Dimension(20,20));
    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillOval(x1, y1, WID, HEI);
    }

}

 public JPanel createContentPane (){

    JPanel totalGUI = new JPanel();
    totalGUI.setLayout(null);

    JPanel redPanel = new JPanel();
    redPanel.setBackground(Color.red);
    redPanel.setLocation(10, 10);
    redPanel.setSize(20,20);
    MyGraphics tr = new MyGraphics();
    tr.setLocation(0, 0);
    redPanel.add(tr);
    totalGUI.add(redPanel);

    JPanel bluePanel = new JPanel();
    bluePanel.setBackground(Color.blue);
    bluePanel.setLocation(220, 10);
    bluePanel.setSize(50, 50);
    totalGUI.add(bluePanel);

    totalGUI.setOpaque(true);
    return totalGUI;
}

private static void createAndShowGUI() {

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("[=] ??? [=]");


    PanelExample_Extended demo = new PanelExample_Extended();
    frame.setContentPane(demo.createContentPane());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(290, 100);
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
}


推荐答案

你从-5开始绘制,你从JComponent中抽取出来,所以图像的那部分不会显示是有意义的。我认为你的问题源于试图在默认使用FlowLayout的JPanel上使用绝对定位(一种避免的事情)。

If you start drawing at -5, you're drawing off of the JComponent, so it makes sense that that portion of the image won't show. I think that your problem stems from trying to use absolute positioning (a thing to avoid) on a JPanel that by default uses FlowLayout.

如果您更好地描述了您正在尝试做的事情,并且可能会给我们提供您想要的图片,我们可能会更好地帮助您。

If you describe better what you're trying to do and perhaps give us images of what you want, we may be able to help better.

试着给你的红色JPanel一个BorderLayout并看看会发生什么。

Try giving your red JPanel a BorderLayout and seeing what happens.

  JPanel redPanel = new JPanel(new BorderLayout());

但最重要的是,请仔细阅读并理解布局管理器。

But most important, please read up on and understand the layout managers.

这篇关于椭圆形的五个顶行被擦除。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆