Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

easy question - Swing applet background color

I am trying to make a swing applet with a green background, the code fragment below doesn't work, it just displays the background with the default gray color. How to i make it work? This worked fine for a normal applet

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

/*
<APPLET CODE = "swinger.class" WIDTH = 400 HEIGHT = 400>
</APPLET>
*/

public class swinger extends JApplet
{
     
     public void init()
     {
            getContentPane().setLayout(null);
         setSize(350,250);
         this.setBackground(Color.green);                  
     }

}
0
danBosh
Asked:
danBosh
  • 9
  • 6
1 Solution
 
antonsigurCommented:
Hi, this should work, but if you then add a JPanel on it, you must make the background af that panel green to!

I'm working on a applet that needs to be in same color as web page, and I must put the bacground color on every panel...

You can do it the same way

JPanel p = new JPanel();
p.setBackground(Clor.green);

let's hop this is the problem
0
 
danBoshAuthor Commented:
aye that should work but what about for labels,text fields,etc:

myLabel = new JLabel("text");
myLabel.setBounds(170,100,90,20);
myLabel.setBackground(Color.yellow);
getContentPane().add(myLabel);
0
 
antonsigurCommented:
No, I don't think you need to do that. Labels don't have any visible background, textfield, I think, have white background (not sure you wanna change that to)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
danBoshAuthor Commented:
so how do i change the colour of text fields and labels:
0
 
danBoshAuthor Commented:
so how do i change the colour of text fields and labels:
0
 
antonsigurCommented:
JTextField f = new JTextField();
f.setForeground(Color.red); //text color)
f.setBackground(Color.blue); //Field color)

JLabel l = new JLabel("Some label");
l.setForeground(Colro.red); //Text color)
0
 
antonsigurCommented:
You can create your own textfields and labels...

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

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
antonsigurCommented:
You can create your own textfields and labels...

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

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
antonsigurCommented:
You can create your own textfields and labels...

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

public class VTextField extends JTextField
{
  VTextField(int lengd)
  {
    super();
    /* Force size */
    setSize(lengd, 25);
    setPreferredSize(new Dimension(lengd, 25) );
    setMaximumSize(new Dimension(lengd, 25) );
    setMinimumSize(new Dimension(lengd,25));
    /* Set color */
    this.setForeground(Color.red);
  }
}

Now you just juse VTextField instead of JTextField...
0
 
antonsigurCommented:
Hmm, this is strange, I only posted this once... but, lucky number three! hehe
0
 
danBoshAuthor Commented:
?
but when i did this:
JLabel myLabel = new JLabel("text");
myLabel.setBounds(170,100,90,20);
myLabel.setBackground(Color.yellow);
getContentPane().add(myLabel);

it didnt work

so why would this work:
JLabel l = new JLabel("Some label");
l.setForeground(Color.red); //Text color)
l.setBackground(Color.blue); //Text color)  
?
0
 
antonsigurCommented:
myLabel.setBackground(Color.yellow); //Nothing happends (no backround on labels)
myLabel.setForeground(Color.yellow); //Yellow Text
0
 
danBoshAuthor Commented:
sorry for being thick here, so it is impossible to change the color of textfields and labels in java swing?
0
 
antonsigurCommented:
Yes it is.

try this ...


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

public class Bensin extends javax.swing.JApplet {

    public static Color getBgColor() {
         return new Color(255,255,204);
    }


    public void init() {

        this.setBackground(this.getBgColor());
        this.setSize(600,200);
        JTextField f = new JTextField();
        f.setForeground(Color.red); //text color)
        f.setBackground(Color.blue); //Field color)

        JLabel l = new JLabel("Some label");
        l.setForeground(Colro.red); //Text color)
        this.getContentPane().add(f);
        this.getContentPane().add(l);


    }


}
0
 
danBoshAuthor Commented:
for any else wiv simular problems this is how i did it

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

//import java.awt.Color.*;
/*
<APPLET CODE = "swinger.class" WIDTH = 400 HEIGHT = 400>
     <param name="param1" value="dan">
       <param name="param2" value="biff">
       <param name="param3" value="Chris">
 </APPLET>

*/
public class swinger extends JApplet
{
     public static Color getBgColor() {
        return new Color(255,255,204);
   }
     
     public void init()
     {
          Container c = getContentPane();
        c.setLayout(new FlowLayout());
          c.setSize(350,250);
          c.setBackground(Color.green);
                 
          JTextField f = new JTextField(20);
        JLabel l = new JLabel("Some label");
     
       c.add(f);
       c.add(l);
       f.setForeground(Color.red); //text color)
       f.setBackground(Color.blue); //Field color)
       f.setText("foff");
       l.setForeground(Color.red); //Text color)
                   
     }

}
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now