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);

我的图像图标不显示,我将它们放在同一个目录中?请帮忙

4

2 回答 2

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);
}

`

于 2011-12-21T13:51:04.133 回答
0

好的,谢谢任何想法如何在 switch 语句中的运算符之间使用以防万一,例如 case ((hours>=0)&&(hours<2)):amount = 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 :/行动

case 2TO4 :/行动

case 4TO8 :/行动

} ..

于 2011-12-21T15:07:39.180 回答