2012-10-08 46 views
0

我想單擊按鈕時動態添加文本框,但要獲取的值和按鈕位於一個類中,並且要在其中添加文本框和滑塊的面板在不同的階級中。代碼是 -動態添加來自不同類的文本框和JSlider

public class TipSplitting extends JPanel 

JLabel lblNoOfGuests = new JLabel("No. Of guests"); 
    lblNoOfGuests.setBounds(10, 26, 95, 14); 
    add(lblNoOfGuests); 

private JTextField noofguests = new JTextField(); 
    noofguests.setBounds(179, 23, 86, 20); 
    add(noofguests); 
    noofguests.setColumns(10); 
JButton btnTiptailoring = new JButton("TipTailoring"); 
    btnTiptailoring.setBounds(117, 286, 89, 23); 
    add(btnTiptailoring); 

public class TipTailoring extends JPanel {} 

在這個類中,我需要根據編號動態地創建文本字段。進入。在變量noofguests中點擊上一課中的按鈕。

+2

卸下'setBounds'呼叫並使用'LayoutManager'其允許組件的動態數。然後,簡單地創建並添加組件 – Robin

+1

..你的問題是什麼? –

+0

我的問題是如何在一個面板中動態創建文本框,該面板處於不同類中,從不同類中的文本字段輸入但在同一個包中? – user1661816

回答

3

我不能真正看到問題所在,但是這裏有一些簡單的演示代碼。

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class TestDynamicallyAddedTextFields { 

    private void initUI() { 
     JFrame frame = new JFrame(TestDynamicallyAddedTextFields.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JPanel textfieldContainerPanel = new JPanel(); 
     textfieldContainerPanel.setLayout(new GridBagLayout()); 
     JLabel nrOfGuests = new JLabel("Nr. of guests"); 
     final JFormattedTextField textfield = new JFormattedTextField(); 
     textfield.setValue(Integer.valueOf(1)); 
     textfield.setColumns(10); 
     JButton add = new JButton("Add"); 
     add.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if (textfield.getValue() != null) { 
        addTextFieldsToPanel((Integer) textfield.getValue(), textfieldContainerPanel); 
       } 
      } 
     }); 
     JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING)); 
     panel.add(nrOfGuests); 
     panel.add(textfield); 
     panel.add(add); 
     frame.add(panel, BorderLayout.NORTH); 
     frame.add(new JScrollPane(textfieldContainerPanel)); 
     frame.setSize(300, 600); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    protected void addTextFieldsToPanel(Integer value, JPanel textfieldContainerPanel) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     gbc.gridheight = 1; 
     for (int i = 0; i < value; i++) { 
      textfieldContainerPanel.add(new JTextField(20), gbc); 
     } 
     textfieldContainerPanel.revalidate(); 
     textfieldContainerPanel.repaint(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TestDynamicallyAddedTextFields().initUI(); 
      } 
     }); 
    } 

} 

而結果:

Result image

+0

+1表示SSCCE :-) – kleopatra