java - 我将如何在单击时动态地将摆动组件添加到 gui?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:19:12  来源:igfitidea点击:

java - How would I dynamically add swing component to gui on click?

javauser-interfaceswing

提问by slex

What I am looking to do is a similar principle to adding attachments to emails, you can click a button and a new browse box would open increasing the number of separate attachments you can have.

我想要做的是类似于向电子邮件添加附件的原则,您可以单击一个按钮,一个新的浏览框将打开,增加您可以拥有的单独附件的数量。

I'm fairly new so if someone could point me towards an example?

我是新手,所以如果有人可以指出我的例子吗?

采纳答案by Jigar Joshi

Sample code to add Buttons on the fly dynamically.

动态添加按钮的示例代码。

panel.add(new JButton("Button"));
validate();

Full code:

完整代码:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class AddComponentOnJFrameAtRuntime extends JFrame implements ActionListener {

    JPanel panel;

    public AddComponentOnJFrameAtRuntime() {
        super("Add component on JFrame at runtime");
        setLayout(new BorderLayout());
        this.panel = new JPanel();
        this.panel.setLayout(new FlowLayout());
        add(panel, BorderLayout.CENTER);
        JButton button = new JButton("CLICK HERE");
        add(button, BorderLayout.SOUTH);
        button.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
        this.panel.add(new JButton("Button"));
        this.panel.revalidate();
        validate();
    }

    public static void main(String[] args) {
        AddComponentOnJFrameAtRuntime acojfar = new AddComponentOnJFrameAtRuntime();
    }
}

回答by dacwe

public static void main(String[] args) {

    final JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(0, 1));

    frame.add(new JButton(new AbstractAction("Click to add") {
        @Override
        public void actionPerformed(ActionEvent e) {

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    frame.add(new JLabel("Bla"));
                    frame.validate();
                    frame.repaint();
                }
            });
        }
    }));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

screenshot

截屏

回答by Vidura Adikari

Component was not visible until setSize()was called:

组件在setSize()被调用之前不可见:

component.setSize(100,200);
jPanel.add(component);
jPanel.revalidate();
jPanel.repaint(); 

回答by Raj Trivedi

panel.add(button);

面板添加(按钮);

panel.revalidate();

panel.revalidate();

panel.repaint();

panel.repaint();

回答by Chetan Bhagat

Java : Dynamically add swing components

Java:动态添加swing组件

for Example : count=3
//Java Swing: Add Component above method
public void  dya_addcomp(int count)
{
//Dynamicaly Delete Image_icon
 BufferedImage Drop_Tablefield = null;
 try {
     Drop_Tablefield = ImageIO.read(this.getClass().getResource("/images/drop.png"));
 } catch (IOException ex) {
     msg(" Error: drop and edit icon on Table, "+ex);
 }
 //count Items:  3 times for loop executed..
 for(int i=0;i<count;i++)
 {
     //cnt++;
     //lblcount.setText("Count : "+cnt);
     JTextField txtcolnm=new JTextField("",20);
     JComboBox cmbtype=new JComboBox();
     JTextField txtcolsize=new JTextField("",20);

     JButton Drop_Table_Field = new JButton(new ImageIcon(Drop_Tablefield));

     cmbtype.addItem("INTEGER"); cmbtype.addItem("FLOAT");
     cmbtype.addItem("STRING");  cmbtype.addItem("BOOLEAN");

     colnamepanel.add(txtcolnm);   colnamepanel.add(cmbtype);
     colnamepanel.add(txtcolsize); colnamepanel.add(Drop_Table_Field);

     colnamepanel.setAutoscrolls(true);

     //refresh panel
     colnamepanel.revalidate();
     colnamepanel.repaint();

     //set the layout on Jpanel
     colnamepanel.setLayout(new FlowLayout(FlowLayout.LEFT,5,0));
  }//end for loop
 }//end method