我在向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,圖像假設在哪個目錄?
很好的問題 - 什麼是* *問題? ;-) – 2010-06-09 13:25:25
您是否嘗試過將圖像面板添加到空的'JFrame'?如果你這樣做會發生什麼? – jjnguy 2010-06-09 13:41:30