2014-12-06 177 views
0

我試圖在頁面中間添加一個按鈕,在圖像上。我嘗試了幾種方法,最後我用了這個方法。我不確定爲什麼,但它肯定不會像按鈕(按下/釋放時的動畫)。 下面是一段代碼Java圖像按鈕

public class Window extends JFrame 
{ 
    public static final int WIDTH=400, HEIGHT=550; 
    private JPanel panel=new JPanel(); 

    public Window() 
    { 
     super("Tetris"); 
     setSize(WIDTH,HEIGHT); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     panel.setBackground(new Color(176,224,230)); 

     add(panel); 

    } 

    public void addLogo() 
    { 
     String address="C:/Users/Lorena/Desktop/Calculatoare Engleza/Anul II/OOP/Tetris v2//bin/Tetris/tetris.png"; 
     ImageIcon icon=new ImageIcon(address); 

     Image img = icon.getImage(); 
     Image newimg = img.getScaledInstance(320, 150, java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); 

     JLabel label = new JLabel(); 
     label.setIcon(icon); 
     panel.add(label); 
     this.getContentPane().add(panel); 
    } 

    public void addStartButton() 
    { 

     String address="C:/Users/Lorena/Desktop/Calculatoare Engleza/Anul II/OOP/Tetris v2//bin/Tetris/play.png"; 
     ImageIcon icon=new ImageIcon(address); 

     //Used to resize image 
     Image img = icon.getImage(); 
     Image newimg = img.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); 

     /*JLabel label = new JLabel(); 
     label.setIcon(icon); 
     panel.add(label); 
     this.getContentPane().add(panel);*/ 

     //this.add(panel2,BorderLayout.CENTER); 
     JButton start=new JButton(icon); 
     start.setBorder(BorderFactory.createEmptyBorder()); 
     start.setContentAreaFilled(false); 

     panel.add(start); 
     this.getContentPane().add(panel); 

    } 

    public void addBottomFirstPage() 
    { 
     String address="C:/Users/Lorena/Desktop/Calculatoare Engleza/Anul II/OOP/Tetris v2//bin/Tetris/bigbricks.png"; 
     ImageIcon icon=new ImageIcon(address); 

     Image img = icon.getImage(); 
     Image newimg = img.getScaledInstance(370, 150, java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); 

     JLabel label = new JLabel(); 
     label.setIcon(icon); 
     panel.add(label); 
     this.getContentPane().add(panel); 
    } 
} 

回答

0

如果您註釋掉

// start.setBorder(BorderFactory.createEmptyBorder()); 

它應該顯示爲普通的按鈕就行了。

否則,我不知道你期望它做什麼,因爲邊框通常是「點擊」動畫的責任。

+0

這就是事實,我不知道它到底會做什麼。謝謝 – 2014-12-06 22:32:22