如何做没有让按钮调整我一个按钮添加到画布? [英] How do I add a button to Canvas without letting the button resize?
问题描述
我正在登录屏幕为我的游戏。我一共上有两个图像。一个是溅截图,另一个是背景图像。我使用BufferedImages渲染图像在屏幕上。
我得到的问题是,当我添加一个标准按钮,画布,按钮占据了整个窗口,看样子,我不希望这样。
我会张贴图片,但很可惜,我没有足够的信誉来做到这一点。下面就来看看我的code,但:
进口java.awt.Button中;
进口java.awt.Canvas中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.Graphics;
进口java.awt.TextField的;
进口java.awt.image.BufferStrategy;
进口java.awt.image.BufferedImage中;
进口java.awt.image.DataBufferInt;进口javax.swing.JFrame中;
进口javax.swing.UIManager中;公共类扩展登录帆布实现Runnable {私有静态最后的serialVersionUID长1L =;私有静态最终诠释WIDTH = 495;
私有静态最终诠释HEIGHT = 307;
私有静态最终诠释SCALE = 2;
私人最终尺寸大小=新的外形尺寸(宽*尺,高度* SCALE);私人形象的BufferedImage =新的BufferedImage(宽度,高度,BufferedImage.TYPE_INT_RGB);
公共INT []个像素=((DataBufferInt)image.getRaster()getDataBuffer()。)的getData();私人的BufferedImage飞溅=新的BufferedImage(315,177,BufferedImage.TYPE_INT_RGB);
公众诠释[] splashPixels =((DataBufferInt)splash.getRaster()getDataBuffer()。)的getData()。私人螺纹螺纹;
公共静态布尔isRunning = FALSE;JFrame的框架;
MainMenu的菜单;
飞溅启动画面;按钮登录;
的按钮;
文本字段用户名;私人登录(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}赶上(例外EXC){
exc.printStackTrace();
} 帧=新的JFrame(游戏登录);
菜单=新的MainMenu(宽度,高度,/login/login_screen.png);
闪屏=新飞溅(315,177,/login/splash.png); frame.setSize(大小);
frame.setVisible(真);
frame.setResizable(假);
frame.setLocationRelativeTo(NULL); 集preferredSize(大小);
frame.add(本); 登录名=新按钮(登录);
login.setBounds(0,0,的getWidth(),的getHeight()); frame.add(登录); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}私人无效的开始(){
createBufferStrategy(2);
线程=新主题(本);
thread.start();
isRunning = TRUE;
}私人无效完成()抛出InterruptedException的{
isRunning = FALSE;
的Thread.join();
}私人无效updateLogin(){
对于(int类型的= 0; A< pixels.length; A ++){
像素[A] = menu.pixels [A];
}
}私人无效renderLogin(){
BufferStrategy中缓冲= getBufferStrategy();
显卡GFX = buffer.getDrawGraphics(); 对于(int类型的= 0; A< splashPixels.length; A ++){
splashPixels [α] = splashscreen.splashPixels [一个];
} gfx.drawImage(图像,0,0,的getWidth(),的getHeight(),空);
gfx.drawImage(飞溅,320,37,625,340,NULL); gfx.setColor(Color.WHITE);
gfx.drawString(游戏有限公司©2013,3,(的getHeight() - 4));
gfx.drawString(\\游戏\\是布拉赫-夸夸其谈的商标。,(的getWidth() - 268),(的getHeight() - 3)); gfx.dispose();
buffer.show();
}公共无效的run(){
而(isRunning ==真){
updateLogin();
renderLogin();
} 尝试{
完();
}赶上(EXC InterruptedException的){
exc.printStackTrace();
}
}公共静态无效的主要(字串[] args){
登录登录=新的登录();
login.begin();
}
}
再一次,我唯一的问题是,我一直得到一个放大按钮。
在此先感谢,我知道你们都很忙,诸如此类的东西,我AP preciate抽空过目,并帮助回答我的问题。
P.S。有谁知道如何使密码字段与AWT?我还需要这一点。 ;)
解决方法:添加您的JButton(再次使用Swing组件)首先到JPanel(它默认使用的FlowLayout),然后添加到顶层窗口
块引用>您可以只改变的布局管理你的框架到的FlowLayout所以它会像一个JPanel。
frame.setLayout(新的FlowLayout());
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. ;)
解决方案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.
You could just change the layout manager for your frame to a FlowLayout so it will behave like a JPanel.
frame.setLayout(new FlowLayout());
这篇关于如何做没有让按钮调整我一个按钮添加到画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!