1

Im trying to make a game. I have a frame and insert jbutton jlabel jtextfield on the frame and the frame has a background. But now it doesnt appear. Why isnt buttons etc visible? Can you fix these codes?

public class NewPlayer extends JFrame {

    public NewPlayer() {

        JLabel backGround = new JLabel(new ImageIcon("images\\fiha2taban2.png"));
        setTitle("FiHa");
        setSize(750, 550);
        getContentPane().add(backGround);
        setLocationRelativeTo(null); // Center the frame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage("images\\iconfh.png"));

        Color renk=new Color(255,250,155);

        // Create two labels and set their components
        JLabel label1= new JLabel("NEW PLAYER !");
        label1.setBounds(220,200,300,40);
        this.add(label1);
        label1.setFont(new Font("Times New Roman", Font.BOLD , 40));

        JLabel label2= new JLabel("Please enter your name: ");
        label2.setBounds(270,290,249,40);
        this.add(label2);
        label2.setFont(new Font("Times New Roman", Font.BOLD , 20));

        // Create a text field and set its components
        JTextField txtField = new JTextField("", 20);
        txtField.setBounds(220,335,300,40);
        this.add(txtField);
        txtField.setFont(new Font("Times New Roman", Font.BOLD , 16));

        // Create a button and set its components
        JButton button1 = new JButton("OKAY");
        button1.setBounds(315,396,110,40);
        this.add(button1);
        button1.setFont(new Font("Times New Roman", Font.BOLD , 16));
        button1.setBackground(renk);
        }
    }
}
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
crescent
  • 61
  • 4
  • Move `setVisible(true);` to the end of the constructor; Use an appropriate layout manager – MadProgrammer Mar 15 '16 at 07:58
  • I did it. it worked firstly. all appears. but background went. Then I press the button, all of the frame became a button – crescent Mar 15 '16 at 08:07
  • `JFrame` is using a `BorderLayout` by default – MadProgrammer Mar 15 '16 at 08:16
  • Okay,Which layout do I have to use ? – crescent Mar 15 '16 at 08:22
  • The one which meets your needs. Personally I'd use a `GridBagLayout`, see [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details – MadProgrammer Mar 15 '16 at 08:23
  • As @MadProgrammer said, it is a layout issue. For `BorderLayout` `add` method placing the given component to the center position. So each time time you called `this.add`, previous component removing from the main container. You should learn to how to use layouts first. – rdonuk Mar 15 '16 at 09:54
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 3) `label1.setFont(new Font("Times New Roman", Font.BOLD , 40));` Better to establish the font just once, and using the cross platform constants, e.g. `Font f = new Font(Font.SERIF, Font.BOLD , 12));` then.. – Andrew Thompson Mar 15 '16 at 11:00
  • .. use `deriveFont(..)` as needed, e.g. `label1.setFont(f.deriveFont(40f)); //check docs for details..` – Andrew Thompson Mar 15 '16 at 11:01

0 Answers0