gpt4 book ai didi

java - 在全屏图形之上添加一个 JComponent

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:40 26 4
gpt4 key购买 nike

我有一个全屏程序,它为我正在制作的游戏绘制了很多东西。 Canvas 是整个窗口,它应该是。现在我想在我的窗口中添加一个按钮或文本字段(JButton 和 JTextField 等...)。但是,当我添加一个按钮或其他任何东西时,它们就会开始 Canvas 。

你知道如何让按钮显示在 Canvas 上,以便我可以点击它吗?

编辑:绘制代码:

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。

最佳答案

很难从代码片段中看出很多(请考虑在以后发布 SSCCE),但是:

  1. 不要在这个千禧年编写 AWT。
  2. 不要将 Swing 与 AWT 混合使用(至少在 Java 7 之前)
  3. 不要在draw() 方法(大概是从paintComponent() 方法调用)中加载图像。
  4. 请注意,Swing 中的自定义绘画可以在 JComponentJPanel 中完成,如果类是后者,它可以添加其他组件的布局。

关于java - 在全屏图形之上添加一个 JComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com