簡體   English   中英

軟件工程,圖形用戶界面

[英]software engineering, GUI

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Guiii extends JFrame{

  private JButton menu;
  private JButton custom;

  public Guiii(){
    super ("The Title");
    setLayout(new FlowLayout());

    menu = new JButton("menu Button");
    add (menu);

    Icon b = new ImageIcon (getClass().getResource("button.png"));
    Icon x = new ImageIcon (getClass().getResource("greenbutton.png"));
    custom = new JButton("Custom",b);
    custom.setRolloverIcon(x);
    add (custom);


    HandlerClass handler = new HandlerClass();
    menu.addActionListener(handler);
    custom.addActionListener(handler);

  }

  private class HandlerClass implements ActionListener{
    public void actionPerformed(ActionEvent event){

      JOptionPane.showMessageDialog(null, String.format("~s",     event.getActionCommand()));
    }   
  }
}

這是我的代碼我遇到了一些問題並且無法正常工作,我已經為它做了一個主要但顯示錯誤有人可以幫助我並解釋代碼將如何工作khg

這些是錯誤

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at Guiii.<init>(Guiii.java:22)
    at main.main(main.java:7)

這是代碼的其余部分,但它必須在另一個文件中:

import javax.swing.JFrame;
public class main {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    Guiii o = new Guiii();
    o.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    o.setSize(300,200);
    o.setVisible(true);
  }
}

圖標的構造函數拋出 NPE,因為getResource返回null

將圖像放在與 Guiiii 類相同的文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM