【问题标题】:Error when trying to change the background color using java [duplicate]尝试使用java更改背景颜色时出错[重复]
【发布时间】:2016-04-24 04:31:06
【问题描述】:

我正在尝试用 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);
     }}

【问题讨论】:

  • @A. Di Matteo 标题有什么问题?这就是我想给它命名的方式。
  • 这很奇怪,我只是添加了标签,我没有更改标题,你可以恢复它,绝对。然而,作为我编辑的一部分,历史指出了变化,非常奇怪的行为
  • 好的!只是认为这是像我一样命名标题的问题。认为这是一个编码问题。好的,谢谢

标签: java swing colors awt background-color


【解决方案1】:

改变

f.setBackground(Color.GREEN); 

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

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

您可以在此处阅读有关它的更多信息: JFrame.setBackground() not working -- why?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 2018-03-22
    • 2018-05-03
    相关资源
    最近更新 更多