package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
public class Fram {
public static void main(String[] args) {
        JFrame jf = new JFrame();
      // jf.setDefaultCloseOperation(operation)
        final Container conPan = jf.getContentPane();       
       JPanel jp1 = new JPanel();
       jp1.setBackground( Color.black);
       jp1.setLayout(new FlowLayout());
       JButton jb1 = new JButton("铵钮1");
       JButton jb2 = new JButton("铵钮1");
       JButton jb3 = new JButton("铵钮1");    
       jb3.setPreferredSize(new Dimension(20,19));
      // jb3.setSize(100, 150);
       //jp1.setSize(200, 300);
       jp1.setPreferredSize(new Dimension(200,189));
       jp1.add(jb1);
       jp1.add(jb2);
       jp1.add(jb3);       
       final JPanel jp2 = new JPanel();
       jp2.setBackground(Color.blue);
       final JPanel jp3 = new JPanel();
       jp3.setBackground(Color.gray);       
       conPan.setLayout(new BorderLayout());
       conPan.add(jp1,"North");
       //conPan.add(jp2,"West");
      // conPan.add(jp3,"Center");      
       conPan.add(jp3,"Center");    
       conPan.add(jp2,"Center");
       conPan.add(jp2,"Center");// 这里是可以的.     
       
     //  final JPanel jp4 = new JPanel();
     //  jp4.setBackground(Color.green);
     //  JPanel jp5 = new JPanel();
     //  jp5.setBackground(Color.orange);
    //   jp2.setLayout(new BorderLayout());
      // jp2.add(jp4,"North");
      // jp2.add(jp5,"Center");
       
       jb1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent Event){
        System.out.println('a');
        conPan.add(jp3,"Center");//~~~~疑问就在这里,怎么这句不起作用呢? 要怎么写呢?
        //conPan.repaint();
      // jf.repaint();
       // jp2.setVisible(true);
       // conPan.repaint();
        //conPan.setVisible(true);
        
        //jp2.add(jp4,"Center");
       // jp2.setVisible(true);
        //jp4.setVisible(true);
       // jp2.repaint();
        
        }
       });
       jf.setSize(600, 500);
       jf.setVisible(true);
}}

解决方案 »

  1.   

           jb1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent Event){
                   System.out.println('a');
                   conPan.add(jp3,"Center");//~~~~疑问就在这里,怎么这句不起作用呢? 要怎么写呢?
                   conPan.validate(); // 加这一句使用布局生效
                   //conPan.repaint();
                 // jf.repaint();
                  // jp2.setVisible(true);
                  // conPan.repaint();
                   //conPan.setVisible(true);
                   
                   //jp2.add(jp4,"Center");
                  // jp2.setVisible(true);
                   //jp4.setVisible(true);
                  // jp2.repaint();
                   
               }
           });
      

  2.   

    你的代码太乱。具体实现思路可以参考。import java.awt.FlowLayout;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.border.TitledBorder;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;
    public class Test {
     JFrame jf = new JFrame();
     JPanel jp3 = new JPanel();

        public static void main(String[] args) {
         Test t=new Test();
         JFrame jf=t.jf;
          // jf.setDefaultCloseOperation(operation)
            Container conPan = jf.getContentPane();       
           JPanel jp1 = new JPanel();
           jp1.setBackground( Color.black);
           jp1.setLayout(new FlowLayout());
           JButton jb1 = new JButton("铵钮1");
           JButton jb2 = new JButton("铵钮1");
           JButton jb3 = new JButton("铵钮1");    
           jb3.setPreferredSize(new Dimension(20,19));
          // jb3.setSize(100, 150);
           //jp1.setSize(200, 300);
           jp1.setPreferredSize(new Dimension(200,189));
           jp1.add(jb1);
           jp1.add(jb2);
           jp1.add(jb3);       
           final JPanel jp2 = new JPanel();
           jp2.setBackground(Color.blue);
           
           t.jp3.setBackground(Color.gray);       
           conPan.setLayout(new BorderLayout());
           conPan.add(jp1,"North");
           //conPan.add(jp2,"West");
          // conPan.add(jp3,"Center");      
           conPan.add(t.jp3,"Center");    
           conPan.add(jp2,"Center");
           conPan.add(jp2,"Center");// 这里是可以的.     
           
         //  final JPanel jp4 = new JPanel();
         //  jp4.setBackground(Color.green);
         //  JPanel jp5 = new JPanel();
         //  jp5.setBackground(Color.orange);
        //   jp2.setLayout(new BorderLayout());
          // jp2.add(jp4,"North");
          // jp2.add(jp5,"Center");
           jb1.addActionListener(new ActionListerSimple());
           
    //           public void actionPerformed(ActionEvent Event){
    //               System.out.println('a');
    //
    //               conPan.add(jp3,"Center");//~~~~疑问就在这里,怎么这句不起作用呢? 要怎么写呢?
    //               //conPan.repaint();
    //             // jf.repaint();
    //              // jp2.setVisible(true);
    //              // conPan.repaint();
    //               //conPan.setVisible(true);
    //               
    //               //jp2.add(jp4,"Center");
    //              // jp2.setVisible(true);
    //               //jp4.setVisible(true);
    //              // jp2.repaint();
    //               
    //           }
           
           jf.setSize(600, 500);
           jf.setVisible(true);
        }
    }
    class ActionListerSimple implements ActionListener{
    private JFrame jf =this.jf;
    private JPanel jp3=this.jp3;
    public void actionPerformed(ActionEvent e) {
      System.out.println('a');
              jf.add(jp3,"Center");
    }

    }
      

  3.   


    多谢两位... 这个good.. 就是要这种效果.