将图像添加到JFrame的问题

我在将图片添加到JFrame时遇到了问题,缺少某些东西或者写错了。 这是课程:

主要课程:

public class Tester { public static void main(String args[]) { BorderLayoutFrame borderLayoutFrame = new BorderLayoutFrame(); borderLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); borderLayoutFrame.setSize(600,600); borderLayoutFrame.setVisible(true); } } public class BorderLayoutFrame extends JFrame implements ActionListener { private JButton buttons[]; // array of buttons to hide portions private final String names[] = { "North", "South", "East", "West", "Center" }; private BorderLayout layout; // borderlayout object private PicPanel picture = new PicPanel(); // set up GUI and event handling public BorderLayoutFrame() { super( "Philosofic Problem" ); layout = new BorderLayout( 5, 5 ); // 5 pixel gaps setLayout( layout ); // set frame layout buttons = new JButton[ names.length ]; // set size of array // create JButtons and register listeners for them for ( int count = 0; count < names.length; count++ ) { buttons[ count ] = new JButton( names[ count ] ); buttons[ count ].addActionListener( this ); } add( buttons[ 0 ], BorderLayout.NORTH ); // add button to north add( buttons[ 1 ], BorderLayout.SOUTH ); // add button to south add( buttons[ 2 ], BorderLayout.EAST ); // add button to east add( buttons[ 3 ], BorderLayout.WEST ); // add button to west add( picture, BorderLayout.CENTER ); // add button to center } // handle button events public void actionPerformed( ActionEvent event ) { } } 

我试图将图像添加到布局的中心。

这是图像类:

 public class PicPanel extends JPanel { Image img; private int width = 0; private int height = 0; public PicPanel() { super(); img = Toolkit.getDefaultToolkit().getImage("table.jpg"); } public void paintComponent(Graphics g) { super.paintComponents(g); if ((width <= 0) || (height <= 0)) { width = img.getWidth(this); height = img.getHeight(this); } g.drawImage(img,0,0,width,height,this); } } 

请你帮忙,有什么问题? 谢谢

顺便说一句:我正在使用eclipse,图像所在的目录是什么?

您发布的代码存在以下几个问题:

  • 你应该在BorderLayoutFrame类中使用getContentPane().add()而不是简单的add()
  • 您应该使用SwingUtilities.invokeLater()从测试器类启动JFrame。 像这样的东西:

  SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.setProperty("DEBUG_UI", "true"); BorderLayoutFrame blf = new BorderLayoutFrame(); blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); blf.setSize(600,600); blf.setVisible(true); } }); 
  • 不要使用Toolkit加载图像! 在下面的代码中,如果“Table.jpg”与PicPanel位于同一个包中,则图像将正确加载。

 public PicPanel() { super(); try { rUrl = getClass().getResource("Table.jpg"); if (rUrl != null) { img = ImageIO.read(rUrl); } } catch (IOException ex) { Logger.getLogger(PicPanel.class.getName()).log(Level.SEVERE, null, ex); } } 
  • PicPanel.PaintComponent()你调用super.paintComponents()是’s’是一个typeo?
  • PicPanel.PaintComponent() ,您不需要所有宽度/高度的东西,只需这样做:

    g.drawImage(img, 0, 0, getWidth(), getHeight(), this);

并且避免一起调用super.paintComponent,因为你正在绘制一个图像,为什么你想让面板完全绘制?

我最后的实现你的东西:

 public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { System.setProperty("DEBUG_UI", "true"); BorderLayoutFrame blf = new BorderLayoutFrame(); blf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); blf.setSize(600,600); blf.setVisible(true); } }); } } class BorderLayoutFrame extends JFrame implements ActionListener { private final BorderLayout layout; private final JButton[] buttons; private final String names[] = {"North", "South", "East", "West", "Center"}; public BorderLayoutFrame() { super( "Philosofic Problem" ); layout = new BorderLayout( 5, 5 ); getContentPane().setLayout( layout ); buttons = new JButton[ names.length ]; for (int i=0; i 

在Eclipse中,如果您使用路径"table.jpg"那么您需要将该图像放在项目的顶层。

有关Eclipse项目结构的更多信息,请参阅此Stack Overflow问题。

您可能想尝试使用ImageIcon显示图像,而不是在面板上绘制它。

以下代码将创建一个只能添加到JPanelImageIcon

 ImageIcon pic = new ImageIcon(getClass().getResource("myimage.jpeg"); 

这使用一种奇特的方式来加载pic,以便在放入Jar文件时它可以工作。