0

I'm working on a login screen for my game. I have a total of two images on it. One is a splash screenshot and the other is the background image. I'm using BufferedImages to render the images to the screen.

The problem I get is that when I add a standard button to the Canvas, the button takes up the whole window, and evidently, I don't want that.

I would post a picture, but alas, I do not have "enough reputation" to do that. Here's a look at my code though:

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();
}
}

Once again, my only problem is that I keep getting a enlarged button.

Thanks in advance, I know you guys are busy and whatnot and I appreciate taking the time to look over and help answer my questions.

P.S. Does anyone know how to make a password field with AWT? I'll also need that too. ;)

4

2 回答 2

1

你说:

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

您正在尝试将组件直接添加到使用 BorderLayout 的容器中,可能是顶级窗口的 contentPane,因此默认情况下会添加 BorderLayout.CENTER 并填充容器。

解决方案:首先将您的 JButton(再次使用 Swing 组件)添加到 JPanel(默认使用 FlowLayout),然后将其添加顶层窗口。

同样,没有必要混合 AWT 和 Swing 组件,而且事实上有强烈的理由不这样做。我建议您坚持为您的 GUI 使用所有 Swing 组件。


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

同样,不要使用 AWT,而是使用 Swing JPasswordField。

于 2013-10-13T19:22:43.750 回答
1

解决方案:首先将您的 JButton(再次使用 Swing 组件)添加到 JPanel(默认使用 FlowLayout),然后将其添加到顶层窗口。

您可以将框架的布局管理器更改为 FlowLayout,使其行为类似于 JPanel。

frame.setLayout(new FlowLayout());
于 2013-10-13T19:46:11.110 回答