开发者

Java - my paint program needs a menu bar with a submenu with an image - how to do?

开发者 https://www.devze.com 2023-04-09 02:23 出处:网络
SO I have this program that is kind of like paint, it lets you draw shapes and lines, but my assignment is to add a menu bar with a sub menu Fil开发者_开发问答e-> About, when Click on the about sectio

SO I have this program that is kind of like paint, it lets you draw shapes and lines, but my assignment is to add a menu bar with a sub menu Fil开发者_开发问答e-> About, when Click on the about section. It should pull up a panel or a frame or a something that has a image which I have already made.

However it's not working and I have never actually dealt with images before. All I have is from a Google search and that failed. I know the method is being called from the delightful system.out.println, and a box opens up but there is no picture!~ any help would be very nice.

if (e.getActionCommand().equals("About"))
{
    System.out.println("stfu");
    JFrame about = new JFrame("About");
    about.setSize(300, 300);
    BufferedImage img = null;
    try{
    img = ImageIO.read(new File("C:/Users/TehRobot/Desktop/Logo.png"));
    }catch (IOException e1)
    {

    }


You'll want to ...

  • Read in the Image with ImageIO.read(...)
  • Put your Image into an ImageIcon via the ImageIcon constructor
  • Put the ImageIcon into a JLabel via JLabel's setIcon(...) method.
  • And display that JLabel in a dialog such as a JOptionPane (super easy to do) or a modal JDialog (a little more complicated, but not much).

In all this should just take a few lines of code, that's all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号