简体   繁体   English

动态添加textField时的setlayout

[英]setlayout when dynamic adding textField

I am adding dynamic JTextField and JLabel in panel1 but I am not able to set the layout of JTextField and JLabel . 我在panel1添加了动态JTextFieldJLabel ,但是我无法设置JTextFieldJLabel的布局。 I need to add JTextfield and JLabel to panel1 and I add panel1 to panel . 我需要将JTextfieldJLabel添加到panel1然后将panel1添加到panel I need to add JTextField s and JLabel s in a Top-Bottom manner and set layout. 我需要以上下方式添加JTextFieldJLabel并设置布局。 panel1 and panel are instances of JPanel. panel1panel是JPanel的实例。

My code : 我的代码:

public class MakeScrollablePanel extends JFrame implements ActionListener
{

    static JButton jButton11,jButton12;
    static JPanel panel,panel1;
    static JTextField jTextFields;
    static JLabel label;
    static JComboBox<String> jComboBox;
    static Dimension dime,dime1,dime2,dime3,dime4,dime5;
    static JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();  
    BoxLayout bx=null;//  @jve:decl-index=0:

    int count=1,i=0;

    public MakeScrollablePanel() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show(); 

        add(jButton11);
        add(scroll);
        dime=new Dimension(600,550);
        setSize(dime);
        setTitle("Priyank Panel");
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);

    }
     private void Show()
     {
         jButton11=new JButton("Add Designation");
         panel=new JPanel(); 
         bx=new BoxLayout(panel,BoxLayout.Y_AXIS);

         scroll=new JScrollPane(panel);
         dime1=new Dimension(500,3000);
         dime5=new Dimension(500,450);
         panelConstraints = new GridBagConstraints();
         scroll.setPreferredSize(dime5);
         scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         panel.setLayout(bx);
         panel.add(Box.createHorizontalBox());
         panel.setBorder(LineBorder.createBlackLineBorder());
         panel.setBackground(new Color(204, 230 , 255));
        jButton11.addActionListener(this);

     }
     public void actionPerformed(ActionEvent event) 
        {
            if(event.getSource()==jButton11)
             {
                label=new JLabel("Add Designation "+count  +" :-");
                jTextFields=new JTextField(30);

                panel1=new JPanel();
                panel1.setBackground(new Color(204, 230 , 255));
                panel1.add(label);
                panel1.add(jTextFields); 
                    panel.add(panel1);
                    panel1.revalidate();
                                panel.revalidate();
                     panel.updateUI();
                count++;
                i++;
             }
        }

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

            @Override
            public void run() {
                new MakeScrollablePanel();

            }
        });

    }

}

still not working 还是行不通

still I can't see thre any problem with (depsite fact that there is used BoxLayout instead of GridLayout, but result could be very similair in the case that is used many JTextFields) 仍然我看不到任何问题(depsite事实,有使用BoxLayout而不是GridLayout,但结果可能非常类似于使用许多JTextFields的情况)

在此输入图像描述

.

在此输入图像描述

.

在此输入图像描述

from (little bit) modifyied OPs code 从(一点点)修改OPs代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class MakeScrollablePanel extends JFrame implements ActionListener {

    private JButton jButton11, jButton12;
    private JPanel panel, panel1;
    private JTextField jTextFields;
    private JLabel label;
    private JComboBox<String> jComboBox;
    private Dimension dime, dime1, dime2, dime3, dime4, dime5;
    private JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();
    private BoxLayout bx = null;//  @jve:decl-index=0:
    private int count = 1, i = 0;

    public MakeScrollablePanel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show();
        add(jButton11, BorderLayout.NORTH);
        add(scroll);
        setTitle("Priyank Panel");
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(true);
    }

    private void Show() {
        jButton11 = new JButton("Add Designation");
        panel = new JPanel();
        bx = new BoxLayout(panel, BoxLayout.Y_AXIS);
        scroll = new JScrollPane(panel);
        dime5 = new Dimension(500, 150);
        panelConstraints = new GridBagConstraints();
        scroll.setPreferredSize(dime5);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel.setLayout(bx);
        panel.add(Box.createHorizontalBox());
        panel.setBorder(LineBorder.createBlackLineBorder());
        panel.setBackground(new Color(204, 230, 255));
        jButton11.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == jButton11) {
            label = new JLabel("Add Designation " + count + " :-");
            jTextFields = new JTextField(30);
            panel1 = new JPanel();
            panel1.setBackground(new Color(204, 230, 255));
            panel1.add(label);
            panel1.add(jTextFields);
            panel.add(panel1);
            panel.revalidate();
            panel.repaint();
            count++;
            i++;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MakeScrollablePanel();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM