gpt4 book ai didi

java - 如何将按钮添加到 Canvas 而不让按钮调整大小?

转载 作者:行者123 更新时间:2023-12-02 06:38:53 26 4
gpt4 key购买 nike

我正在为我的游戏制作登录屏幕。我上面总共有两张图片。一张是初始屏幕截图,另一张是背景图像。我正在使用 BufferedImages 将图像渲染到屏幕上。

我遇到的问题是,当我向 Canvas 添加标准按钮时,该按钮占据了整个窗口,显然,我不希望这样。

我会发布一张照片,但可惜的是,我没有“足够的声誉”来做到这一点。不过,请看一下我的代码:

import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;
import javax.swing.UIManager;

public class Login extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

private static final int WIDTH = 495;
private static final int HEIGHT = 307;
private static final int SCALE = 2;
private final Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

private BufferedImage splash = new BufferedImage(315, 177, BufferedImage.TYPE_INT_RGB);
public int[] splashPixels = ((DataBufferInt) splash.getRaster().getDataBuffer()).getData();

private Thread thread;
public static boolean isRunning = false;

JFrame frame;
MainMenu menu;
Splash splashscreen;

Button login;
Button register;
TextField username;

private Login() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exc) {
exc.printStackTrace();
}

frame = new JFrame("Game Login");
menu = new MainMenu(WIDTH, HEIGHT, "/login/login_screen.png");
splashscreen = new Splash(315, 177, "/login/splash.png");

frame.setSize(size);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);

setPreferredSize(size);
frame.add(this);

login = new Button("Login");
login.setBounds(0, 0, getWidth(), getHeight());

frame.add(login);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void begin() {
createBufferStrategy(2);
thread = new Thread(this);
thread.start();
isRunning = true;
}

private void finish() throws InterruptedException {
isRunning = false;
thread.join();
}

private void updateLogin() {
for (int a = 0; a < pixels.length; a++) {
pixels[a] = menu.pixels[a];
}
}

private void renderLogin() {
BufferStrategy buffer = getBufferStrategy();
Graphics gfx = buffer.getDrawGraphics();

for (int a = 0; a < splashPixels.length; a++) {
splashPixels[a] = splashscreen.splashPixels[a];
}

gfx.drawImage(image, 0, 0, getWidth(), getHeight(), null);
gfx.drawImage(splash, 320, 37, 625, 340, null);

gfx.setColor(Color.WHITE);
gfx.drawString("Game Co © 2013", 3, (getHeight() - 4));
gfx.drawString("\"Game\" is a trademark of Blah-Blah-Blah.", (getWidth() - 268), (getHeight() - 3));

gfx.dispose();
buffer.show();
}

public void run() {
while (isRunning == true) {
updateLogin();
renderLogin();
}

try {
finish();
} catch (InterruptedException exc) {
exc.printStackTrace();
}
}

public static void main(String[] args) {
Login login = new Login();
login.begin();
}
}

再一次,我唯一的问题是按钮不断放大。

提前致谢,我知道你们很忙等等,我很感谢您花时间查看并帮助回答我的问题。

附注有谁知道如何用 AWT 制作密码字段?我也需要那个。 ;)

最佳答案

Solution: add your JButton (again use Swing components) First to a JPanel (which uses FlowLayout by default), and then add that to the top level window.

您可以将框架的布局管理器更改为 FlowLayout,这样它的行为就像 JPanel 一样。

frame.setLayout(new FlowLayout());

关于java - 如何将按钮添加到 Canvas 而不让按钮调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19349155/

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