0

I have created a JTextField and I have used the setBounds() method which is not working. There are no bugs in my code so Im not sure whats wrong. I will include pictures and image of output below.

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


public class A1 extends JFrame {

    public A1(String title) {
        super(title);
        this.setSize(500, 500);
        this.setLocation(100, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
                
        JTextField text1 = new JTextField();
        text1.setBounds(50, 100, 200, 30);
                
        Container mainContainer = this.getContentPane();
            
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setBackground(Color.GRAY);
        tabbedPane.setForeground(Color.BLACK);
        tabbedPane.setFont(new Font("Century Gothic", Font.BOLD, 14));
        
        //tab1
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.CYAN);
        panel1.add(text1);
        
        
        
        //tab2
        JPanel panel2 = new JPanel();
        
        
        
        
        tabbedPane.addTab("Future/Present Value", panel1);
        tabbedPane.addTab("Financial Ratios", panel2);
        
        mainContainer.add(tabbedPane);
        
        
    }
    
    
    public static void main(String[] args) {
        A1 launch = new A1("Financial Calculator");
        launch.setVisible(true);
        
    }

}

enter image description here

Im honestly baffled why it doesnt change size. Any help would be much appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nial Dixon
  • 33
  • 1
  • 7
  • Setting bounds, size, position only works if the container that holds said components uses a null layout, but having said that, I will add that that this is something you should never do, and in fact, you should *never* set the bounds or size or even the preferred size of a text component, since that will prevent it from working as it should. Rather, learn the layout managers and *use them*, and if you need to make a JTextField wider, set its column property appropriately, and if you need to make it larger, set its font appropriately. – Hovercraft Full Of Eels Feb 24 '21 at 02:32
  • 1
    so here, I'd do something like: `JTextField text1 = new JTextField(12);` or other int constant passed in that gives the JTextField a columns value – Hovercraft Full Of Eels Feb 24 '21 at 02:36
  • Okay, I thinks its to do with my layout and that im in a flowLayout i dont know if im right in thinking that – Nial Dixon Feb 24 '21 at 02:40
  • but yh putting the number in made it work perfectly – Nial Dixon Feb 24 '21 at 02:41
  • Doing that sets the columns property of the JTextField (as the API will tell you). .You can also set it directly by calling `.setColumns(...)` on your JTextField object. – Hovercraft Full Of Eels Feb 24 '21 at 02:43

0 Answers0