简体   繁体   English

Jframe内的Jpanel涂有油漆

[英]Jpanel inside Jframe with paint

hi i'm triyng to do something like this example 嗨,我正准备做这个例子

but i'm always getting the cross or in top-west or it doesn't appear and don't know why. 但是我总是在十字架上或在最西端,或者它没有出现,也不知道为什么。 I try to see the borderLayout and some stackoverflow explanations examples but didn´t find anything related. 我尝试查看borderLayout和一些stackoverflow解释示例,但未找到任何相关内容。 can someone explain to me what i'm doing wrong and why please? 有人可以向我解释我在做错什么,为什么呢?

public Base() {
        super("Menu");
        JPanel p = createPanel();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        setSize(screen.width, screen.height);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(p);
    }

    private JPanel createPanel() {
        JPanel P = new JPanel() {
            public void paint(Graphics g) {
                super.paint(g);
                Graphic gr = new Graphicimpl();
                g.drawLine(gr.PositionX(-25.0), gr.PositionY(0.0), gr.PositionX(25.0), gr.PositionY(0.0));
                g.drawLine(gr.PositionX(0.0), gr.PositionY(25.0), gr.PositionX(0.0), gr.PositionY(-25.0));

            }
        };

        //P.setSize(50, 50);
        //P.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300));
        return P;
    }
}
public class Graphicimpl implements Graphic{
    int FACTOr_ESCALACION = 10;


    public  int PositionX(Double x) {
        return (int) ((x *  FACTOr_ESCALACION) + 320);
    }

    @Override
    public int PositionY(Double y) {
        return (int) ( - (y * FACTOr_ESCALACION ) +240);
    }


}

public interface Graphic {
    int PositionX(Double x);
    int PositionY(Double y);
}
public class Main {
    public static void main(String args[]) {
        Base base=new Base();
    }

}

The location of the cross depends on the coordinates you have inserted. 十字的位置取决于您插入的坐标。 Since you are inserting them manually. 由于您是手动插入它们。 You can test the program with different coordinates until the cross shows where you want it to. 您可以使用不同的坐标来测试程序,直到十字显示您想要的位置为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM