2014-02-17 150 views
1

的頂部,我完成了我的DiceRollGUI,當我運行它,然後按「滾動」彼此頂部的圖像棧,我怎麼做,因此我後再次按下按鈕消失?圖像疊加在彼此

我做什麼最MadProgrammer告訴我,但我碰到了這是錯誤的:

--------------------Configuration: <Default>-------------------- 
G:\DiceRollGUI\src\DiceRollGUI.java:26: error: constructor RollButton in class RollButton cannot be applied to given types; 
    button.addActionListener(new RollButton(diceRoll)); 
          ^
required: JPanel 
found: JLabel 
reason: actual argument JLabel cannot be converted to JPanel by method invocation conversion 
G:\DiceRollGUI\src\DiceRollGUI.java:42: error: cannot find symbol 
     contentPane.add(diceRoll); 
     ^
symbol: variable contentPane 
location: class RollButton 
2 errors 

Process completed. 

Dice Stack

代碼:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.util.Random; 

public class DiceRollGUI { 
    public static JLabel label; 
    public static JFrame frame; 
    public static JPanel panel; 
    private static JButton button; 
    private static JButton buttonRollDie; 
    private static JLabel diceRoll; 

public static void main (String[] args) { 
    JFrame frame = new JFrame("Dice Roll GUI"); 
    panel = new JPanel(); 
    JPanel contentPanel = new JPanel(new GridLayout(0,2,5,10)); 
    button = new JButton("Roll");; 

    frame.setVisible(true); 
    frame.setResizable(false); 
    frame.setSize(1000, 1000); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    button.setActionCommand("Roll"); 
    button.addActionListener(new RollButton(diceRoll)); 
    button.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.setPreferredSize(new Dimension(750, 500)); 
    frame.setContentPane(panel); 
    frame.pack(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
    panel.add(button); 
} 

    private static class RollButton implements ActionListener { 

    private JPanel diceRoll; 

    public RollButton(JPanel diceRoll){ 
     this.diceRoll = diceRoll; 
     contentPane.add(diceRoll); 
    }   
     public void actionPerformed(ActionEvent event){ 
      int roll = (int) (Math.round((Math.random() * 5) + 1)); 
      ImageIcon dice = null; 

       if(roll == 1){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png"); 
       } 
       else if(roll == 2){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png"); 
       } 
       else if(roll == 3){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png"); 
       } 
       else if(roll == 4){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg"); 
       } 
       else if(roll == 5){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png"); 
       } 
       else if(roll == 6){ 
        dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png"); 
       } 
       JLabel diceRoll = new JLabel("",dice, JLabel.CENTER); 
       panel.add(diceRoll); 
       panel.revalidate(); 
      } 
    } 
} 

回答

0

1-在構造函數中,創建diceRollJLabel的實例,並把它添加到您的panel

diceRoll = new JLabel(); 
contentPane.add(diceRoll); 

2-而是通過你的contentPaneRollButtonActionListener的參考,傳遞給diceRoll,而不是一個參考......

button.addActionListener(new RollButton(diceRoll)); 

3-更改您的RollButtonActionListener使用JLabel代替JPanel的支持......

private static class RollButton implements ActionListener { 

    private JLabel diceRoll; 

    public RollButton(JLabel diceRoll){ 
     this.diceRoll= diceRoll; 
    }  

4-在你actionPerformed方法,只需更改diceRoll的圖標屬性...

public void actionPerformed(ActionEvent event){ 
    int roll = (int) (Math.round((Math.random() * 5) + 1)); 
    ImageIcon dice = null; 
    //... 
    diceRoll.setIcon(dice); 
} 

您可能發現在contentPane上使用BorderLayoutGridBagLayout會產生很好的效果。您可能還需要調查horizontalAligmentverticalAligment性能

更新與編譯例如

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class DiceRollGUI { 

    public static JLabel label; 
    public static JFrame frame; 
    public static JPanel panel; 
    private static JButton button; 
    private static JButton buttonRollDie; 
    private static JLabel diceRoll; 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Dice Roll GUI"); 
     panel = new JPanel(); 
     JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10)); 
     button = new JButton("Roll"); 

     // !! Add this !! // 
     diceRoll = new JLabel(); 
     contentPanel.add(diceRoll); 

     frame.setVisible(true); 
     frame.setResizable(false); 
     frame.setSize(1000, 1000); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     button.setActionCommand("Roll"); 
     button.addActionListener(new RollButton(diceRoll)); 
     button.setAlignmentX(Component.CENTER_ALIGNMENT); 
     panel.setPreferredSize(new Dimension(750, 500)); 
     frame.setContentPane(panel); 
     frame.pack(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
     panel.add(button); 
    } 

    private static class RollButton implements ActionListener { 

     // !! Change This !! // 
     private JLabel diceRoll; 

     // !! Change This !! // 
     public RollButton(JLabel diceRoll) { 
      // !! Change This !! // 
      this.diceRoll = diceRoll; 
     } 

     public void actionPerformed(ActionEvent event) { 
      int roll = (int) (Math.round((Math.random() * 5) + 1)); 
      ImageIcon dice = null; 

      if (roll == 1) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_1.png"); 
      } else if (roll == 2) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_2.png"); 
      } else if (roll == 3) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_3.png"); 
      } else if (roll == 4) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_4.jpeg"); 
      } else if (roll == 5) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_5.png"); 
      } else if (roll == 6) { 
       dice = new ImageIcon("DiceRollGUI Pictures/die_face_6.png"); 
      } 
      // !! Change This !! // 
      diceRoll.setIcon(dice); 
     } 
    } 
} 

5-你從來沒有真正加入到contentPane什麼...更改main看起來更像..

public static void main(String[] args) { 
    JFrame frame = new JFrame("Dice Roll GUI"); 
    panel = new JPanel(); 
    JPanel contentPanel = new JPanel(new GridLayout(0, 2, 5, 10)); 
    button = new JButton("Roll"); 

    // !! Add this !! // 
    diceRoll = new JLabel(); 
    contentPanel.add(diceRoll); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    button.setActionCommand("Roll"); 
    button.addActionListener(new RollButton(diceRoll)); 
    button.setAlignmentX(Component.CENTER_ALIGNMENT); 
    panel.setPreferredSize(new Dimension(750, 500)); 
    panel.setLayout(new BorderLayout()); 
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); 
    panel.add(button, BorderLayout.NORTH); 
    panel.add(contentPanel); 
    frame.setContentPane(panel); 
    frame.pack(); 
    frame.setVisible(true); 
} 
+0

我做了最你說什麼,但我得到的錯誤(更新後) – user3163392

+0

不,你沒有。我說要改變'RollButton'接受'JLabel'並且您還沒有實例化diceRoll'的'的實例,如在第一點建議... – MadProgrammer

+0

好吧,我改變了它,所以我RollButton的ActionListener支持的JLabel代替的JPanel但是對於diceRoll.setIcon(dice);改變它到什麼屬性? :\ – user3163392

0

按照你的代碼,看來你添加每次actionPerformed都會調用一個新的JLabel。 一旦JLabel將被添加到它停留在JPanel中,你必須之前刪除以前的一個:

panel.removeAll() 
+0

是的,但如果我把它放在,它也刪除我的按鈕 – user3163392

+0

然後使用remove(int index)方法,你的按鈕sho ULD是在索引0,那麼你的第一個JLabel應該是在索引1 – Kwoinkwoin