gpt4 book ai didi

java - 当我更改单独的代码时,JButton 有时会起作用,而其他则不起作用

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

当我运行我的程序时,Jbuttons 有时会出现,但有时却不会。例如,如果我更改与 JButton 无关的内容,它不会显示它们。它只会显示一个空的 jframe。 PS 抱歉,如果我的代码出现格式错误,我是这个网站的新手。任何有关提问的提示将不胜感激。而且它也不会让我显示代码的顶部。

package ca.seanmckee.digcraft;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Game extends JPanel {

public static String versionnumber = "0.0.1"; //For updating game version number
public static String gamename = "Digcraft ";
public static boolean dig = true;
public static int rocks = 0;
public static int sticks = 0;
public static int logs = 0;


public static void main(String[]a){

JFrame frame = new JFrame(gamename + versionnumber);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

JPanel panel = new JPanel();
frame.add(panel);
JButton dig = new JButton("Dig"); //Dig mechanic allows players to find things
JButton stickscounter = new JButton("Sticks: " + sticks);
JButton rockscounter = new JButton ("Rocks: " + rocks);
JButton logscounter = new JButton("logs" + logs);
JButton craft = new JButton("Craft"); //uses things found by digging to create more advanced things
panel.add(dig);
panel.add(stickscounter);
panel.add(rockscounter);
panel.add(logscounter);
panel.add(craft);

dig.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {

dig();
}
});

}

public static void dig(){
int counter = 0;
while(counter < 5){

Random random = new Random();
int number;
number = 1+random.nextInt(50); //Gets random number to select what you dug up
switch(number){
case 1:
System.out.println("You find a rock");
rocks = rocks + 1;
break;
case 2:
System.out.println("You find a log");
logs = logs + 1;
break;
case 3:
System.out.println("You find a stick");
sticks = sticks + 1;
break;
default:
System.out.println("You dig deeper...");
break;

}
counter = counter + 1;
}


}



}

最佳答案

首先,采用frame.setVisible并将其设为main方法的最后一条语句...

public static void main(String[]a){

JFrame frame = new JFrame(gamename + versionnumber);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Take this...
//frame.pack();
//frame.setSize(800,600);
//frame.setLocationRelativeTo(null);
//frame.setVisible(true);

JPanel panel = new JPanel();
frame.add(panel);
JButton dig = new JButton("Dig"); //Dig mechanic allows players to find things
JButton stickscounter = new JButton("Sticks: " + sticks);
JButton rockscounter = new JButton ("Rocks: " + rocks);
JButton logscounter = new JButton("logs" + logs);
JButton craft = new JButton("Craft"); //uses things found by digging to create more advanced things
panel.add(dig);
panel.add(stickscounter);
panel.add(rockscounter);
panel.add(logscounter);
panel.add(craft);

dig.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {

dig();
}
});

// Put it here...
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

您还会发现最后调用 packsetLocationRelativeTo 也会有所帮助,因为您现在框架中的内容将允许 pack > 做好它的工作

其次,将 UI 封装在 EventQueue.invokeLater block 中...

public static void main(String[]a){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame(gamename + versionnumber);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
frame.add(panel);
JButton dig = new JButton("Dig"); //Dig mechanic allows players to find things
JButton stickscounter = new JButton("Sticks: " + sticks);
JButton rockscounter = new JButton ("Rocks: " + rocks);
JButton logscounter = new JButton("logs" + logs);
JButton craft = new JButton("Craft"); //uses things found by digging to create more advanced things
panel.add(dig);
panel.add(stickscounter);
panel.add(rockscounter);
panel.add(logscounter);
panel.add(craft);

dig.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {

dig();
}
});

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

参见Initial Threads了解更多详情

第三,获取r

关于java - 当我更改单独的代码时,JButton 有时会起作用,而其他则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825437/

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