2011-11-30 67 views
11
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的新手,所以我没有意识到这是一个错误。任何帮助将不胜感激。在Java Swing中显示图像

+0

您是否检查过以确保img!= null?在绘制它之前,我会调试或者使用打印语句进行检查,以确保图像安装正确。 –

+0

'新的JButton(“高分和\ nStatistics”);''JButton'不支持换行符。可以使用HTML来获得换行符,但是当禁用时该按钮看起来会错误。 –

回答

31

你正在做的事情为自己很难由具有非常混乱的程序结构,我建议您简化的东西很多

其中之一就是,您不需要使用当前的MinesweeperMenu类来扩展MinesweeperPanel,而用于后一类来扩展JFrame。然后你在其他地方有一个静态的JFrame - 这是太多的JFrames,并且启动时,你试图在一个JFrame中显示你的图像,但显示另一个没有图像。你的程序只需要一个JFrame,它可能应该被创建,填充内容,打包并显示在一个地方,而不是分散在这里和那里。

您试图在paintComponent重写中显示图片,但此方法永远不会被调用,因为您的类扩展了JFrame(最终),并且JFrame没有此方法。您正在使用正确的方法,但该类应该扩展JPanel,并且您应该在paintComponent方法块上方有一个@Override注释,以确保实际上覆盖父方法。

你应该摆脱全部静态在这个程序中的一切。这里唯一静态的东西应该是主要的方法,也许是一些常数,但就是这样。

这里还有更多的错误,我没有时间去浏览所有的错误。考虑从头开始,从小处着手,让小部分工作,然后将它们加在一起。例如,首先创建一个非常小的程序,试图将图像读入Image对象,将其放入ImageIcon中,将ImageIcon放入JLabel中,并将JLabel显示在JOptionPane中,这很简单,只是看看你是否可以读取图像中确定,例如,像这样:

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 

public class TestImages { 

    // *** your image path will be different ***** 
    private static final String IMG_PATH = "src/images/image01.jpg"; 

    public static void main(String[] args) { 
     try { 
     BufferedImage img = ImageIO.read(new File(IMG_PATH)); 
     ImageIcon icon = new ImageIcon(img); 
     JLabel label = new JLabel(icon); 
     JOptionPane.showMessageDialog(null, label); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 
} 

然后当你已经这样做了,看看你现在可以创建一个JPanel,显示同一图像中的paintComponent的方法里面,并在JOptionPane中显示这个JPanel。

然后创建一个JFrame并在JFrame中显示持有图像的JPanel。

通过连续迭代,您将测试概念,纠正错误并构建程序。

+0

由于有一个非常令人困惑的程序结构,你在为自己制造困难,我建议你简化很多事情。 ...看起来像生活中的经验教训:) :) –

6
File input = new File("./setup/images/MineMenuPicture.jpg"); 

如果MineMenuPicture.jpg是一个应用程序的资源,它应该是在一个罐子,从Class.getResource(String)获得的URL访问。

+0

不错的拿起! 1+ –