如何在Java中设置文本字段的高度和宽度?

时间:2013-02-11 02:33:44

标签: java swing jtextfield jcreator

我试图让我的JTextField填充宽度并为其设置高度但仍然失败。我尝试添加代码setPreferredSize(new Dimension(320,200));但仍然失败了。有什么方法可以让我的JTextField填充宽度并将高度设置为200或其他什么?

8 个答案:

答案 0 :(得分:21)

你不应该玩高度。让文本字段根据使用的字体确定高度。

如果要控制文本字段的宽度,则可以使用

textField.setColumns(...);

让文本字段确定首选宽度。

或者,如果您希望宽度为父面板的整个宽度,则需要使用适当的布局。也许是BorderLayout的北方。

有关详细信息,请参阅Layout Managers上的Swing教程。

答案 1 :(得分:6)

  

将高度设置为200

Font设置为大型变体(150 + px)。如前所述,使用列控制宽度,并使用将遵循首选宽度和宽度的布局管理器(或约束)。高度。

Big Text Fields

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

public class BigTextField {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new FlowLayout(5));
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));

                // Create big text fields & add them to the GUI
                String s = "Hello!";
                JTextField tf1 = new JTextField(s, 1);
                Font bigFont = tf1.getFont().deriveFont(Font.PLAIN, 150f);
                tf1.setFont(bigFont);
                gui.add(tf1);

                JTextField tf2 = new JTextField(s, 2);
                tf2.setFont(bigFont);
                gui.add(tf2);

                JTextField tf3 = new JTextField(s, 3);
                tf3.setFont(bigFont);
                gui.add(tf3);

                gui.setBackground(Color.WHITE);

                JFrame f = new JFrame("Big Text Fields");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

答案 2 :(得分:2)

有一种方法可能并不完美,但可以满足您的要求。这里的要点是使用特殊尺寸来限制高度。但与此同时,宽度实际上是免费的,因为最大宽度足够大。

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

public final class TestFrame extends Frame{
    public TestFrame(){
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
        p.setPreferredSize(new Dimension(500, 200));
        p.setMaximumSize(new Dimension(10000, 200));
        p.add(new JLabel("TEST: "));

        JPanel p1 = new JPanel();
        p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
        p1.setMaximumSize(new Dimension(10000, 200));
        p1.add(new JTextField(50));

        p.add(p1);

        this.setLayout(new BorderLayout());
        this.add(p, BorderLayout.CENTER);
    }
    //TODO: GUI CREATE
}

答案 3 :(得分:1)

     f.setLayout(null);

添加上述行( f JFrame 容器,其中添加了 JTestField

但是尝试在java中学习'LayoutManager';请参阅教程链接的其他答案。或者尝试此http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

答案 4 :(得分:0)

您正在为要添加JTextField的面板使用什么类型的LayoutManager?

不同的布局管理器以不同的方式处理对它们的大小调整元素,一些方面尊重SetPreferredSize(),而另一些则会扩展组件以适应它们的容器。

请参阅:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

PS。这与eclipse无关,它的java。

答案 5 :(得分:0)

package myguo;
import javax.swing.*;

public class MyGuo {

    JFrame f;
    JButton bt1 , bt2 ;
    JTextField t1,t2;
    JLabel l1,l2;

    MyGuo(){

        f=new JFrame("LOG IN FORM");
        f.setLocation(500,300);
        f.setSize(600,500);
        f.setLayout(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        l1=new JLabel("NAME");
        l1.setBounds(50,70,80,30);

        l2=new JLabel("PASSWORD");
        l2.setBounds(50,100,80,30);

        t1=new JTextField();
        t1.setBounds(140, 70, 200,30);

         t2=new JTextField();
        t2.setBounds(140, 110, 200,30);

        bt1 =new JButton("LOG IN");
        bt1.setBounds(150,150,80,30);

        bt2 =new JButton("CLEAR");
        bt2.setBounds(235,150,80,30);

         f.add(l1);
        f.add(l2);
         f.add(t1);
         f.add(t2);
         f.add(bt1);
         f.add(bt2);

    f.setVisible(true);

    }
    public static void main(String[] args) {
        MyGuo myGuo = new MyGuo();
}
}

答案 6 :(得分:0)

xyz.setColumns()方法控制TextField的宽度。

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



class miniproj extends JFrame {

  public static void main(String[] args)
  {
    JFrame frame=new JFrame();
    JPanel panel=new JPanel();
    frame.setSize(400,400);
    frame.setTitle("Registration");


    JLabel lablename=new JLabel("Enter your name");
    TextField tname=new TextField(30);
    tname.setColumns(45);

    JLabel lableemail=new JLabel("Enter your Email");
    TextField email=new TextField(30);
    email.setColumns(45);
    JLabel lableaddress=new JLabel("Enter your address");
    TextField address=new TextField(30);
    address.setColumns(45);
    address.setFont(Font.getFont(Font.SERIF));
    JLabel lablepass=new JLabel("Enter your password");
    TextField pass=new TextField(30);
    pass.setColumns(45);

    JButton login=new JButton();
    JButton create=new JButton();
    login.setPreferredSize(new Dimension(90,30));
    login.setText("Login");
    create.setPreferredSize(new Dimension(90,30));
    create.setText("Create");



    panel.add(lablename);
    panel.add(tname);
    panel.add(lableemail);
    panel.add(email);
    panel.add(lableaddress);
    panel.add(address);
    panel.add(lablepass);
    panel.add(pass);
    panel.add(create);
    panel.add(login);



    frame.add(panel);
    frame.setVisible(true);
  }
}

enter image description here

答案 7 :(得分:0)

setBounds 仅在 BorderLayout 中工作,使用 BorderLayout 作为框架或容器或面板,并使用 setBounds 设置文本字段的宽度和高度。 看这个代码简单的代码

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

   class uni1
   {
     public static void main(String[] args)
{
    JFrame frm = new JFrame();
    TextField txt = new TextField();
    txt.setBounds(0, 0, 1200, 400);
    frm.add(txt,BorderLayout.NORTH);
    frm.setLayout(new BorderLayout());
    frm.setVisible(true);
    frm.setDefaultCloseOperation(frm.EXIT_ON_CLOSE);
}

}

相关问题