2012-07-15 90 views
3

有人可以请帮我在运行时如何设置一个JTextField的宽度?我希望我的文本字段在运行时调整大小。它会询问用户的长度,然后输入将改变文本字段的宽度。如何在运行时设置JTextField的宽度?

if(selectedComponent instanceof javax.swing.JTextField){ 
    javax.swing.JTextField txtField = (javax.swing.JTextField) selectedComponent; 
    //txtField.setColumns(numInput); //tried this but it doesn't work 
    //txtField.setPreferredSize(new Dimension(numInput, txtField.getHeight())); //also this 
    //txtField.setBounds(txtField.getX(), txtField.getY(), numInput, txtField.getHeight()); 
    //and this 
    txtField.revalidate(); 
} 

我使用null布局这一点,因为我在编辑模式。

回答

5

您只需使用jTextFieldObject.setColumns(int columnSize)。这会让你在运行时增加它的大小。您在最后无法完成的原因是null布局。这是为什么不鼓励使用null Layout/Absolute Positioning的主要原因之一。下面是在尝试你的手一个小例子:

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

public class JTextFieldExample 
{ 
    private JFrame frame; 
    private JPanel contentPane; 
    private JTextField tfield; 
    private JButton button; 
    private int size = 10; 

    private ActionListener action = new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      String input = JOptionPane.showInputDialog(
           frame, "Please Enter Columns : " 
               , String.valueOf(++size)); 
      tfield.setColumns(Integer.parseInt(input));     
      contentPane.revalidate(); 
      contentPane.repaint(); 
     } 
    }; 

    private void createAndDisplayGUI() 
    { 
     frame = new JFrame("JTextField Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

     contentPane = new JPanel(); 
     contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 

     tfield = new JTextField(); 
     tfield.setColumns(size); 

     JButton button = new JButton("INC Size"); 
     button.addActionListener(action); 

     contentPane.add(tfield); 
     contentPane.add(button); 

     frame.getContentPane().add(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new JTextFieldExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 

对于你需要呼吁JTextFieldsetSize()为了获得结果,但你应该始终牢记之所以这种做法是绝对定位的情况下气馁,如Java Doc's第一段所述。

+2

哦。 :(在编辑之前,我真的很喜欢这个答案,说.. *“绝对定位..”* Eeek!让我们不要让新手走向疯狂和怪物的道路哦,好吧,无论如何+1 :) – 2012-07-15 04:05:06

+0

+ 1我的答案有效,但这样更好/更彻底 – 2012-07-15 04:10:21

+0

@AndrewThompson:Thankx指出我的错误,因此我对上述方向做了一些更改:-) – 2012-07-15 04:55:49

2

我只是通过使用setBounds来调整文本字段的大小。看看下面的例子:

import java.awt.Dimension; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class Resize extends JFrame{ 
    public JTextField jtf = new JTextField(); 

    public Resize(){ 
    //frame settings 
    setTitle("Resizable JTextField"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(null); 
    setSize(new Dimension(600,400)); 
    setResizable(false); 

    //init and add text field to the frame 
    add(jtf); 
    jtf.setBounds(20,50,200,200); 

    //button to change text field size 
    JButton b = new JButton("Moar."); 
    add(b); 
    b.setBounds(20,20,b.getPreferredSize().width,b.getPreferredSize().height); 
    b.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent evt){ 
      jtf.setBounds(20,50,jtf.getSize().width+10,jtf.getSize().height); //THIS IS WHERE THE RESIZING HAPPENS 
     } 
     }); 

    setVisible(true); 
    } 
    public static void main(String[] args){ 
    Resize inst = new Resize(); 
    } 
} 
+0

+1另一个选择:-) – 2012-07-15 04:54:15

+0

-1为空布局和手动调整大小 - 不管是什么问题,这不是解决方案 – kleopatra 2012-07-15 08:49:13

+0

@kleopatra够公平的,尽管OP表示他们想使用'null'布局 – 2012-07-15 15:47:32

相关问题