简体   繁体   English

Java JFrame背景颜色不起作用

[英]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 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);

    }


    }
}

Since you did not post an SSCCE , I will do it for you. 既然你没有发布SSCCE ,我会为你做的。 This shows how to change the background color of a JFrame. 这显示了如何更改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. 从这开始,您可以开始向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 );
      }
    } );
  }
}

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

Just put your setVisible(true); 只需把你的setVisible(true); at the end of your constructor. 在构造函数的末尾。

Moreover you had added mainPnl on your JFrame, so changing colour of the JFrame will be useless, 此外,您已在JFrame上添加了mainPnl ,因此更改JFrame的颜色将毫无用处,

so instead of writing 所以不要写作

add(mainPnl);

in your GameFrame class, you better be using 在你的GameFrame类中,你最好使用

setContentPane(mainPnl);

for frame1.getContentPane().setBackground(Color.YELLOW); for frame1.getContentPane().setBackground(Color.YELLOW); to work. 上班。

Hope this might help 希望这可能有所帮助

Regards 问候

我知道这是一个非常古老的问题,但对于寻找正确答案的其他人来说,这也可以写成如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM