gpt4 book ai didi

java - 如何将 JPanel 添加到 JFrame

转载 作者:行者123 更新时间:2023-12-01 17:01:39 25 4
gpt4 key购买 nike

我是java初学者,我有这个目标要完成:enter image description here

这是我的部分代码:

公共(public)类 Trial 扩展了 JFrame {

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int SCALE = 3;

private BufferedImage backBuffer;

public KeyboardInput input;
private Stage stage;
public static Trial Trial;
public static String message = "";
private Object Object;
private Message MessageBox;

public void init() {
this.Trial = this;
input = new KeyboardInput();
this.addWindowListener(new WinListener());
this.setSize(WIDTH, HEIGHT);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setUndecorated(false);
this.setVisible(true);
this.addKeyListener(input);
client = new TrialClient(SERVER_IP, this);
MessageBox = new Message(username);
backBuffer = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);
}

public Stage getStage() {
return stage;
}

public class WinListener extends WindowAdapter {

@Override
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}

}


private Font font = new Font("Munro Small", Font.PLAIN, 96);
private Font font2 = new Font("Munro Small", Font.PLAIN, 50);
private Font fontError = new Font("Munro Small", Font.PLAIN, 25);
private int op = 0;


public void updateMenu() {
if (input.up.isPressed()) {
input.up.toggle(false);
} else if (input.down.isPressed()) {
input.down.toggle(false);
} else if (input.enter.isPressed() && op == 0) {
} else if (input.enter.isPressed() && op == 1) {
System.exit(0);
}
}


public void drawMenu() {
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setFont(font);
bbg.setColor(Color.white);

bbg.drawString("Sample", 189, 180);
bbg.setFont(font2);
if (op == 0) {
bbg.setColor(Color.red);
bbg.drawString("Start", 327, 378);
}

g.drawImage(backBuffer, 0, 0, this);
}

public void draw() {
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.black);
bbg.fillRect(0, 0, WIDTH, HEIGHT);
stage.drawStage(bbg, this);
for (Object t : stage.getPlayers()) {
t.draw(bbg, SCALE, this);
}
g.drawImage(backBuffer, 0, 0, this);
}


public void drawLogin() {
Graphics g = getGraphics();
Graphics bbg = backBuffer.getGraphics();
bbg.setColor(Color.black);
bbg.fillRect(0, 0, 800, 600);
bbg.setColor(Color.red);
bbg.setFont(fontError);
bbg.drawString(message, 100, 100);
bbg.setFont(font2);
bbg.setColor(Color.white);
bbg.drawString("Username", 284, 254);
bbg.setColor(Color.red);
bbg.drawString(username, 284, 304);
g.drawImage(backBuffer, 0, 0, this);
}



public class StringWait extends Thread {

public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
}

public void start() {
init();
while (true) {
...
}
}

public static void main(String[] args) {
Trial g = new Trial();
g.start();
}

}

如何在 JFrame 中声明 Jpanel?或者有一种方法可以在创建 JFrame 后在不同的窗口中创建 Jpanel ?我正在 netbeans 中执行此操作。

最佳答案

我认为问题在于该行

backBuffer = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB);

请修改它,以免它占据整个区域。

关于java - 如何将 JPanel 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344232/

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