我有一个全屏程序,可以为我制作的游戏绘制很多内容.画布是整个窗口,应该是整个窗口.现在,我想在窗口中添加一个按钮或文本字段(JButton和JTextField等…).但是,当我添加一个按钮或其他任何东西时,他们开始使用画布.
您是否知道如何使按钮显示在画布上,所以我可以单击它?
编辑:
绘图代码:
public synchronized void draw(Graphics2D g) {
if (!welcome)
g.drawImage(background,0,0,null);
if (welcome) {
g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
}
else if (gettingName) {
setFont (new Font("Verdana", Font.ITALIC, 40));
g.draw3DRect(center.x -325, center.y -50, 650, 100, true);
g.drawString("Please enter your name", center.x -250, center.y -120);
g.drawString(name, center.x -315, center.y);
}
if (game) {
for (Player p: player) {
int x = center.x;
int y = center.y;
switch (p.getID()) {
case 1:
x -= name.length()*10;
y *= 1.4;
break;
case 2:
x -= name.length()*10;
y /= 1.4;
break;
case 3:
x *= 1.4;
x -= name.length()*10;
break;
case 4:
x *= 0.6;
x -= name.length()*10;
break;
default:
System.out.println("Error 2");
}
g.drawString(p.getName(),x,y);
}
for (int x = 0; x<13; x++) {
g.drawImage(diamonds[x].getImage(), (int) diamonds[x].getX(), (int) diamonds[x].getY(), null);
g.drawImage(clubs[x].getImage(), (int) clubs[x].getX(), (int) clubs[x].getY(), null);
g.drawImage(hearts[x].getImage(), (int) hearts[x].getX(), (int) hearts[x].getY(), null);
g.drawImage(spades[x].getImage(), (int) spades[x].getX(), (int) spades[x].getY(), null);
}
g.drawImage(put.getImage(), (int) put.getX(), (int) put.getY(), null);
}
if (won) {
g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
}
else if (lost) {
g.drawImage(new ImageIcon (getClass().getResource("images/backgroundWelcome.jpg")).getImage(), 0,0, null);
}
}
编辑2:
我的框架是一个JFrame.