Java命令执行失败
嘿,如果有人能找出为什么我的代码不起作用,那就太好了。每次我尝试以文件或项目的形式运行时,都会失败。我真的不知道该怎么做来尝试和修复这个问题,我是新的,所以如果这是一些基本的,我忽略了对不起。我正在运行netbeans 11.2,以防出现问题Java命令执行失败,java,Java,嘿,如果有人能找出为什么我的代码不起作用,那就太好了。每次我尝试以文件或项目的形式运行时,都会失败。我真的不知道该怎么做来尝试和修复这个问题,我是新的,所以如果这是一些基本的,我忽略了对不起。我正在运行netbeans 11.2,以防出现问题 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gameclock;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.ImageIcon;
import javax.accessibility.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author jacobk
*/
public class GameClock001 extends JFrame {
private final JLayeredPane layeredpane;
private JLabel background;
private JLabel monthdot;
private JLabel daydot;
private JLabel hourdot;
private JLabel mindot;
private JLabel ampmdot;
private JLabel monthbuttons;
private JLabel daybuttons;
private JLabel hourbuttons;
private JLabel minbuttons;
private JLabel ampmam;
private JLabel ampmpm;
private JButton monthup;
private JButton monthdown;
private JButton dayup;
private JButton daydown;
private JButton hourup;
private JButton hourdown;
private JButton minup;
private JButton mindown;
private JButton ampm;
public GameClock001() {
super("Game Clock");
setSize(900, 603);
setLookAndFeel();
FlowLayout flo = new FlowLayout();
setLayout(flo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon Background = new ImageIcon("GameClockSmol.jpeg", "GameClockIMG");
ImageIcon up = new ImageIcon("Up.jpeg", "Button Up");
ImageIcon down = new ImageIcon("Down.jpeg", "Button Down");
ImageIcon dot = new ImageIcon("dot_png29.png", "Dot");
ImageIcon swap = new ImageIcon("switch.png", "Switch");
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(new Dimension(900, 563));
int monthx = (0);
int monthy = (0);
int dayx = (0);
int dayy = (0);
int hourx = (0);
int houry = (0);
int minx = (0);
int miny = (0);
background.setIcon(Background);
background.setBounds(0, 0, Background.getIconWidth(), Background.getIconHeight());
monthdot.setIcon(dot);
monthdot.setBounds(monthx, monthy, dot.getIconWidth(), dot.getIconHeight());
daydot.setIcon(dot);
daydot.setBounds(dayx, dayy, dot.getIconWidth(), dot.getIconHeight());
hourdot.setIcon(dot);
hourdot.setBounds(hourx, houry, dot.getIconWidth(), dot.getIconHeight());
mindot.setIcon(dot);
mindot.setBounds(minx, miny, dot.getIconWidth(), dot.getIconHeight());
ampmdot.setIcon(dot);
monthbuttons.setText("Month");
daybuttons.setText("Day");
hourbuttons.setText("Hour");
minbuttons.setText("Minute");
ampmam.setText("AM");
ampmpm.setText("PM");
monthup.setIcon(up);
monthdown.setIcon(down);
dayup.setIcon(up);
daydown.setIcon(down);
hourup.setIcon(up);
hourdown.setIcon(down);
minup.setIcon(up);
mindown.setIcon(down);
ampm.setIcon(swap);
layeredpane.add(background, new Integer(3));
layeredpane.add(monthdot, new Integer(1));
layeredpane.add(daydot, new Integer(1));
layeredpane.add(hourdot, new Integer(1));
layeredpane.add(mindot, new Integer(1));
layeredpane.add(ampmdot, new Integer(1));
add(monthbuttons);
add(daybuttons);
add(hourbuttons);
add(minbuttons);
add(ampmam);
add(ampmpm);
add(monthup);
add(monthdown);
add(dayup);
add(daydown);
add(hourup);
add(hourdown);
add(minup);
add(mindown);
add(ampm);
add(layeredpane);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
}
public static void main(String[] arguments) {
GameClock001 frame = new GameClock001();
}
}
我立即注意到的两件事是,您正在
main
线程上执行所有操作(该线程将立即终止),并且您从未将帧设置为可见。将main()
方法更改为
public static void main(String[] arguments) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GameClock001 frame = new GameClock001();
frame.setVisible(true);
}
});
}
那么,它怎么会失败呢?我注意到你从来没有设置你的框架可见。你在
main
线程上执行所有操作。例如,monthup
从未创建过,因此monthup.setIcon
很可能失败。代码中也有更多这种故障类型的示例。您需要执行类似于monthup=newjbutton(“单击”)代码>首先。如果你能解释你更改了什么以及为什么更改,你的答案会更好,而不是仅仅转储大约一百行代码,并期望读者直观地识别你更改了什么。嘿,我试过了,它构建正确,但没有保持打开状态或向我显示窗口,所以现在我更困惑了。
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLayeredPane layeredpane;
private JLabel background = new JLabel();
private JLabel monthdot = new JLabel();
private JLabel daydot = new JLabel();
private JLabel hourdot = new JLabel();
private JLabel mindot = new JLabel();
private JLabel ampmdot = new JLabel();
private JLabel monthbuttons = new JLabel();
private JLabel daybuttons = new JLabel();
private JLabel hourbuttons = new JLabel();
private JLabel minbuttons = new JLabel();
private JLabel ampmam = new JLabel();
private JLabel ampmpm = new JLabel();
private JButton monthup = new JButton();
private JButton monthdown = new JButton();
private JButton dayup = new JButton();
private JButton daydown = new JButton();
private JButton hourup = new JButton();
private JButton hourdown = new JButton();
private JButton minup = new JButton();
private JButton mindown = new JButton();
private JButton ampm = new JButton();
public GameClock001() {
super("Game Clock");
setSize(900, 603);
setLookAndFeel();
FlowLayout flo = new FlowLayout();
setLayout(flo);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon Background = new ImageIcon();
ImageIcon up = new ImageIcon("download.png", "Button Up");
ImageIcon down = new ImageIcon("download.png", "Button Down");
ImageIcon dot = new ImageIcon("download.png", "Dot");
ImageIcon swap = new ImageIcon("download.png", "Switch");
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(new Dimension(900, 563));
int monthx = (0);
int monthy = (0);
int dayx = (0);
int dayy = (0);
int hourx = (0);
int houry = (0);
int minx = (0);
int miny = (0);
background.setIcon(Background);
background.setBounds(0, 0, Background.getIconWidth(), Background.getIconHeight());
monthdot.setIcon(dot);
monthdot.setBounds(monthx, monthy, dot.getIconWidth(), dot.getIconHeight());
daydot.setIcon(dot);
daydot.setBounds(dayx, dayy, dot.getIconWidth(), dot.getIconHeight());
hourdot.setIcon(dot);
hourdot.setBounds(hourx, houry, dot.getIconWidth(), dot.getIconHeight());
mindot.setIcon(dot);
mindot.setBounds(minx, miny, dot.getIconWidth(), dot.getIconHeight());
ampmdot.setIcon(dot);
monthbuttons.setText("Month");
daybuttons.setText("Day");
hourbuttons.setText("Hour");
minbuttons.setText("Minute");
ampmam.setText("AM");
ampmpm.setText("PM");
monthup.setIcon(up);
monthdown.setIcon(down);
dayup.setIcon(up);
daydown.setIcon(down);
hourup.setIcon(up);
hourdown.setIcon(down);
minup.setIcon(up);
mindown.setIcon(down);
ampm.setIcon(swap);
layeredpane.add(background, new Integer(3));
layeredpane.add(monthdot, new Integer(1));
layeredpane.add(daydot, new Integer(1));
layeredpane.add(hourdot, new Integer(1));
layeredpane.add(mindot, new Integer(1));
layeredpane.add(ampmdot, new Integer(1));
add(monthbuttons);
add(daybuttons);
add(hourbuttons);
add(minbuttons);
add(ampmam);
add(ampmpm);
add(monthup);
add(monthdown);
add(dayup);
add(daydown);
add(hourup);
add(hourdown);
add(minup);
add(mindown);
add(ampm);
add(layeredpane);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
exc.printStackTrace();
}
}
public static void main(String[] arguments) {
GameClock001 frame = new GameClock001();
frame.setVisible(true);
}