Jframe 内部的 Jpanel 与 paint
Jpanel inside Jframe with paint
嗨,我正在尝试做这样的事情example
但我总是得到十字架或在顶部西边,或者它没有出现并且不知道为什么。我尝试查看 borderLayout 和一些 Whosebug 解释示例,但没有找到任何相关内容。有人可以向我解释我做错了什么以及为什么吗?
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();
}
}
十字的位置取决于您输入的坐标。由于您是手动插入它们。您可以使用不同的坐标测试程序,直到十字显示您想要的位置。
嗨,我正在尝试做这样的事情example
但我总是得到十字架或在顶部西边,或者它没有出现并且不知道为什么。我尝试查看 borderLayout 和一些 Whosebug 解释示例,但没有找到任何相关内容。有人可以向我解释我做错了什么以及为什么吗?
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();
}
}
十字的位置取决于您输入的坐标。由于您是手动插入它们。您可以使用不同的坐标测试程序,直到十字显示您想要的位置。