-1

I'm trying to adjust the size of a JTextField but it doesnt seem to work, I'm suppoed to get this :

enter image description here

What I get is this :

enter image description here

Here is the code if it matters :

JLabel placeNumberT = new JLabel( "<html><span bgcolor=\"lime\">place n°</span></html>");
placeNumberP.add(placeNumberT);
JTextField inputField = new JTextField(1);
placeNumberP.add(inputField);

I tried adjusting the size with the font but it doesn't change a thing

matt
  • 10,892
  • 3
  • 22
  • 34
Toven
  • 1
  • 1
  • You might want to start by looking into how layout managers work. Since you didn't include any information on those I assume you don't know about them - and we are lacking the info on what's being used to define the component sizes. – Thomas Jan 19 '23 at 12:57
  • The layout manager is an important part of this, what are these two going to be part of? I have this problem quite often so I expect it is a duplicate. – matt Jan 19 '23 at 12:58
  • For example https://stackoverflow.com/a/26822644/2067492 – matt Jan 19 '23 at 13:00
  • Additionally, I'd say the lime background being so small is probably due to the use of html, i.e. the span would normally just be around the text and that's it. Is there any real reason for you to use html instead of setting the background color directly on the label? – Thomas Jan 19 '23 at 13:00

1 Answers1

-2

You can use setBounds method for setting the size of components

setBounds(int x-coordinate, int y-coordinate, int width, int height)

From the code You have given, You can try this modified code:

JLabel placeNumberT = new JLabel( "<html><span bgcolor=\"lime\">place n°</span></html>");

placeNumberT.setBounds(50,50, 100,30); 

placeNumberP.add(placeNumberT);

JTextField inputField = new JTextField(1);

inputField.setBounds(50,100, 100,30);

placeNumberP.add(inputField);
Atul Patel
  • 11
  • 2