java swing动态添加组件 [英] java swing dynamically adding components

查看:874
本文介绍了java swing动态添加组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢Java Swing。我有一些怀疑在Swing中动态组件。



基本上我有一个主要 JPanel 由两个子 JPanel (leftpanel和rightpanel),它水平排列。在左边 JPanel 我有一些 JButtons ,当我点击 JButton 我确定显示一些 JLabel JTextArea 等在右边的 JPanel 。我尝试了一个代码,但它不工作。当我点击按钮,它进入事件侦听器功能,但 JLabel 我无法查看。



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

 包我的; 

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(系统管理员);
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);
}
});
}
}


解决方案

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

  rightPanel 。新增(标签); 
rightPanel.revalidate();

应该做的伎俩。


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

Basically I hav one Main JPanel consisting of two sub JPanel (leftpanel and rightpanel ) which alligned horizontally.In left JPanel I hav some JButtons, when I will click on JButton I nedd to show some JLabel, JTextArea etc 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 JLabel I am not able to view.

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);
                }
            });
        }
    }

解决方案

You need to call revalidate after adding (or removing) components:

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

should do the trick.

这篇关于java swing动态添加组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆