1

I am new to JFrame, components and the such, but I'm trying to change a size of a specific text field so you can actually see what you put into it. Here's the code:

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class InterestCalculator extends JFrame{

    private static final long serialVersionUID = 1L;

    JLabel desc = new JLabel("This is a simple interest calculator.\n"
            + " Enter three fields to get the fourth.");
    JLabel inteLabel = new JLabel("\nInterest: ");
    JTextField inte = new JTextField();

    public static void main(String [] args) {
        InterestCalculator comp = new InterestCalculator();
        comp.FrameHandler();
    }

        public void FrameHandler() {

        setSize(500, 500);
        setTitle("Template");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new FlowLayout());

        add(desc);
        add(inteLabel);
        add(inte);

        inte.setSize(new Dimension(100,15));

        validate();
    }
}

This is what comes out: https://i.stack.imgur.com/ldwed.png So, how can I fix this?

Zunon
  • 137
  • 1
  • 8
  • 1
    If you have multiple questions, ask them separately. – chancea Feb 03 '15 at 17:01
  • *"BONUS: to save some time for later,.."* Ask a separate question NOW. SO is a Q&A site, where each thread should have one specific question, not a help desk. – Andrew Thompson Feb 03 '15 at 17:03
  • BTW - replace `setSize(500, 500);` with `pack()` after components are added. The first is just a guess at how large the GUI needs to be. The second will result in a size that is *exactly* as large as it needs to be. – Andrew Thompson Feb 03 '15 at 17:05
  • 1
    Don't use validate. Instead the `setVisible(...)` statement should be the last statement executed AFTER you add all the components to the frame. Also, you should invoke `pack()` before the setVisible(...); – camickr Feb 03 '15 at 17:07

1 Answers1

4
JTextField inte = new JTextField();

Should set a number of columns, I.E.:

JTextField inte = new JTextField(10);

See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)

The same applies to setSize(..).

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433