GridLayout的透明背景

时间:2015-09-08 20:20:35

标签: java swing

我使用GridLayou编写了一个代码,但是我无法删除灰色背景并使用img i插入作为背景,图片显示了问题:

有人可以帮忙吗?

这是代码

public class mainClass {


private static  JButton start;
static BackgroundPanel bp = null;
static JFrame mainf = null;
static int R;
final static boolean shouldFill = true;

// group turns boolean
boolean redTurn = false;
boolean blueTurn = false;
boolean greenTurn = false;
boolean yellowTurn = false;

// boards
static ImageIcon gameBoard;
static ImageIcon blueBoard;
static ImageIcon qBoard;

 // dice
static JButton dice_1 = null;


public static void main(String[] args) throws IOException {
     mainf = new JFrame ("سين جيم");


    // background
    BufferedImage mFrame = ImageIO.read(new File("B1.png"));
    bp = new BackgroundPanel(mFrame);
    mainf.add(bp);
    bp.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
  //  c.fill = GridBagConstraints.HORIZONTAL;


    // Hi string 
    JLabel hi = new JLabel ("أهلا وسهلا بكم في لعبة الليدو");
    Font fs = hi.getFont();
    hi.setFont(fs.deriveFont(50f));


    bp.add(hi);

    // empty

    // button 
    start = new JButton ( "لنبدأاللعب");
    c.fill = GridBagConstraints.HORIZONTAL;


    c.ipady = 20;      //make this component tall
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    c.insets = new Insets(10,0,0,0);  //top padding
    bp.add(start, c);



    // Action Listener
    start.addActionListener(new ActionListener() { 
          public void actionPerformed(ActionEvent e) { 

              // starting the game area 

              // emptying all
              bp.removeAll();
              bp.revalidate();
              bp.repaint();
              BufferedImage mFrame2= null;
            try {
                // changing background
                mFrame2 = ImageIO.read(new File("B2.png"));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // setting new background
              bp = new BackgroundPanel(mFrame2);
              bp.setLayout(new GridBagLayout());

              GridBagConstraints c = new GridBagConstraints();
              // adding gameBoard 
              gameBoard = new ImageIcon("gameBoard.png");
              JLabel gameBoard_1 = new JLabel(gameBoard);
              bp.add(gameBoard_1);

            // adding blueBoard 
              blueBoard = new ImageIcon("blueBoard.png");
              JLabel blueBoard_1 = new JLabel(blueBoard);
              bp.add(blueBoard_1);


              GridLayout experimentLayout = new GridLayout(4,4); // rows | , cols-                
              blueBoard_1.setLayout(experimentLayout);

              // gameName panel
              JPanel gameName = new JPanel();
              gameName.setLayout(new GridLayout(1,0));

              JLabel Ledu= new JLabel ("لعبة اللدو");
              Font font3 = new Font("Verdana", Font.BOLD, 30);
              Ledu.setFont(font3);
              Ledu.setForeground(Color.WHITE);

              gameName.add(Ledu);

              // teamPanel
              JPanel teamName = new JPanel();
              teamName.setLayout(new GridLayout(2,0));  

              JLabel blue= new JLabel ("الفريق الأرزق");
              Font font2 = new Font("Verdana", Font.BOLD, 20);
              blue.setFont(font2);
              blue.setForeground(Color.BLUE);
              JLabel red= new JLabel ("الفريق الأحمر");
              red.setFont(font2);
              red.setForeground(Color.RED);      
              JLabel yellow= new JLabel ("الفريق الأصفر");
              yellow.setFont(font2);
              yellow.setForeground(Color.YELLOW);     
              JLabel green= new JLabel ("الفريق الأخضر");
              green.setFont(font2);
              green.setForeground(Color.GREEN);
              // team panel
              teamName.add(blue);
              teamName.add(red);
              teamName.add(yellow);
              teamName.add(green);

              // adding question
              JPanel question = new JPanel();
              question.setLayout(new GridLayout(3,2));
              ImageIcon q = new ImageIcon("Q.png");
              JLabel q_1 = new JLabel(q);
              // adding Question panel
              question.add(q_1);

              // dicePanel

              final JPanel dicePanel = new JPanel();
              dicePanel.setLayout(new GridLayout(4,0));  
              ImageIcon dice = new ImageIcon("dice.png");
              dice_1 = new JButton(dice);

              // adding dice panel
              dicePanel.add(dice_1);
              dice_1.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                      dicePanel.remove(dice_1);
                      dicePanel.revalidate();
                      dicePanel.repaint();
                      ImageIcon dice = new ImageIcon("dice.gif");
                    dice_1 = new JButton(dice);
                  dicePanel.add(dice_1);

                 // random number 
                 Random r = new Random();
                 R = r.nextInt(10) + 2;
                 System.out.println(R);

                  }
              });




              // Centeralizing Ledu
              Ledu.setHorizontalAlignment(SwingConstants.CENTER);
              gameName.setAlignmentX(Component.CENTER_ALIGNMENT);
              blue.setHorizontalAlignment(SwingConstants.CENTER);
              red.setHorizontalAlignment(SwingConstants.CENTER);
              yellow.setHorizontalAlignment(SwingConstants.CENTER);
              green.setHorizontalAlignment(SwingConstants.CENTER);
              teamName.setAlignmentX(Component.CENTER_ALIGNMENT);


              // no backgrounf color
              Ledu.setOpaque( false );
              gameName.setOpaque( false );
              blue.setOpaque( false );
              red.setOpaque( false );
              yellow.setOpaque( false );
              green.setOpaque( false );
              teamName.setOpaque( false );
              question.setOpaque( false );
              dicePanel.setOpaque( false );

              // final add 
              blueBoard_1.add(gameName, BorderLayout.PAGE_START);
              blueBoard_1.add(teamName, BorderLayout.CENTER);
              blueBoard_1.add(question, BorderLayout.LINE_END);
              blueBoard_1.add(dicePanel, BorderLayout.PAGE_END);

              /*
              // add dice
               ImageIcon dice = new ImageIcon("dice.png");
               dice_1 = new JButton(dice);
               dice_1.setSize(100, 70);
               bp.add(dice_1);

               // changin to moving dice
              dice_1.addActionListener(new ActionListener() { 
                  public void actionPerformed(ActionEvent e) { 
                      bp.remove(dice_1);
                      bp.revalidate();
                      bp.repaint();
                      ImageIcon dice = new ImageIcon("dice.gif");
                    dice_1 = new JButton(dice);
                    dice_1.setSize(100, 70);
                   bp.add(dice_1);

                 // random number 
                 Random r = new Random();
                 int R = r.nextInt(10) + 2;
                 System.out.println(R);

                  }
              });

              */


                mainf.setContentPane(bp);




          };}); // end of start actionListener

                mainf.pack();
                mainf.setVisible(true);

};

    // dice class

}

1 个答案:

答案 0 :(得分:2)

默认情况下,JPanel是不透明的,因此您会看到灰色背景。

您需要使用:

panel.setOpaque( false );

如果您将面板添加到包含背景图像的组件中。

顺便说一下,你不应该使用静态变量。查看Swing教程中的示例,以获得更好的方法来创建框架。那就是创建一个代表你游戏的类。这将是一个JPanel。然后在此课程中定义游戏所需的所有变量。然后,您可以简单地将面板添加到框架中。您不应该在main()方法中创建组件。