无法调整JTextField的大小

时间:2015-02-03 16:58:52

标签: java swing resize jframe jtextfield

我是JFrame,组件等的新手,但我正在尝试更改特定文本字段的大小,以便您可以实际看到您放入的内容。这是代码:

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();
    }
}

这就是出现的结果:http://i.imgur.com/SoOEMNT.png 那么,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

JTextField inte = new JTextField();

应设置多个列,I.E。:

JTextField inte = new JTextField(10);

另见Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?(是)。

同样适用于setSize(..)