将 JButton 与 Canvas 一起使用
Using JButtons with Canvas
我目前正尝试在 JFrame
window 中为我正在开发的测试游戏创建一个原始 HUD。在这样做的过程中,我所有的尝试都被挫败了。要么包含代表 HUD 的所有按钮的面板不显示,要么我尝试绘制的 Canvas
似乎不存在,因为没有显示我绘制的图形。
我相信问题是,这些层不是'overlapping',因此一层遮挡了另一层。但是,我尝试使用 Flowlayout()
(仅用于测试目的),但仍然没有解决我的问题。所以现在,我对尝试拥有 buttons/Labels.
的 HUD 感到茫然
出于视觉目的,我正在尝试做这样的事情:
但我实际上得到了这个(只是一个测试绘制的图块,没有 HUD):
这是涉及的代码:
public static void createDrawFrame(int width, int height) {
f = new JFrame();
c = new Canvas();
c.setPreferredSize(new Dimension(width, height));
c.setFocusable(false);
f.add(c);
f.pack();
f.setSize(width, height);
f.setLocationRelativeTo(null);
和
public static JFrame getDrawFrame() {
f.setVisible(true);
return f;
}
和
public static JFrame createHUD(JFrame f, Game game) {
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.setSize(100, 100);
p.setLocation(300, 0);
Button b0 = new Button("Settings");
Button b1 = new Button("Exit");
MenuListeners mListeners = new MenuListeners(game);
b0.addActionListener(mListeners);
b1.addActionListener(mListeners);
p.add(b0);
p.add(b1);
f.add(p);
return f;
}
这里叫:
private void init() {
Display.createDrawFrame(width, height);
Display.createMenuFrame(width, height);
this.f = Display.getDrawFrame();
this.c = Display.getCanvas();
this.f = HUDDisplay.createHUD(this.f, this);
MapAssets.init();
}
并在此处呈现:
private void render() {
b = c.getBufferStrategy();
if(b == null) {
c.createBufferStrategy(3);
return;
}
do {
do {
g = b.getDrawGraphics();
g.clearRect(0, 0, width, height);
state.render(g);
b.show();
}while(b.contentsLost());
g.dispose();
}while(b.contentsRestored());
}
希望我正确说明了这个问题。回顾一下,我只想知道为什么我的按钮没有按应有的方式显示在 JFrame 中。感谢您的帮助。
我认为您可以使用 JFrame
的添加方法和 BorderLayout
来定位 canvas 和按钮。我更改了您的代码以显示(演示)它可以完成。我希望这就是你要找的。
我将组件(Canvas
和 Panel
带按钮)添加到 JFrame
使用这样的代码使它们可见:
frame.add(panel, BorderLayout.NORTH); // panel with buttons
frame.add(canvas, BorderLayout.CENTER);
这里有 Oracle Java Swing 教程的链接,以了解更多详细信息;请参阅 Swing Components and Laying Out Components Within a Container.
部分
示例代码:
import java.awt.*;
import javax.swing.*;
public class FrameLayoutTest {
private static JFrame f;
private static Canvas c;
public static void main(String [] args) {
init();
}
private static void init() {
//Display.createDrawFrame(width, height);
createDrawFrame(400, 400);
//Display.createMenuFrame(width, height);
createHUD();
//this.f = Display.getDrawFrame();
//getDrawFrame();
//this.c = Display.getCanvas();
//this.f = createHUD(this.f, this);
//MapAssets.init();
}
public static void createDrawFrame(int width, int height) {
f = new JFrame();
c = new Canvas();
c.setBackground(Color.blue);
c.setPreferredSize(new Dimension(width, height));
c.setFocusable(false);
f.add(c, BorderLayout.CENTER); //f.add(c);
f.pack();
f.setVisible(true); // getDrawFrame()
f.setSize(width, height);
f.setLocationRelativeTo(null);
}
public static void createHUD() {
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.setSize(100, 100);
p.setLocation(300, 0);
Button b0 = new Button("Settings");
Button b1 = new Button("Exit");
//MenuListeners mListeners = new MenuListeners(game);
//b0.addActionListener(mListeners);
//b1.addActionListener(mListeners);
p.add(b0);
p.add(b1);
f.add(p, BorderLayout.NORTH); //f.add(p);
}
}
我目前正尝试在 JFrame
window 中为我正在开发的测试游戏创建一个原始 HUD。在这样做的过程中,我所有的尝试都被挫败了。要么包含代表 HUD 的所有按钮的面板不显示,要么我尝试绘制的 Canvas
似乎不存在,因为没有显示我绘制的图形。
我相信问题是,这些层不是'overlapping',因此一层遮挡了另一层。但是,我尝试使用 Flowlayout()
(仅用于测试目的),但仍然没有解决我的问题。所以现在,我对尝试拥有 buttons/Labels.
出于视觉目的,我正在尝试做这样的事情:
但我实际上得到了这个(只是一个测试绘制的图块,没有 HUD):
public static void createDrawFrame(int width, int height) {
f = new JFrame();
c = new Canvas();
c.setPreferredSize(new Dimension(width, height));
c.setFocusable(false);
f.add(c);
f.pack();
f.setSize(width, height);
f.setLocationRelativeTo(null);
和
public static JFrame getDrawFrame() {
f.setVisible(true);
return f;
}
和
public static JFrame createHUD(JFrame f, Game game) {
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.setSize(100, 100);
p.setLocation(300, 0);
Button b0 = new Button("Settings");
Button b1 = new Button("Exit");
MenuListeners mListeners = new MenuListeners(game);
b0.addActionListener(mListeners);
b1.addActionListener(mListeners);
p.add(b0);
p.add(b1);
f.add(p);
return f;
}
这里叫:
private void init() {
Display.createDrawFrame(width, height);
Display.createMenuFrame(width, height);
this.f = Display.getDrawFrame();
this.c = Display.getCanvas();
this.f = HUDDisplay.createHUD(this.f, this);
MapAssets.init();
}
并在此处呈现:
private void render() {
b = c.getBufferStrategy();
if(b == null) {
c.createBufferStrategy(3);
return;
}
do {
do {
g = b.getDrawGraphics();
g.clearRect(0, 0, width, height);
state.render(g);
b.show();
}while(b.contentsLost());
g.dispose();
}while(b.contentsRestored());
}
希望我正确说明了这个问题。回顾一下,我只想知道为什么我的按钮没有按应有的方式显示在 JFrame 中。感谢您的帮助。
我认为您可以使用 JFrame
的添加方法和 BorderLayout
来定位 canvas 和按钮。我更改了您的代码以显示(演示)它可以完成。我希望这就是你要找的。
我将组件(Canvas
和 Panel
带按钮)添加到 JFrame
使用这样的代码使它们可见:
frame.add(panel, BorderLayout.NORTH); // panel with buttons
frame.add(canvas, BorderLayout.CENTER);
这里有 Oracle Java Swing 教程的链接,以了解更多详细信息;请参阅 Swing Components and Laying Out Components Within a Container.
部分示例代码:
import java.awt.*;
import javax.swing.*;
public class FrameLayoutTest {
private static JFrame f;
private static Canvas c;
public static void main(String [] args) {
init();
}
private static void init() {
//Display.createDrawFrame(width, height);
createDrawFrame(400, 400);
//Display.createMenuFrame(width, height);
createHUD();
//this.f = Display.getDrawFrame();
//getDrawFrame();
//this.c = Display.getCanvas();
//this.f = createHUD(this.f, this);
//MapAssets.init();
}
public static void createDrawFrame(int width, int height) {
f = new JFrame();
c = new Canvas();
c.setBackground(Color.blue);
c.setPreferredSize(new Dimension(width, height));
c.setFocusable(false);
f.add(c, BorderLayout.CENTER); //f.add(c);
f.pack();
f.setVisible(true); // getDrawFrame()
f.setSize(width, height);
f.setLocationRelativeTo(null);
}
public static void createHUD() {
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.setSize(100, 100);
p.setLocation(300, 0);
Button b0 = new Button("Settings");
Button b1 = new Button("Exit");
//MenuListeners mListeners = new MenuListeners(game);
//b0.addActionListener(mListeners);
//b1.addActionListener(mListeners);
p.add(b0);
p.add(b1);
f.add(p, BorderLayout.NORTH); //f.add(p);
}
}