[Java] resize image + imagebutton
- // resize image + make imagebutton
- JPanel panel = new JPanel();
- panel.setLayout(null); // set 了layout是null後必需調用setBounds
- panel.setBounds(0, 0, 200 , 200); // 參數1: x座標; 2:y座標; 3:width; 4:height
-
- ImageIcon img = new ImageIcon(getClass().getResource("/lam/images/1.jpg"));
-
- // resize img width and height to 100,100
- Image image = img.getImage();
- Image newimg = image.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
- img = new ImageIcon(newimg);
-
- // add image to button
-
- JButton btn = new JButton();
- btn.setBorder(LineBorder.createGrayLineBorder());
- btn.setIcon(img);
- btn.setBounds(0, 0, 150, 120);
- panel.add(btn);
沒有留言:
張貼留言