0

Following is the code in which I want to change the background of JFrame. I have also used the set Background property on the object of JFrame but it does nothing. Help is highly appreciated.

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

public class Rog{
    Rog(){
        JFrame jframe=new JFrame("check");
        jframe.setLayout(null);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);

    JTextField jtextfield=new JTextField();
    jtextfield.setBounds(70, 50, 100, 50);

    JButton jbutton=new JButton("button");
    jbutton.setBounds(200, 300, 100, 50);

    JLabel jlabel=new JLabel("Input:");
    JLabel notify=new JLabel();
    jlabel.setBounds(25, 50, 50, 50);
    notify.setBounds(50, 400, 100, 50);

    jframe.add(jbutton);
    jframe.add(jtextfield);
    jframe.add(jlabel);
    jframe.add(notify);

    jframe.setSize(950, 1000);
    jframe.setBackground(Color.red);

    jbutton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            System.out.println(jtextfield.getText());
            notify.setText("text saved");
        }
    });
}

    public static void main(String args[]){
        new Rog();
    }

}

Omar Malik
  • 23
  • 5
  • 1
    I gave you help in your last question: https://stackoverflow.com/questions/50065116/jbutton-not-appearing-on-jframe and you are completely ignoring it. So I won't bother wasting my time with an simple one line answer this time. You code show you still have not read the tutorial and don't understand the basics of how Swing works. We are not here to help you with every little problem, when you don't even attempt to learn the basics. – camickr Apr 27 '18 at 20:00
  • 1
    As per @camickr comment, it appears you are still using null layout. Well, when I first learnt swing, I was shown null layout and once I was shown the rather large pitfalls that you can encounter with it, I took some time to learn about proper layouts. I suggest you consider the same. – achAmháin Apr 27 '18 at 20:08
  • If this approach of using null layouts is wrong then why have they created it? – Omar Malik Apr 27 '18 at 20:10
  • Its for applications that can't use a layout manager. For example when you drag components around the frame you can't write a layout manager to know where the user will drag the component to. There is absolutely no reason to use a null layout for your code so take the time to learn how to code in Swing properly. A null layout will cause you problems in the future when you need more complex GUI and the ability to scroll panels etc. – camickr Apr 27 '18 at 20:18
  • Start reading the Swing tutorial. The section on [Using Top Level Containers](https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html) is a good place to start. – camickr Apr 27 '18 at 20:19
  • Also have a look at [How to use root panes](https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) which will explain why your approach is causing your issues – MadProgrammer Apr 27 '18 at 21:11

0 Answers0