0

I cannot figure out how to set the location of the text area. Here is my code. Not sure what is not working, obviously JTextArea is not as simple as setting the location like a button.

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

class BackgroundImageJFrame extends JFrame {
    
    JButton b1;
    JLabel l1;
    
    public BackgroundImageJFrame() {
        setTitle("Background Color for JFrame");
        setSize(1000, 1000);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        /*One way-----------------*/
        setLayout(new BorderLayout());
        JLabel background = new JLabel(new ImageIcon("background-landing.png"));
        add(background);
        background.setLayout(new FlowLayout());
        l1 = new JLabel("Here is a button");
        b1 = new JButton("I am a button");
        JTextArea t1 = new JTextArea("enter username", 10, 20);
        t1.setLocation(30, 30);
        background.add(l1);
        background.add(b1);
        background.add(t1);
    }
    
    public static void main(String args[]) {
        new BackgroundImageJFrame();
    }
}

follow up to the last comment, this is what I am trying to achieve

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gianluca
  • 900
  • 8
  • 27
  • 1
    You are also mixing LayoutManagers with absolute Layout, see [here](https://stackoverflow.com/questions/40311819/why-null-layout-and-absolute-positions-are-bad-practice-in-java-swing) for why you should avoid using absolute Layout. Try to use LayoutManagers exclusively. Also see [Extending JFrame vs Delegation](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) – MDK Nov 03 '20 at 19:30
  • 1
    Why are you use a JTextArea to enter a name? A JTextArea is used to display multiple lines of text. A JTextField is used to display a single line of text. – camickr Nov 03 '20 at 20:10

1 Answers1

0

obviously JTextArea is not as simple as setting the location like a button.

You tried to create a label with the text "Here is a button" and a button with the text "I am a button"

You button is not displayed either so that comment doesn't make sense.

The issue is that you are adding components to a JLabel. By default a JLabel does not display child components since it does not have a layout manager.

You will be able to add components to the label by setting a layout manager:

JLabel background = new JLabel(...);
background.setLayout( new FlowLayout() );

Read the section from the Swing tutorial on Layout Managers for more basics and working example of using layout managers. The tutorial code will also show you how to better structure your class so you follow Swing guidelines.

You can also check out: Background Panel for a possible alternate solution.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Glad it helped. Don't forget to "accept" the answer by clicking on the checkmark (beside the answer) so people know the problem has been solved. This applies to all your questions since you have not yet accepted any answers to your 19 questions. – camickr Nov 05 '20 at 03:24