Java 当我更改单独的代码时,JButtons有时会工作,而不是其他

Java 当我更改单独的代码时,JButtons有时会工作,而不是其他,java,swing,Java,Swing,当我运行程序时,Jbuttons有时会出现,但有时不会出现。例如,如果我更改了与JButtons无关的内容,它将不会显示它们。它只会显示一个空的jframe。PS抱歉,如果我的代码出现格式错误,我是这个网站的新手。任何关于提问的提示都将不胜感激。而且它也不会让我显示代码的顶部 package ca.seanmckee.digcraft; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import

当我运行程序时,Jbuttons有时会出现,但有时不会出现。例如,如果我更改了与JButtons无关的内容,它将不会显示它们。它只会显示一个空的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;
    }


}



}
package ca.seanmckee.digcraft;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类游戏扩展JPanel{
公共静态字符串versionnumber=“0.0.1”;//用于更新游戏版本号
公共静态字符串gamename=“Digcraft”;
公共静态布尔值dig=true;
公共静态int=0;
公共静态int=0;
公共静态int日志=0;
公共静态void main(字符串[]a){
JFrame=新JFrame(游戏名+版本号);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
框架。设置尺寸(800600);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
JPanel面板=新的JPanel();
框架。添加(面板);
JButton dig=新的JButton(“dig”);//挖掘机制允许玩家找到东西
JButton stickscounter=新JButton(“Sticks:+Sticks”);
JButton rocksconter=新JButton(“岩石:+岩石”);
JButton logsconter=新JButton(“日志”+日志);
JButton craft=newjbutton(“craft”);//使用挖掘发现的东西来创建更高级的东西
面板。添加(挖掘);
面板。添加(粘滞计数器);
面板。添加(RockSconter);
面板。添加(日志中心);
小组.增补(工艺);
dig.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
挖掘();
}
});
}
公共静态void dig(){
int计数器=0;
while(计数器<5){
随机=新随机();
整数;
number=1+random.nextInt(50);//获取随机数以选择所挖掘的内容
开关(编号){
案例1:
System.out.println(“你找到一块石头”);
岩石=岩石+1;
打破
案例2:
System.out.println(“找到日志”);
日志=日志+1;
打破
案例3:
System.out.println(“你找到一根棍子”);
棍棒=棍棒+1;
打破
违约:
System.out.println(“你挖得更深…”);
打破
}
计数器=计数器+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);
}
您还将发现调用
pack
setLocationRelativeTo
last也会有所帮助,因为现在框架中的内容将允许
pack
完成它的工作

其次,将UI包装在
EventQueue.invokeLater
块中

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);
        }
    });
}
有关更多详细信息,请参阅

第三,在main(String[])方法的结尾处使用frame.setVisible(true)。此外,在向main(String[])方法添加所有组件后,在main(String[])方法的结尾处使用frame.pack()。主方法应该是这样的:-

public static void main(String[]a){

JFrame frame = new JFrame(gamename + versionnumber);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(800,600);
frame.setLocationRelativeTo(null);


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.add(panel);
 frame.pack();
 frame.setVisible(true);

}

另外,
frame.pack()
将是最后一条语句,但在
setVisible()
之前,将
frame.setSize()
替换为
frame.setpreferedsize()
@Meraman对
pack
是,对
preferredSize
否-见我完全不同意在JFrame中不使用
setPreferredSize()
。因为无论jframe以何种方式布置组件,但如果您需要jframe自定义尺寸,那么这是唯一更好的方式。您还可以使用各种布局的空顺序和插入或间隙更好地控制大小;)此外,我个人更喜欢使用布局属性和边框来调整帧(和调用包)的大小,因为它们会自动考虑平台之间的渲染差异,比任何时候的幻数都要好