即使已添加getcontentpane,JFrame的颜色也无法正常工作

时间:2014-10-16 03:22:41

标签: java swing jframe jpanel

我对Java很新。这是我的课堂作业。我完成了所有的事情,除了JFrame颜色没有显示。我看了所有其他类似的问题。他们中的大多数人都说要使用getContentPane()。问题是我已添加,但它没有显示。以下是我的代码。我把它分成两部分。第二部分包含所有JFrame.getContentPane()。setBackground(color。**)代码。谢谢。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class NumberGame extends JFrame  {

private JPanel Content;
private JFrame  DisplayFrame;
private JTextField Input;
private JLabel DisplayText, Message;
private int Guess;
private JButton button;
private JButton NewGame;
private int Number;
private int Lowest = 0;
private int Highest = 0;






  public  void NumberGame  () {
     Content = new JPanel ();
    DisplayFrame = new JFrame ("Welcome");
    DisplayFrame.setSize(700, 400);
    DisplayFrame.setLayout (new BorderLayout());

     DisplayText = new JLabel ("I have a number between 1 and 1000. Can you guess my number?    
 Enter your first guess.");





     Input = new JTextField (20);

     Content.add(Input);
     Message = new JLabel ("");

      button = new JButton ("Submit");
      button.addActionListener (new GuessHandler()); 





      NewGame = new JButton ("New Game");   
      NewGame.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent ae) {
              Input.setText("");
              Message.setText("");
              repaint();
          }
        } );




    DisplayFrame.add(Content);
    Content.add(DisplayText);
    Content.add(Input);
    Content.add(button);
    Content.add(NewGame);
    Content.add(Message);


    theGame();
    DisplayFrame.setVisible(true);
    }

   public void theGame () {
       Number = (int) (Math.random() * 1000 +1);
   }








   public static void main(String[] args) {


    NumberGame a = new NumberGame ();
    a.NumberGame();



    }

以下是我遇到问题的其余代码。

class GuessHandler implements ActionListener {
   @Override
   public void actionPerformed (ActionEvent ae) {

       Guess = Integer.parseInt(Input.getText());

       if(Guess>Number) {
           Message.setText("Too high!");

           if (Guess < Lowest) {
               Lowest = Guess;
               DisplayFrame.getContentPane().setBackground(Color.red);

           }

           else
               DisplayFrame.getContentPane().setBackground(Color.blue);
       }

       else if (Guess < Number) {
           Message.setText("Too Low!");

           if (Guess > Highest) {
               Highest = Guess;
               DisplayFrame.getContentPane().setBackground(Color.red);
           }
           else
               DisplayFrame.getContentPane().setBackground(Color.blue);
       }

       else {
           Message.setText("Correct!");
           Input.setEditable(false);
           Lowest = 0;
           Highest = 1000;
       }
           repaint ();


       }


   }

}

1 个答案:

答案 0 :(得分:0)

ContentJPanel,因为JFrame使用BorderLayout,面板Content将占据整个可用空间,覆盖框架自己的内容窗格(不要混淆两者,不同)。

尝试更改

DisplayFrame.add(Content);

DisplayFrame.setContentPane(Content);

您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码