Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

swing - Java JFrame background color not working

I tried using:

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

But it is not working. Can anyone help me?

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);

    }


    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you did not post an SSCCE, I will do it for you. This shows how to change the background color of a JFrame. Starting from this, you can start adding components to the JFrame and see where you go wrong, instead of letting us look at a few hundred lines of code.

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 );
      }
    } );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...