2011-12-21 73 views
0
coins = new JPanel(); 
    coins.setLayout(new GridLayout(0,1)); 
    ImageIcon tenP1 = new ImageIcon("10p.jpeg"); 
    tenP = new JButton("", tenP1); 
    ImageIcon twentyP1 = new ImageIcon("20p.jpeg"); 
    twentyP = new JButton("", twentyP1); 
    ImageIcon fiftyP1 = new ImageIcon("50p.jpeg"); 
    fiftyP = new JButton("", fiftyP1); 
    ImageIcon pound = new ImageIcon("pound.jpeg"); 
    onePound = new JButton("", pound); 

我的图片图标不显示,我有他们在同一目录中同名吗?请帮助java imageicon和框架

回答

0

试试这个:

public static void main(String[] args) {

JFrame frame = new JFrame("Test image"); 
    JPanel pane = new JPanel(); 

    ImageIcon img = new ImageIcon(TestImage.class.getResource("/img.png")); 
    ImageIcon img2 = new ImageIcon(TestImage.class.getResource("/img2.png")); 
    ImageIcon img3 = new ImageIcon(TestImage.class.getResource("/img3.png")); 
    ImageIcon img4 = new ImageIcon(TestImage.class.getResource("/img4.png")); 
    ImageIcon img5 = new ImageIcon(TestImage.class.getResource("/img5.png"));` 

    JButton testBut = new JButton(img); 
    JButton testBut2 = new JButton(img2); 
    JButton testBut3 = new JButton(img3); 
    JButton testBut4 = new JButton(img4); 
    JButton testBut5 = new JButton(img5); 

    pane.setLayout(new BorderLayout()); 
    pane.add(testBut,BorderLayout.NORTH); 
    pane.add(testBut2,BorderLayout.WEST); 
    pane.add(testBut3,BorderLayout.EAST); 
    pane.add(testBut4,BorderLayout.SOUTH); 
    pane.add(testBut5,BorderLayout.CENTER); 

    frame.add(pane); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

`

+0

香港专业教育学院将他们添加到面板和容器,但只显示noral按钮 – user1106130 2011-12-21 14:04:31

+1

我想你应该thisto例如TRU您的ImageIcon:[代码] ImageIcon img = new ImageIcon(this.getClass()。getRessource(“/ img.png”)); – 2011-12-21 14:21:39

+0

好的感谢任何想法如何使用之间的运算符switch语句中的情况下,例如case((小时> = 0)&&(小时<2)):amount = 0.50将工作 – user1106130 2011-12-21 14:44:57

0

好,谢谢任何想法如何运营商之间的switch语句中的情况下使用,因此,例如情况下((小时> = 0)& &(小时< 2)):数量= 0.50可以工作 长达2小时50p; 2至4小时£1; 4至8小时2英镑; 8至12小时3英镑; 12至24小时5英镑; - 您可以创建一个枚举与范围:

public enum Hours {

U2,2TO4,4TO8,8TO12,12TO24;

}`

Hours value = HOURS.value //只是一个例子,你有一个getter

得到它swicth(value){ case: U2 :/action

case 2TO4 : /动作

case 4TO8 : /动作

} ..

+0

小时作为arguement – user1106130 2011-12-21 15:12:57

+0

过去了,你可以添加一个函数,它接受你的论点,并返回你的时间价值: – 2011-12-21 15:37:42