尝试使用java更改背景颜色时出错

时间:2016-01-18 21:19:56

标签: java swing colors awt background-color

我在Java中尝试使用此代码开始创建游戏。我有一个问题,背景颜色默认保留,即使我已经将setBackgroundColor用于代码。有人告诉我如何解决这个问题?这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;


public class Background extends JFrame{ 

    private JButton b;
     public Background () {

         super("the title");
            setLayout(new FlowLayout());

         b=new JButton("ROLL THE DICES");

         b.setForeground(Color.WHITE);//ndryshon ngjyren e shkrimit
         b.setBackground(Color.YELLOW);
         b.setBounds(20, 30, 20, 70);
         add(b);

         thehandler hand=new thehandler();
         b.addActionListener(hand);
          }
     private class thehandler implements ActionListener{
         public void actionPerformed(ActionEvent event) {
                JOptionPane.showMessageDialog(null, "Ju shtypet butonin");

     }

     }


     public static void main(String[] args) {



            Background f=new Background();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setBackground(Color.GREEN);
            f.setSize(400,300);
            f.setVisible(true);
     }}

1 个答案:

答案 0 :(得分:3)

更改

f.setBackground(Color.GREEN); 

f.getContentPane().setBackground(Color.GREEN);

您需要更改内容窗格背景。

您可以在此处详细了解: JFrame.setBackground() not working -- why?

相关问题