Java JFrame背景颜色不起作用

时间:2012-01-27 05:33:09

标签: java swing colors jframe setbackground

我尝试使用:

frame1.getContentPane().setBackground(Color.yellow);

但它不起作用。任何人都可以帮助我吗?

import java.awt.*;
import java.awt.Color;

public class PlayGame {

public static void main(String[] args) {
    GameFrame frame1 = new GameFrame();

    frame1.getContentPane().setBackground(Color.yellow);

    // Set Icon
    Image icon = Toolkit.getDefaultToolkit().getImage("image/poker_icon.gif");
    frame1.setIconImage(icon);
    frame1.setVisible(true);
    frame1.setSize(600, 700);
    frame1.setTitle("Card Game");

    // Set to exit on close
    frame1.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE);
    }
}

GameFrame

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

public class GameFrame extends JFrame implements ActionListener {

private JPanel topPnl, btmPnl, pcPnl, mainPnl;
private JPanel titlePnl, playerPnl, computerPnl;
private JLabel titleLbl, playerLbl, computerLbl;
private JLabel testTextBox1, testTextBox2;
private ImageIcon playerIcon, computerIcon;
//
private JPanel pickCardPnl, pickCardTitlePnl, cardPnl, resultPnl, optionPnl;
private JLabel pickCardTitleLbl;
private JLabel card1Lbl, card2Lbl, card3Lbl, card4Lbl, card5Lbl;
private JLabel resultLbl;
private JButton restartBtn, showCardBtn, exitBtn;
private JButton card1Btn, card2Btn, card3Btn, card4Btn, card5Btn;
private ImageIcon card1Pic, card2Pic, card3Pic, card4Pic, card5Pic;
private JButton playerBtn, computerBtn;
private ImageIcon playerPic;
private String[] card = new String[53];
private String name;

// ArrayInt al;
public GameFrame() {

    // a1 = new Array();
    //a1.generateRandom();



    setCard();

   setName();




    // Top Panel /////////////////////////////////////
    mainPnl = new JPanel(new BorderLayout());
    topPnl = new JPanel(new BorderLayout(50, 0));
    titlePnl = new JPanel();
    pcPnl = new JPanel(new GridLayout(1, 2, 10, 10));
    playerPnl = new JPanel(new BorderLayout(10, 10));
    computerPnl = new JPanel(new BorderLayout(10, 10));

    // Title Panel
    titleLbl = new JLabel("Card Game");
    titlePnl.add(titleLbl);


    // Player Panel

    playerIcon = new ImageIcon("image/player.png");
    playerLbl = new JLabel(name, playerIcon, JLabel.CENTER);
    playerPnl.add(playerLbl, BorderLayout.NORTH);


    playerPic = new ImageIcon("image/unknwon.png");
    playerBtn = new JButton(playerPic);
    playerPnl.add(playerBtn, BorderLayout.CENTER);


    playerBtn.setContentAreaFilled(false);
    playerBtn.setBorder(BorderFactory.createEmptyBorder());

    // Computer Panel
    computerIcon = new ImageIcon("image/computer.png");
    computerLbl = new JLabel("Computer:", computerIcon, JLabel.CENTER);
    computerPnl.add(computerLbl, BorderLayout.NORTH);

    playerPic = new ImageIcon("image/back.png");
    computerBtn = new JButton(playerPic);
    computerPnl.add(computerBtn, BorderLayout.CENTER);

    computerBtn.setContentAreaFilled(false);
    computerBtn.setBorder(BorderFactory.createEmptyBorder());




    pcPnl.add(playerPnl);
    pcPnl.add(computerPnl);

    // Add panel into Top Panel
    topPnl.add(titlePnl, BorderLayout.NORTH);
    topPnl.add(pcPnl, BorderLayout.CENTER);



    // Bottom Panel /////////////////////////////////////
    btmPnl = new JPanel(new BorderLayout());
    pickCardPnl = new JPanel(new BorderLayout());
    pickCardTitlePnl = new JPanel();
    cardPnl = new JPanel(new GridLayout(1, 5, 5, 5));
    resultPnl = new JPanel();
    optionPnl = new JPanel(new GridLayout(1, 3, 5, 5));

    // Pick Card Panel
    pickCardTitleLbl = new JLabel("Pick Your Card:");
    pickCardPnl.add(pickCardTitleLbl, BorderLayout.NORTH);




    card1Pic = new ImageIcon(card[1]);
    card1Btn = new JButton(card1Pic);
    cardPnl.add(card1Btn);
    card1Btn.addActionListener(this);

    card2Pic = new ImageIcon(card[2]);
    card2Btn = new JButton(card2Pic);
    cardPnl.add(card2Btn);
    card2Btn.addActionListener(this);

    card3Pic = new ImageIcon(card[3]);
    card3Btn = new JButton(card3Pic);
    cardPnl.add(card3Btn);
    card3Btn.addActionListener(this);

    card4Pic = new ImageIcon(card[4]);
    card4Btn = new JButton(card4Pic);
    cardPnl.add(card4Btn);
    card4Btn.addActionListener(this);

    card5Pic = new ImageIcon(card[5]);
    card5Btn = new JButton(card5Pic);
    cardPnl.add(card5Btn);
    card5Btn.addActionListener(this);

    // new ImageIcon(a1.getRandomNumber);

    pickCardPnl.add(cardPnl, BorderLayout.CENTER);

    card1Btn.setContentAreaFilled(false);
    card1Btn.setBorder(BorderFactory.createEmptyBorder());

    card2Btn.setContentAreaFilled(false);
    card2Btn.setBorder(BorderFactory.createEmptyBorder());

    card3Btn.setContentAreaFilled(false);
    card3Btn.setBorder(BorderFactory.createEmptyBorder());

    card4Btn.setContentAreaFilled(false);
    card4Btn.setBorder(BorderFactory.createEmptyBorder());

    card5Btn.setContentAreaFilled(false);
    card5Btn.setBorder(BorderFactory.createEmptyBorder());




    // Result Panel
    setCard();
    resultLbl = new JLabel("adasdadadasdasdasdasd");
    resultPnl.add(resultLbl);


    // Option Panel
    restartBtn = new JButton("Restart");
    optionPnl.add(restartBtn);
    restartBtn.addActionListener(this);

    showCardBtn = new JButton("Show Cards");
    optionPnl.add(showCardBtn);
    showCardBtn.addActionListener(this);

    exitBtn = new JButton("Exit");
    optionPnl.add(exitBtn);
    exitBtn.addActionListener(this);



    // Add panel into Bottom Panel
    btmPnl.add(pickCardPnl, BorderLayout.NORTH);
    btmPnl.add(resultPnl, BorderLayout.CENTER);
    btmPnl.add(optionPnl, BorderLayout.SOUTH);

    //
    mainPnl.add(topPnl, BorderLayout.NORTH);
    //  add(midPNL, BorderLayout.CENTER);
    mainPnl.add(btmPnl, BorderLayout.CENTER);

    add(mainPnl);


    // Menu bar
    JMenuBar menuBar = new JMenuBar();

    JMenu menu = new JMenu("Game");
    menuBar.add(menu);

    JMenuItem item3 = new JMenuItem("Change Name");
    item3.addActionListener(this);
    menu.add(item3);

    JMenuItem item = new JMenuItem("Change Card Deck");
    item.addActionListener(this);
    menu.add(item);

    JMenu subMenu = new JMenu("Change BackGround");
    subMenu.addActionListener(this);
    menu.add(subMenu);

    JMenuItem subItem = new JMenuItem("Blue");
    subItem.addActionListener(this);
    subMenu.add(subItem);

    JMenuItem subItem2 = new JMenuItem("Green");
    subItem2.addActionListener(this);
    subMenu.add(subItem2);

    //
    menu.addSeparator();
    //

    JMenuItem item4 = new JMenuItem("Quit");
    item4.addActionListener(this);
    menu.add(item4);

    setJMenuBar(menuBar);

} //End of GameFrame

public void setCard() {
    GenRandom g1 = new GenRandom();
    g1.GenRandomCard();

    int[] allCard = new int[11];

    allCard = g1.getAllCard();

    for (int i = 1; i <= 10; i++) {
        card[i] = "image/card/" + allCard[i] + ".png";

    }


}

public void setName() {

  //  name = JOptionPane.showInputDialog(null, "Please Enter Your Name", "Welcome", JOptionPane.QUESTION_MESSAGE) + ":";


}

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == card1Btn) {

        playerBtn.setIcon(card1Pic);
        card1Btn.setEnabled(false);

    }

    if (e.getSource() == card2Btn) {

        playerBtn.setIcon(card2Pic);
        card2Btn.setEnabled(false);

    }

    if (e.getSource() == card3Btn) {

        playerBtn.setIcon(card3Pic);
        card3Btn.setEnabled(false);

    }

    if (e.getSource() == card4Btn) {

        playerBtn.setIcon(card4Pic);
        card4Btn.setEnabled(false);

    }

    if (e.getSource() == card5Btn) {

        playerBtn.setIcon(card5Pic);
        card5Btn.setEnabled(false);

    }



    if (e.getSource() == restartBtn) {
        new AePlayWave("sound/jet.wav").start();
        JOptionPane.showMessageDialog(null, "Restart Button ");



    }

    if (e.getSource() == exitBtn) {


        /*      long start = System.currentTimeMillis();
        long end = start + 4 * 1000; // 60 seconds * 1000 ms/sec
        while (System.currentTimeMillis() < end) {
        // run
        new AePlayWave("sound/jet.wav").start();
        }*/

        System.exit(0);

    }


    }
}

4 个答案:

答案 0 :(得分:7)

由于您没有发布SSCCE,我会为您做的。这显示了如何更改JFrame的背景颜色。从这开始,您可以开始向JFrame添加组件并查看出错的位置,而不是让我们查看几百行代码。

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.EventQueue;

public class ColoredFrame {

  public static void main( String[] args ) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame( "TestFrame" );
        frame.getContentPane().setBackground( Color.PINK );
        //frame contains nothing, so set size
        frame.setSize( 200, 200 );
        frame.setVisible( true );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      }
    } );
  }
}

答案 1 :(得分:2)

您应该为JPanel提供背景颜色,然后在JFrame中使用此JPanel,而不是直接向您的JFrame提供背景。

答案 2 :(得分:2)

只需将setVisible(true);放在构造函数的末尾。

此外,您在JFrame上添加了mainPnl,因此更改JFrame的颜色将毫无用处,

所以不要写

add(mainPnl);
在你的GameFrame类中,你最好使用

setContentPane(mainPnl);

frame1.getContentPane().setBackground(Color.YELLOW);工作。

希望这可能会有所帮助

此致

答案 3 :(得分:1)

我知道这是一个非常老的问题,但是对于其他寻求正确答案的人,也可以这样写:

frame1.getContentPane().setBackground(new Color (255,255,102)); //or whatever color you want in the RGB range