2009penguin 2023-02-10 19:03 采纳率: 54.5%
浏览 62
已结题

关于#BUG#的问题,如何解决?工程文件内的图标无法显示)

BUG : 工程文件内的图标无法显示
源代码 :

package SRC;
//RandomNumber.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class RandomNumber extends JFrame{
    private static final long serialVersionUID = 1L;
    static JFrame F = new JFrame("RandomNumber");;
    static JLabel MAX;
    static JLabel MIN;
    static JLabel NUMBER;
    static JLabel GAP;
    static JLabel result;
    static JTextField max;
    static JTextField min;
    static JTextField number;
    static JTextField gap;
    static JButton Count;
    static JTextArea Result;
    static Panel Left,Right,MainPanel;
    static int TmpOfNumber = 1;
    static int TmpOfGap = 10;
    static int TmpOfMax = 100;
    static int TmpOfMin = 10;
    static SystemTray systemTray = SystemTray.getSystemTray();
    static ImageIcon icon = new ImageIcon(RandomNumber.class.getClass().getClassLoader().getResource("RandomNumber/src/Image/Icon.png"));
    static Image image = icon.getImage();
    static JPopupMenu pop = new JPopupMenu(); 
    static TrayIcon tray = new TrayIcon(image,"RadomNumber");
 
    public static void main(String args[]) throws IOException{
        int x = 500;
        int y = 350;
        //INIT
        MAX =new JLabel("最大值:");
        max = new JTextField("100",6);
        MIN =new JLabel("最小值:");
        min = new JTextField("10",6);
        NUMBER = new JLabel("个数:");
        number = new JTextField("1",6);
        GAP = new JLabel("最小间隔:");
        gap = new JTextField("10",6);
        Count = new JButton("抽号");
        result = new JLabel("结果:");
        Result = new JTextArea(15,15);
        Left = new Panel();
        Right = new Panel();
        Left.setLayout(new GridLayout(6,2,10,10));
        Right.setLayout(new FlowLayout(FlowLayout.RIGHT));
        F.setLayout(new FlowLayout(FlowLayout.LEFT));
        Result.setLineWrap(true);
        //AddListener
        Count.addActionListener((e) -> {
            boolean GapDo = false;
            boolean NumberDo = false;
            boolean MaxDo = false;
            boolean MinDo = false;
            try {
                if(max.getText() != null && min.getText() != null && gap.getText() != null && number.getText() != null) {
                    //Number
                    if(Integer.parseInt(number.getText()) > 70 || Integer.parseInt(number.getText()) < 1) {
                        JOptionPane.showMessageDialog(null,"The number of random number extraction cannot be greater than 70 times or be less than 1 time","Prompt",JOptionPane.WARNING_MESSAGE);
                        number.setText(String.valueOf(TmpOfNumber));
                        NumberDo = false;
                    }else {
                        TmpOfNumber = Integer.parseInt(number.getText());
                        NumberDo = true;                       
                    }
                    //Minimum interval
                    if(Integer.parseInt(gap.getText()) < 0) {
                        JOptionPane.showMessageDialog(null,"The minimum interval must not be less than 0","Prompt",JOptionPane.WARNING_MESSAGE);
                        number.setText(String.valueOf(TmpOfGap));
                        GapDo = false;
                    }else {
                        TmpOfGap = Integer.parseInt(gap.getText());
                        GapDo = true;                       
                    }
                    //Maximum
                    if(Integer.parseInt(max.getText()) < 0) {
                        JOptionPane.showMessageDialog(null,"The maximum value must not be less than 0","Prompt",JOptionPane.WARNING_MESSAGE);
                        max.setText(String.valueOf(TmpOfMax));
                        MaxDo = false;
                    }else {
                        TmpOfMax = Integer.parseInt(max.getText());
                        MaxDo = true;                       
                    }
                    if(Integer.parseInt(max.getText()) < Integer.parseInt(min.getText())) {
                        JOptionPane.showMessageDialog(null,"The maximum value shall not be less than the minimum value","Prompt",JOptionPane.WARNING_MESSAGE);
                        max.setText(String.valueOf(TmpOfMax));
                        min.setText(String.valueOf(TmpOfMin));
                        MaxDo = false;
                    }else {
                        TmpOfMax = Integer.parseInt(max.getText());
                        MaxDo = true;                       
                    }
                    //Minimum
                    if(Integer.parseInt(min.getText()) < 0) {
                        JOptionPane.showMessageDialog(null,"The minimum value must not be less than 0","Prompt",JOptionPane.WARNING_MESSAGE);
                        min.setText(String.valueOf(TmpOfMin));
                        MinDo = false;
                    }else {
                        TmpOfMin = Integer.parseInt(min.getText());
                        MinDo = true;                       
                    }
                    //Invoke
                    if(NumberDo && GapDo && MaxDo && MinDo) {
                        Result.setText(RandomNumber.RandomInteger(Integer.parseInt(max.getText()),Integer.parseInt(min.getText()),Integer.parseInt(number.getText()),Integer.parseInt(gap.getText())));
                    }
                }    
            }catch(Exception ae) {
                JOptionPane.showMessageDialog(null,ae.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            }
        });
        F.addWindowListener(new WindowAdapter() {
            static boolean TRAY = false;
            static int IsClick = 0;
            public void windowClosing(WindowEvent e) {
                JRadioButton YES = new JRadioButton("Exit program");
                JRadioButton NO = new JRadioButton( "Minimize to System Tray");
                JButton Click = new JButton("OK");
                JOptionPane Pane = new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
                JDialog Dialog = Pane.createDialog("Exit prompt");                
                Dialog.add(Pane);
                Dialog.pack();
                Object[] Options = {YES,NO,Click};                               
                Pane.setMessage(Options);
                Pane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
                YES.addActionListener((a) -> {
                    if(!TRAY) {
                        NO.setSelected(false);
                    }
                    IsClick ++;
                    TRAY = false;                                               
                   });
                NO.addActionListener((a) -> {
                    if(TRAY) {
                        YES.setSelected(false);
                    }
                    IsClick ++;
                    TRAY = true;                   
                });
                Click.addActionListener((a) -> {
                    if(IsClick > 0) {
                        if(!TRAY) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e1) {
                                JOptionPane.showMessageDialog(null,e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                            }
                               System.exit(0);
                        }else {
                            F.setVisible(false);
                            Dialog.setVisible(false);
                            try {
                                miniTray();
                            } catch (AWTException e1) {
                                JOptionPane.showMessageDialog(null,e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                            }
                        }                                                       
                    }else {
                        JOptionPane.showMessageDialog(null,"Please choose","Error",JOptionPane.WARNING_MESSAGE);
                    }                    
                });
                Dialog.dispatchEvent(new WindowEvent(Dialog, WindowEvent.WINDOW_CLOSING));
                Dialog.setLocationRelativeTo(F);
                Dialog.setVisible(true);                             
            }
            public void windowIconified(WindowEvent e) {
                F.setVisible(false);
                try {
                    miniTray();
                } catch (AWTException e1) {
                    JOptionPane.showMessageDialog(null,e1.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }
            }
            public void miniTray() throws AWTException {
                JMenuItem Amuse = new JMenuItem("DON'T CLICK");
                JMenuItem show = new JMenuItem("RESTORE");
                JMenuItem exit = new JMenuItem("EXIT"); 
                pop.add(Amuse);
                pop.add(show);
                pop.addSeparator();
                pop.add(exit);
                systemTray.add(tray);
                Amuse.addActionListener((e) -> {
                    JOptionPane.showMessageDialog(null,"RandomNumber's author will not accept any bugs caused by clicking 'DON'T CLICK'.\r\n"
                            + "This is the last warning. Do you want to continue?","Warning",JOptionPane.WARNING_MESSAGE);
                });
                show.addActionListener((e) -> {
                    systemTray.remove(tray);
                    F.setVisible(true);
                    F.setExtendedState(JFrame.NORMAL);
                    F.toFront();                    
                });
                exit.addActionListener((e) -> { 
                    systemTray.remove(tray);
                    System.exit(0);
                });                
                tray.setImageAutoSize(true);
                tray.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e) {
                            ShowPopupMenu(e);                      
                    }
                    public void mousePressed(MouseEvent e) {
                            ShowPopupMenu(e);
                    }
                    public void mouseClicked(MouseEvent e) {
                        if (e.getClickCount() == 2) {
                            systemTray.remove(tray);
                            F.setVisible(true);
                            F.setExtendedState(JFrame.NORMAL);
                            F.toFront();
                        }
                    }
                    public void ShowPopupMenu(MouseEvent e) {
                        if(e.isPopupTrigger()) {
                            pop.setLocation(e.getX() - 70,e.getY() - 70);
                            pop.setInvoker(pop);
                            pop.setVisible(true);
                        }
                    }
                });
            }
        }); 
        //ADD-FIRST(LEFT)
        Left.add(MAX);
        Left.add(max);
        Left.add(MIN);
        Left.add(min);
        Left.add(NUMBER);
        Left.add(number);
        Left.add(GAP);
        Left.add(gap);
        Left.add(Count);
        //ADD-FIRST(RIGHT)
        Result.setEditable(false);
        Right.add(result);
        Right.add(Result);
        //ADD-SECOND
        MainPanel = new Panel();
        MainPanel.add(Left);
        MainPanel.add(Right);
        F.add(MainPanel);
        //END
        F.setResizable(false);
        F.setSize(x, y);
        F.setIconImage(image);
        //SetLocation
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenw = screenSize.width;
        int screenh = screenSize.height;
        F.setLocation((screenw-x)/2, (screenh-y)/2);
        //SetVisible
        F.setVisible(true);
    }     
    public static String RandomInteger(int MaximumValue,int MinimumValue,int Range,int MinimumInterval) {
        int TMP = 0;
        int COUNT = 0;
        String end = "";
        TreeSet<Integer> t = new TreeSet<>();
        Random r = new Random();
        for(; Range > 0 ; Range-- ) {
            COUNT = r.nextInt(MaximumValue - MinimumValue + 1) + MinimumValue;
            if(Math.abs(COUNT - TMP) <= MinimumInterval) {
                while(Math.abs(COUNT - TMP) <= MinimumInterval) {
                    COUNT = r.nextInt(MaximumValue - MinimumValue + 1) + MinimumValue;
                }
            }
            t.add(COUNT);
            TMP = COUNT;
        }
        Iterator<Integer> I = t.iterator();
        while(I.hasNext()) {
            Integer a = I.next();
            String A = null;
            int Time = 15 - String.valueOf(a).length();
            A = String.valueOf(a);
            for(; Time > 0 ; Time --) {
                A += " ";
            }
            end += A;
        }
        return end;
    }
}


package SRC;
//DialogPrompt.java

public class DialogPrompt {

}

结构:

img

img

报错信息:

img

  • 写回答

1条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-02-10 19:11
    关注

    RandomNumber 29行报空指针,检查一下

    // 这样报了空指针,getClassLoader 获取为空了
    static ImageIcon icon = new ImageIcon(RandomNumber.class.getClass().getClassLoader().getResource("RandomNumber/src/Image/Icon.png"));
    
    // 去掉getClass() 再试试
    static ImageIcon icon = new ImageIcon(RandomNumber.class.getClassLoader().getResource("RandomNumber/src/Image/Icon.png"));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 2月18日
  • 已采纳回答 2月10日
  • 创建了问题 2月10日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效