6

我尝试使用:

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.*;
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

4 回答 4

9

既然你没有发布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 );
      }
    } );
  }
}
于 2012-01-27T07:25:03.617 回答
5

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

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

所以而不是写

add(mainPnl);

在你的 GameFrame 类中,你最好使用

setContentPane(mainPnl);

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

希望这可能会有所帮助

问候

于 2012-01-27T05:56:48.240 回答
3

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

于 2012-01-27T05:37:59.533 回答
1

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

frame1.getContentPane().setBackground(new Color (255,255,102)); //or whatever color you want in the RGB range
于 2018-08-13T14:59:27.787 回答