Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame背景色不工作_Java_Swing_Colors_Jframe_Setbackground - Fatal编程技术网

Java JFrame背景色不工作

Java JFrame背景色不工作,java,swing,colors,jframe,setbackground,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

我尝试使用:

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);
    }
}
游戏框架
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共类GameFrame扩展JFrame实现ActionListener{
私人JPanel topPnl、btmPnl、pcPnl、mainPnl;
私人JPanel titlePnl、playerPnl、computerPnl;
私人JLabel titlebl、playerLbl、computerLbl;
专用JLabel testTextBox1、testTextBox2;
私人图像图标playerIcon,computerIcon;
//
私人JPanel pickCardPnl、pickCardTitlePnl、cardPnl、resultPnl、optionPnl;
私人JLabel PickCardTitleBL;
私人JLabel卡1LBL、卡2LBL、卡3LBL、卡4LBL、卡5LBL;
私有JLabel resultbl;
私有JButton restartbn、showCardBtn、exitBtn;
私人JButton card1Btn、card2Btn、card3Btn、card4Btn、card5Btn;
私人图像图标卡1张、卡2张、卡3张、卡4张、卡5张;
私人JButton播放器,计算机BTN;
私人影像图标播放器;
私有字符串[]卡=新字符串[53];
私有字符串名称;
//ArrayInt等人;
公共游戏框架(){
//a1=新数组();
//a1.生成域();
setCard();
setName();
//顶板/////////////////////////////////////
mainPnl=newjpanel(newborderlayout());
topPnl=新JPanel(新边界布局(50,0));
titlePnl=新的JPanel();
pcPnl=新JPanel(新网格布局(1,2,10,10));
playerPnl=新JPanel(新边界布局(10,10));
computerPnl=新JPanel(新边界布局(10,10));
//标题栏
titleLbl=新的JLabel(“纸牌游戏”);
标题NL.添加(标题BL);
//播放器面板
playerIcon=newimageicon(“image/player.png”);
playerLbl=新的JLabel(名称、playerIcon、JLabel.CENTER);
playerPnl.add(playerBL,BorderLayout.NORTH);
playerPic=新图像图标(“image/unknwon.png”);
playerBtn=新的JButton(playerPic);
playerPnl.add(playerBtn,BorderLayout.CENTER);
playerBtn.setContentAreaFilled(假);
playerBtn.setBorder(BorderFactory.createEmptyByOrder());
//计算机面板
computerIcon=新图像图标(“image/computer.png”);
computerLbl=新的JLabel(“计算机:”,computerIcon,JLabel.CENTER);
computerPnl.add(computerLbl,BorderLayout.NORTH);
playerPic=newimageicon(“image/back.png”);
computerBtn=新的JButton(playerPic);
computerPnl.add(computerBtn,BorderLayout.CENTER);
计算机BTN.setContentAreaFilled(假);
computerBtn.setboorder(BorderFactory.createEmptyByOrder());
pcPnl.add(playerPnl);
pcPnl.add(计算机PNL);
//将面板添加到顶部面板中
topPnl.add(标题NL,BorderLayout.NORTH);
添加(pcPnl,BorderLayout.CENTER);
//底板/////////////////////////////////////
btmPnl=新的JPanel(新的BorderLayout());
pickCardPnl=new JPanel(new BorderLayout());
pickCardTitlePnl=新的JPanel();
cardPnl=新的JPanel(新的网格布局(1,5,5));
resultPnl=newjpanel();
optionPnl=新的JPanel(新的网格布局(1,3,5,5));
//拾取卡面板
PickCardTitleBL=新的JLabel(“选择您的卡:”);
添加(PickCardTitleBL,BorderLayout.NORTH);
card1Pic=新图像图标(卡[1]);
card1Btn=新的JButton(card1Pic);
cardPnl.add(card1Btn);
card1Btn.addActionListener(此);
card2Pic=新图像图标(卡[2]);
card2Btn=新的JButton(card2Pic);
cardPnl.add(card2Btn);
card2Btn.addActionListener(此);
card3Pic=新图像图标(卡[3]);
card3Btn=新的JButton(card3Pic);
cardPnl.add(card3Btn);
card3Btn.addActionListener(此);
card4Pic=新图像图标(卡[4]);
card4Btn=新的JButton(card4Pic);
cardPnl.add(card4Btn);
card4Btn.addActionListener(此);
card5Pic=新图像图标(卡[5]);
card5Btn=新的JButton(card5Pic);
cardPnl.add(card5Btn);
card5Btn.addActionListener(此);
//新图像图标(a1.getRandomNumber);
选择cardPnl.add(cardPnl,BorderLayout.CENTER);
card1Btn.setContentAreaFilled(假);
card1Btn.setboorder(BorderFactory.createEmptyBorder());
card2Btn.setContentAreaFilled(假);
card2Btn.setboorder(BorderFactory.createEmptyBorder());
card3Btn.setContentAreaFilled(假);
card3Btn.setboorder(BorderFactory.createEmptyByOrder());
card4Btn.setContentAreaFilled(假);
card4Btn.setboorder(BorderFactory.createEmptyByOrder());
card5Btn.setContentAreaFilled(假);
card5Btn.setBorder(BorderFactory.createEmptyByOrder());
//结果小组
setCard();
结果BL=新的JLabel(“ADASDADASDASD”);
resultPnl.add(resultbl);
//选项板
restartBtn=新的JButton(“重启”);
选项pnl.add(重启btn);
restartbn.addActionListener(此);
showCardBtn=新的JButton(“显示卡”);
选项pnl.add(showCardBtn);
showCardBtn.addActionListener(此);
exitBtn=新的JButton(“退出”);
选项pnl.add(exitBtn);
exitBtn.addActionListener(此);
//将面板添加到底部面板中
添加(pickCardPnl,BorderLayout.NORTH);
添加(resultPnl,BorderLayout.CENTER);
btmPnl.add(选项PNL,BorderLayout.SOUTH);
//
mainPnl.add(topPnl,BorderLayout.NORTH);
//添加(midPNL,BorderLayout.CENTER);
mainPnl.add(btmPnl,BorderLayout.CENTER);
添加(mainPnl);
//菜单栏
JMenuBar menuBar=新的JMenuBar();
JMenu菜单=新JMenu(“游戏”);
菜单栏。添加(菜单);
jMenuItem3=新的JMenuItem(“变更名称”);
项目3.addActionListener(本);
菜单。添加(第3项);
JMenuItem项目=新的JMenuItem(“更改卡片组”);
item.addActionListener(本);
菜单。添加(项目);
JMenu子菜单=新建JMenu(“更改背景”);
子菜单addActionListener(此);
菜单.添加(子菜单);
JMenuItem子项=新的JMenuItem(“蓝色”);
子项addActionListener(此);
子菜单.添加(子项);
JMenuItem子项2=新的JMenuItem(“绿色”);
子项2.addActionListener(此);
子菜单.添加(子项2);
//
menu.addSeparator();
//
jMenuItem4=新的JMenuItem(“退出”);
项目4.addActionListener(本);
菜单。添加
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);

    }


    }
}
add(mainPnl);
setContentPane(mainPnl);
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 );
      }
    } );
  }
}
frame1.getContentPane().setBackground(new Color (255,255,102)); //or whatever color you want in the RGB range