Hey guys! So, I created this form, but I can't seem to add color to it. I'm trying to make it color red. I've been trying to change the color for 2 days cuz I'm a noob :)
Thanks guys! Here's code by the way:

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

public class Login extends JFrame implements ActionListener{
	
    private JTextField jcomp1;
    private JLabel jcomp2;
    private JPasswordField jcomp3;
    private JLabel jcomp4;
    private JButton jcomp5;
    private JLabel jcomp6;
    
    private JPanel login = new JPanel();
    
    public Login() {
    	
        jcomp1 = new JTextField (10);
        jcomp2 = new JLabel ("Username:");
        jcomp3 = new JPasswordField (10);
        jcomp4 = new JLabel ("Password:");
        jcomp5 = new JButton ("Log in");
        jcomp6 = new JLabel ("Shirley's Coffee Shop");
         
        setPreferredSize (new Dimension (624, 413));
        setLayout (null);


        add (jcomp1);
        add (jcomp2);
        add (jcomp3);
        add (jcomp4);
        add (jcomp5);
        add (jcomp6);
         
        jcomp1.setBounds (285, 135, 100, 25);
        jcomp2.setBounds (215, 135, 100, 25);
        jcomp3.setBounds (285, 175, 100, 25);
        jcomp4.setBounds (215, 175, 100, 25);
        jcomp5.setBounds (285, 265, 100, 25);
        jcomp6.setBounds (265, 60, 145, 30);
        getContentPane().add (login);
        
        jcomp5.addActionListener(this);
    }


    public static void main (String[] args) {
		Login frame = new Login();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(0,0,800,800);
		frame.setResizable(false);
		frame.setVisible(true);
    }
    
     public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jcomp5) {
        	
        	MainMenGra mainMenuG;
        	mainMenuG = new MainMenGra();
        	setVisible(false);
			
        }

}
}

Recommended Answers

All 13 Replies

I'm trying to make it color red. I've been trying to change the color for 2 days

Where? Nothing in that code attempts to change the color of anything.

That's the original code I started with. I removed the code I added earlier. My earlier attempt of adding color was a failure :(

Use setOpaque(true) and then setBackground(Color). Try it and post that if it "doesn't work".

What is the syntax for that? So sorry I'm new to all this.

I already showed it to you.

component.setOpaque(true)
component.setBackground(someColor)

Give it a try.

So I added the code but it says: <identifier> expected.

Well, post that. You do realise that that "code" was not cut-n-paste, right? You have to modify to your code, but the syntax is the same.

I'm so sorry, but can you please teach me how to do this?

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


public class Login extends JFrame implements ActionListener{
    
    private JTextField jcomp1;
    private JLabel jcomp2;
    private JPasswordField jcomp3;
    private JLabel jcomp4;
    private JButton jcomp5;
    private JLabel jcomp6;
    
    private JPanel login = new JPanel();
    
    component.setOpaque(true)
    component.setBackground(red)
  
    
    public Login() {
        
        jcomp1 = new JTextField (10);
        jcomp2 = new JLabel ("Username:");
        jcomp3 = new JPasswordField (10);
        jcomp4 = new JLabel ("Password:");
        jcomp5 = new JButton ("Log in");
        jcomp6 = new JLabel ("Shirley's Coffee Shop");
         
        setPreferredSize (new Dimension (624, 413));
        setLayout (null);


        add (jcomp1);
        add (jcomp2);
        add (jcomp3);
        add (jcomp4);
        add (jcomp5);
        add (jcomp6);
         
        jcomp1.setBounds (285, 135, 100, 25);
        jcomp2.setBounds (215, 135, 100, 25);
        jcomp3.setBounds (285, 175, 100, 25);
        jcomp4.setBounds (215, 175, 100, 25);
        jcomp5.setBounds (285, 265, 100, 25);
        jcomp6.setBounds (265, 60, 145, 30);
        getContentPane().add (login);
        
        jcomp5.addActionListener(this);
    }


    public static void main (String[] args) {
        Login frame = new Login();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0,0,800,800);
        frame.setResizable(false);
        frame.setVisible(true);
    }
    
     public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jcomp5) {
            
            MainMenGra mainMenuG;
            mainMenuG = new MainMenGra();
            setVisible(false);
            
        }

}
}

I suggest you take a book (or look at some tutorials online) and write your Login class yourself, rather than copying and pasting it off the internet, judging from where you pasted the code you clearly have no idea how your program works.

http://download.oracle.com/javase/tutorial/getStarted/

can you write a hello world program?

can you write a hello world applet?

Do both those things, then make a hello world applet with a button etc etc


When you have done that, come back and ask again - I'm sure you wont even need to

I curiously typed in a portion of you code to try and find out where you copied it from. It seems this is some kind of class assignment? Someone else posted a question with very very similar code in a "login" class on 28 September 2009, they managed to make it red though

I curiously typed in a portion of you code to try and find out where you copied it from. It seems this is some kind of class assignment? Someone else posted a question with very very similar code in a "login" class on 28 September 2009, they managed to make it red though

Could you post the link to that, so I can study that person's code? Thanks

I purposly didnt, because by the looks of things you wouldnt "study" but copy and paste that persons code.

To be honest, if you cant be bothered learning why are you doing a course in java?

I'm so sorry, but can you please teach me how to do this?

Not until you at least try. We are not here to do your homework for you. You do not learn anything that way, and just makes the rest of the course that much harder (as you are probably already learning as all your other assignments have probably been done by others).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.