2013-10-13 46 views
0

我正在爲我的遊戲登錄屏幕。我總共有兩個圖像。一個是屏幕截圖,另一個是背景圖片。我使用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(); 
} 
} 

再次,我唯一的問題是,我一直得到一個放大按鈕。

在此先感謝,我知道你們很忙,但我很欣賞花時間瀏覽並幫助回答我的問題。

P.S.有誰知道如何使用AWT創建密碼字段?我也需要這個。 ;)

+0

爲什麼要將AWT與Swing混合?這是毫無意義的,迫使你不必要地限制你能做的事情。爲什麼不相反只是堅持所有的Swing組件? –

回答

1

解決方案:添加您的JButton(再次使用Swing組件)先到JPanel(默認使用FlowLayout),然後將其添加到頂層窗口。

您可以將您的框架的佈局管理器更改爲FlowLayout,使其表現得像JPanel一樣。

frame.setLayout(new FlowLayout()); 
+0

我會1+亞那,因爲這也會起作用。 –

1

幽州:

我得到的問題是,當我添加一個標準按鈕,畫布,按鈕佔據了整個窗口,看樣子,我不希望出現這種情況。

你試圖直接將組件添加到正在使用的BorderLayout,頂層窗口的可能的contentPane的容器,並且默認情況下它被添加BorderLayout.CENTER並填充容器。

解決方案:將您的JButton(再次使用Swing組件)首先添加到JPanel(默認情況下使用FlowLayout),然後將添加到頂層窗口。

再一次,沒有必要混合使用AWT和Swing組件,事實上有強烈的理由不這樣做。我建議你堅持使用你的GUI的所有Swing組件。


有誰知道如何使密碼字段與AWT?我也需要這個。 ;)

再次,不要使用AWT,而是使用Swing JPasswordField。

+0

但是,如何將緩衝策略和緩衝圖像添加到JPanel?雖然我對JPanel不太瞭解,但我更喜歡Canvas。 – Payneztastic

+0

@Payneztastic:您通常不必擔心緩衝策略,因爲默認情況下JPanel和所有Swing組件都是雙緩衝。但無論如何,你應該學習如何做Swing圖形/ GUI。從1995年的圖書館進入2003年的圖形庫。這不是最前沿的,但Swing比AWT工作得更好。 –

+0

默認情況下JPanelis爲雙緩衝。你可以簡單地創建兩個或更多的你自己的BufferedImages,繪製到一個,渲染另一個,並根據需要切換它們 – MadProgrammer

相關問題