开发者

Java GUI programming: Setting the Fore/Background

开发者 https://www.devze.com 2023-04-01 05:41 出处:网络
I\'m just getting into GUI programming, slowly learning. However I\'m having a problem right of the bat. I can\'t get the Fore/Background color to change in my window at all.

I'm just getting into GUI programming, slowly learning.

However I'm having a problem right of the bat. I can't get the Fore/Background color to change in my window at all.

However when I add a label via JLabel and then use setFore/Back, they change colors just fine. Just not the whole window.

I thought .setForeground and .setBackground are supposed to change the color of the window?

import javax.swing.*;
import java.awt.*;

public class MyWindow {

    public static void main(String args[])
    {
         Runnable init = new Runnable()
         {
             public void run()
             {

                    JFrame myWindow = new JFrame("Hola!");
                    myWindow.setForeground(Color.YELLOW);
                    myWindow.setBackground(Color.Y开发者_运维百科ELLOW);
                    myWindow.setSize(400, 300);

                    myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myWindow.setLayout(null);

                    myWindow.setVisible(true);

             }

         };
         SwingUtilities.invokeLater(init);
    }


}


First of all, do not use a null layout. Let the layout manager do its job. Second of all, you need to set the background of the content pane of the JFrame instance, as such

myWindow.getContentPane().setBackground(Color.YELLOW);

See also:

  • Using Top-Level Containers


you cannot color a frame. However you can color the ContentPane inside.

import javax.swing.*;
import java.awt.*;

    public class MyWindow {

        public static void main(String args[])
        {
             Runnable init = new Runnable()
             {
                 public void run()
                 {

                        JFrame myWindow = new JFrame("Hola!");

    myWindow.getContentPane().setBackground(Color.YELLOW);

                        myWindow.setSize(400, 300);

                        myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        myWindow.setLayout(null);

                        myWindow.setVisible(true);

                 }

             };
             SwingUtilities.invokeLater(init);
        }  
    }

this should fix your problem...


tangina naman ang bobo naman neto, pokegooo

import javax.swing.; import java.awt.;

public class MyWindow {

    public static void main(String args[])
    {
         Runnable init = new Runnable()
         {
             public void run()
             {

                    JFrame myWindow = new JFrame("Hola!");

myWindow.getContentPane().setBackground(Color.YELLOW);

                    myWindow.setSize(400, 300);

                    myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    myWindow.setLayout(null);

                    myWindow.setVisible(true);

             }

         };
         SwingUtilities.invokeLater(init);
    }  
}

// tanga amputa aral ka muna dudong // pakangkang

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号