你可以用pl.updateUI()来更新。
Swing是no-thread-safe的
因此,只能在event-dispatching-thread中对Swing Component进行操作

解决方案 »

  1.   

    如果你的p1.add(new JLabel("aaa"));是在frame.setVisible(true);之后才添加的,那么你需要先使p1作废,然后再重新布局就可以显示出新的内容。
    p1.invalidate();
    p1.validate();
    <------ 树欲静而风不止 ------>
      

  2.   

    repaint()
    invalidate();
    updateUI();
    换着试试.
      

  3.   

    在一个JFrame中调用这个dialog,注意,basicPane已经加了东西,但由于tabPane加basicPane在前,加basicNorthPane在后,所以没有正确显示
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.*;/**
     * Created by IntelliJ IDEA.
     * User: purples
     * Date: 2003-12-23
     * Time: 16:42:59
     * To change this template use Options | File Templates.
     */
    //main panel show in NodeModifyDlg to modify node,or it could use to instead node add panel
    public class NodeModifyDlg extends JDialog implements ActionListener{    public void actionPerformed(ActionEvent event){
            String command = event.getActionCommand();
            if(command.equals("close")){
                this.dispose();
                if(DEBUG == true){
                    System.out.println("close");
                }
            }
        }    public NodeModifyDlg(Frame frame, boolean model, boolean isAdd){
            super(frame, model);
            init(true, isAdd);
        }
        //default is model,not node add dialog
        public NodeModifyDlg(Frame frame){
            super(frame, true);
            init(false, false);
        }    public NodeModifyDlg(Dialog dialog, boolean model, boolean isAdd){
            super(dialog, model);
            init(false, isAdd);
        }    public NodeModifyDlg(Dialog dialog){
            super(dialog,true);
            init(false, false);
        }    protected void init(boolean debug, boolean isAdd){
            DEBUG = debug;
            this.isAdd = isAdd;
            jbinit();
        }    //init the GUI
        private void jbinit(){
            Container c = this.getContentPane();
            //datePane draw
            datePane = new JPanel();
            datePane.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
            timeLabel = new JLabel("Register Date:"+new java.util.Date());   //todo: need getData() to replace
            updateLabel = new JLabel("Update Date:");//need getUpdateData() to add
            datePane.add(timeLabel);
            datePane.add(updateLabel);
            //datePane finished
                     //tabPane draw
            tabPane = new JTabbedPane();
            tabPane.addTab("Basic info", basicPane);
            tabPane.addTab("Control info", controlPane);
            //button draw
            nodeOk = new JButton("Ok");
            nodeClose = new JButton("Close");
            nodeOk.setActionCommand("ok");
            nodeClose.setActionCommand("close");
            nodeOk.addActionListener(this);
            nodeClose.addActionListener(this);
            buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
            buttonPane.add(nodeOk);
            buttonPane.add(nodeClose);
            //button draw finished        c.setLayout(new BorderLayout(10,10));
            if(isAdd == false){
                setTitle("Node Modify");
                c.add(BorderLayout.NORTH, datePane);
            }
            else{
                setTitle("Node Add");
            }
            makeTabPane();
    //        tabPane.invalidate();
    //        tabPane.validate();
           
            c.add(BorderLayout.CENTER,tabPane);
    //        basicNorthPane.invalidate();
    //        basicNorthPane.validate();
     //       basicNorthPane.repaint();
    //        tabPane.updateUI();
            c.add(BorderLayout.SOUTH, buttonPane);
        }    private void makeTabPane(){
            basicPaneInit();
            controlPaneInit();
        }    private void basicPaneInit(){
            basicPane = new JPanel();
            basicNorthPane = new JPanel();
            basicNorthPane.setLayout(new FlowLayout(FlowLayout.LEFT));
            locationLabel = new JLabel("Location");
            locationComboBox = new JComboBox();
            //todo: add item to comboBox here
            basicNorthPane.add(locationLabel);
            basicNorthPane.add(locationComboBox);        basicPane.setLayout(new BorderLayout());
            basicPane.add( BorderLayout.NORTH, basicNorthPane);    }    private void controlPaneInit(){
            controlPane = new JPanel();
        }    public void setDEBUG(boolean DEBUG) {
            this.DEBUG = DEBUG;
        }    private boolean isAdd;
        private JPanel datePane;
        private JLabel timeLabel;
        private JLabel updateLabel;
        private JTabbedPane tabPane;
        private JPanel basicPane;
        private JPanel controlPane;    private JPanel basicNorthPane;
        private JPanel basicCenterPane;
        private JPanel basicEastPane;
        private JPanel basciSouthPane;    private JLabel locationLabel;
        private JComboBox locationComboBox;    private JPanel buttonPane;
        private JButton nodeOk;
        private JButton nodeClose;
        private boolean DEBUG;
    }