源码

在Java Swing中显示图像


public class MinesweeperMenu extends MinesweeperPanel{

private JPanel picture = new JPanel();
private JButton play = new JButton("Play");
private JButton highScores = new JButton("High Score and /nStatistics");
private JButton changeMap = new JButton("Create Custom /nor Change Map");
private JButton difficulty = new JButton("Custom or/nChange Difficulty");
private JButton user = new JButton("Change User");
Image img;

public MinesweeperMenu()
{
    // Set Layout for the menu
    LayoutManager menuLayout = new BoxLayout(menu, BoxLayout.Y_AXIS);
    menu.setLayout(menuLayout);

    // Set Layout for the window
    LayoutManager windowLayout = new BorderLayout();
    window.setLayout(windowLayout);

    // Add buttons to the panels
    menu.add(play);
    menu.add(highScores);
    menu.add(changeMap);
    menu.add(difficulty);
    menu.add(user);

    // Add picture to the frame
    try{
    File input = new File("./setup/images/MineMenuPicture.jpg");
    img = ImageIO.read(input);
    }
    catch(IOException ie)
    {
        System.out.println(ie.getMessage());
    }

    // Add action listeners
    changeMap.addActionListener(new ChangeMapListener());   

}


public void paintComponent(Graphics g)
{
    // POSITION OF THE PICTURE
    int x = 650;
    int y = 585;
    g.drawImage(img, x, y, null);
}

public void displayFrame()
{
    // Display Frame
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

public static void main(String[] args)
{
    MinesweeperMenu menu = new MinesweeperMenu();
    window.pack();
    menu.displayFrame();
    window.repaint();
}
}


public class MinesweeperPanel extends JFrame{

public static final Color COLOR_KEY = new Color(220, 110, 0);

// Initialize all the objects
public static JFrame window = new JFrame("Minesweeper++");
public static JPanel menu = new JPanel();

// Close the current window
public static void close()
{
    window.setVisible(false);
    window.dispose();
}



}

我无法让我的图像显示在框架中.我已经尝试了所有的东西,但是我得到的印象是,由于我是Java Swing的新手,所以我没有意识到这是一个错误.任何帮助将不胜感激.

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/zaijava-swingzhongxianshituxiang.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 24, 2019 at 06:42 上午

热评文章