java swing动态添加组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6516478/
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-16 06:33:18  来源:igfitidea点击:

java swing dynamically adding components

javaswingcomponentsdynamic

提问by nantitv

I am new to Java Swing. I have some doubt regarding adiing components dynamically in Swing.

我是 Java Swing 的新手。我对 Swing 中的动态组件有一些疑问。

Basically I hav one Main JPanelconsisting of two sub JPanel(leftpanel and rightpanel ) which alligned horizontally.In left JPanelI hav some JButtons, when I will click on JButtonI nedd to show some JLabel, JTextAreaetc in right JPanel. I tried a code but its not working .When I click on the button its going inside the event listener function but JLabelI am not able to view.

基本上我甲肝一个主要JPanel由两个子JPanel(leftpanel和rightpanel)的神韵:horizo​​ntally.In离开JPanel我甲肝一些JButtons,我会点击JButton我NEDD表现出一定的JLabelJTextArea等右JPanel。我尝试了一个代码,但它不起作用。当我点击按钮时,它会进入事件侦听器函数,但JLabel我无法查看。

I am giving my code below. Pls look at this and correct me. thanks in advance

我在下面给出我的代码。请看看这个并纠正我。提前致谢

package my;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;

    /**
     *
     * @author root
     */

    public class myAplliwithPanel extends JFrame{

        JPanel rightPanel;

        public myAplliwithPanel() {
             initGui();
        }        

        public void initGui()
        {
           JPanel mainPanel=new JPanel();
           mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

           JPanel leftPanel=new JPanel();
           leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

            rightPanel=new JPanel();
           rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));

           JButton dbBut=new JButton("DB");
           JButton appliBut=new JButton("Appli");
           appliBut.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    JLabel label=new JLabel("dsggs");
                   rightPanel.add(label);
                }
            });

           JButton backendBut=new JButton("Backend");

           leftPanel.add(dbBut);
           leftPanel.add(appliBut);
           leftPanel.add(backendBut);    

           mainPanel.add(leftPanel);
           mainPanel.add(rightPanel);

           add(mainPanel);

            setTitle("System Manger");
            setSize(400, 400);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);


        }

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

                public void run() {
                    myAplliwithPanel myObj = new myAplliwithPanel();
                    myObj.setVisible(true);
                }
            });
        }
    }

回答by Cameron Skinner

You need to call revalidateafter adding (or removing) components:

revalidate添加(或删除)组件后需要调用:

rightPanel.add(label);
rightPanel.revalidate();

should do the trick.

应该做的伎俩。

回答by StanislavL

call

称呼

rightPanel.revalidate();
rightPanel.repaint();

after adding

添加后

回答by Pratik

just add this line after you add the label

添加标签后只需添加此行

rightPanel.updateUI();

rightPanel.updateUI();

when you add any component at runtime you need to update the ui using this method

当您在运行时添加任何组件时,您需要使用此方法更新 ui