2012-05-17 98 views
0

我點擊'添加'按鈕時創建了文本框和標籤。我給出了x和y座標,但出現的文本框不正確。 如何糾正它?以及如何增加文本框的寬度?java:通過點擊按鈕動態生成文本框

import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.border.LineBorder; 

public class SS 
{ 
// Field members 
static JPanel panel = new JPanel(); 
static Integer indexer = 1; 
static List<JLabel> listOfLabels = new ArrayList<JLabel>(); 
static List<JTextField> listOfTextFields = new ArrayList<JTextField>(); 

public static void main(String[] args) 
{  
    // Construct frame 
    JFrame frame = new JFrame(); 
    frame.setLayout(new GridBagLayout()); 
    frame.setPreferredSize(new Dimension(800, 800)); 
    frame.setTitle("My Example"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    // Frame constraints 
    GridBagConstraints frameConstraints = new GridBagConstraints(); 

    // Construct button 
    JButton addButton = new JButton("Add"); 
    addButton.addActionListener(new ButtonListener()); 

    // Add button to frame 
    frameConstraints.gridx = 0; 
    frameConstraints.gridy = 0; 
    frame.add(addButton, frameConstraints); 

    // Construct panel 
    panel.setPreferredSize(new Dimension(400, 400)); 
    panel.setLayout(new GridBagLayout()); 
    panel.setBorder(LineBorder.createBlackLineBorder()); 

    // Add panel to frame 
    frameConstraints.gridx = 0; 
    frameConstraints.gridy = 1; 
    frameConstraints.weighty = 20; 
    frame.add(panel, frameConstraints); 

    // Pack frame 
    frame.pack(); 

    // Make frame visible 
    frame.setVisible(true); 
} 

static class ButtonListener implements ActionListener 
{ 
    @Override 
    public void actionPerformed(ActionEvent arg0) 
    {  
     // Clear panel 
     panel.removeAll(); 

     // Create label and text field 
     listOfTextFields.add(new JTextField()); 
     listOfLabels.add(new JLabel("Name " + indexer)); 

     // Create constraints 
     GridBagConstraints textFieldConstraints = new GridBagConstraints(); 
     GridBagConstraints labelConstraints = new GridBagConstraints(); 

     // Add labels and text fields 
     for(int i = 0; i < indexer; i++) 
     { 
      // Text field constraints 
      textFieldConstraints.gridx = 20; 
      textFieldConstraints.gridy = i; 

      // Label constraints 
      labelConstraints.gridx = 1; 
      labelConstraints.gridy = i; 

      // Add them to panel 
      panel.add(listOfTextFields.get(i), textFieldConstraints); 
      panel.add(listOfLabels.get(i), labelConstraints); 
     } 

     // Align components top-to-bottom 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = indexer; 
     c.weighty = 1; 
     panel.add(new JLabel(), c); 

     // Increment indexer 
     indexer++; 
    } 
} 

}

+0

[你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

我想dyanamically生成文本框,通過點擊按鈕。 –

+0

寬度可以使用「setSize」方法設置。 – Buffalo

回答

3

要獲得幀你需要在你的actionPerformed方法的底部調用包中的文本框中刷新。

frame.pack(); 

爲此,您需要將幀作爲類變量。

static JFrame frame; 

對於尺寸的網格包佈局將覆蓋你的setSize所以你可以給它的重量,使之延伸到填補空間。這可以在您的其他textFieldContraints調用之後進行。

textFieldConstraints.weightx = 1; 
textFieldConstraints.fill = GridBagConstraints.HORIZONTAL; 

這應該使文本框出現,當你點擊按鈕並拿起框。

0

增加文本字段的寬度。

listOfTextFields.add(new JTextField(null,10));