遇到一个很棘手的问题
  java gui编程中  的问题  
   
    1:很多按钮
    2: 主界面
  我想做的效果是  根据在1处按的按钮改变2号面板中的元素(把2号面板换成另一个面板)
  现在的问题是 2号的面板不改变..?

解决方案 »

  1.   

    // 自定义的初始化方法(需要初始自己定义的属性)
    Private void myinit(){
    // MyDrawPanel 我自己做到面板类
    jpAllSpace = new MyDrawPanel();} //frame窗口的初始化方法
    private void initComponents(){

    loginPanel = new javax.swing.JPanel();
    jlUserName = new javax.swing.JLabel();
    jlPasswd = new javax.swing.JLabel();
    jbtnLogin = new javax.swing.JButton();
    很多初始化代码…
    }//点击按钮调用的方法
    private void jbtnCancelActionPerformed(java.awt.event.ActionEvent evt){
    jpAllSpace = new JPanel();
    this.repaint();
    }
    方法调用
    窗体初始化调用myinit()(窗体类的构造方法中调用)  >> initComponents  (窗体类的构造方法中调用) >>显示窗体然后我在按钮事件中改边jpAllSpace值 再 repaint()  结果没改变jpAllSpace内容
      

  2.   

    这个可以使用cardlayout,2出放一个JPanel用来盛放其他JPanel,并把这个JPanel设成cardlayout。1中按钮的事件响应调用CardLayout的show方法来显示相应的JPanel。
      

  3.   


    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class TextGui extends JFrame { public static void main(String[] args) {
    final Color[] color ={Color.RED,Color.BLACK,Color.green,Color.YELLOW};
    JFrame frame = new JFrame("你好");
    JButton button = new JButton("确定");
    JPanel j = new JPanel();
    final JPanel j2 = new JPanel();
    j.add(button);
    Container container = frame.getContentPane();
    container.setLayout(new FlowLayout());
    container.add(j);
    container.add(j2);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(100,100);
    button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
    j2.setBackground(color[0]);
    }

    });
    }
    }你看一下是不是你要的结果??
      

  4.   

    你可以用那个选项卡啊! TabbedPane
    例子:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class TabbedPane extends JFrame{
        JTabbedPane tab;
        JPanel jp1,jp2,jp3;
        JButton b1,b2,b3;
        JLabel lb1,lb2,lb3;
        JTextField jf1,jf2,jf3;
        public TabbedPane()
        {
          super("选项卡");
          Container c=getContentPane();
          jp1=new JPanel();
          jp2=new JPanel();
          jp3=new JPanel();
          b1=new JButton("新窗口");
          b1.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e)
           {
           if(e.getSource()==b1)
           {
         Frame f=new Frame();
         f.setBounds(300,300,300,300);
         f.setBackground(Color.green);
         f.show();
           }
           }
          });
          b2=new JButton("蓝色");
          b3=new JButton("按钮3");
          lb1=new JLabel("选项卡1");
          lb2=new JLabel("选项卡2");
          lb3=new JLabel("选项卡3");
          jf1=new JTextField(15);
          jf2=new JTextField(15);
          jf3=new JTextField(15);
          //向面板添加组件
          jp1.add(b1);jp1.add(lb1);jp1.add(jf1);
          jp2.add(b2);jp2.add(lb2);jp2.add(jf2);
          jp3.add(b3);jp3.add(lb3);jp3.add(jf3);
          tab=new JTabbedPane();//nullpoint没有指针指向
          //向tab添加面板
          tab.addTab("选项卡1",jp1);
          tab.addTab("选项卡2",jp2);
          tab.addTab("选项卡3",jp3);
          //向容器添加tab
          c.add(tab);
        }
        
      public static void main(String[] args) {
      TabbedPane t=new TabbedPane();
       t.setSize(300,300);
       t.setVisible(true);
           t.setDefaultCloseOperation(TabbedPane.EXIT_ON_CLOSE);
    }}
      

  5.   

    可以用cardLayout
     1  import java.awt.*;
     2  import java.awt.event.*;
     3  import javax.swing.*;
     4
     5  public class ShowCardLayout extends JApplet {
     6    private CardLayout cardLayout = new CardLayout(20, 10);
     7    private JPanel cardPanel = new JPanel(cardLayout) ;
     8    private JButton jbtFirst, jbtNext, jbtPrevious, jbtLast;
     9    private JComboBox jcboImage;
    10    private final int NUM_OF_FLAGS = 6;
    11  
    12    public ShowCardLayout() {
    13      cardPanel.setBorder(
    14        new javax.swing.border.LineBorder(Color.red));
    15
    16      // Add 9 labels for displaying images into cardPanel
    17      for (int i = 1; i <= NUM_OF_FLAGS; i++) {
    18        JLabel label =
    19          new JLabel(new ImageIcon("image/flag" + i + ".gif"));
    20        cardPanel.add(label, String.valueOf(i));
    21      }
    22
    23      // Panel p to hold buttons and a combo box
    24      JPanel p = new JPanel();
    25      p.add(jbtFirst = new JButton("First"));
    26      p.add(jbtNext = new JButton("Next"));
    27      p.add(jbtPrevious= new JButton("Previous"));
    28      p.add(jbtLast = new JButton("Last"));
    29      p.add(new JLabel("Image"));
    30      p.add(jcboImage = new JComboBox());
    31
    32      // Initialize combo box items
    33      for (int i = 1; i <= NUM_OF_FLAGS; i++)
    34        jcboImage.addItem(String.valueOf(i));
    35
    36      // Place panels in the frame
    37      add(cardPanel, BorderLayout.CENTER);
    38      add(p, BorderLayout.SOUTH);
    39
    40      // Register listeners with the source objects
    41      jbtFirst.addActionListener(new ActionListener() {
    42        public void actionPerformed(ActionEvent e) {
    43          // Show the first component in cardPanel
    44          cardLayout.first(cardPanel);
    45        }
    46      });
    47      jbtNext.addActionListener(new ActionListener() {
    48        public void actionPerformed(ActionEvent e) {
    49          // Show the first component in cardPanel
    50          cardLayout.next(cardPanel);
    51        }
    52      });
    53      jbtPrevious.addActionListener(new ActionListener() {
    54        public void actionPerformed(ActionEvent e) {
    55          // Show the first component in cardPanel
    56          cardLayout.previous(cardPanel);
    57        }
    58      });
    59      jbtLast.addActionListener(new ActionListener() { 
    60        public void actionPerformed(ActionEvent e) {
    61          // Show the first component in cardPanel
    62          cardLayout.last(cardPanel);
    63        }
    64      });
    65      jcboImage.addItemListener(new ItemListener() { 
    66        public void itemStateChanged(ItemEvent e) {
    67          // Show the component at specified index
    68          cardLayout.show(cardPanel, (String)e.getItem()); 
    69        }
    70      });
    71    }
    72  }
      

  6.   

    cardlayout 一次性把所有的panel创建出来
     我现在是根据选择的不同而出现特定的panel(临时行的新建对象再显示)要是用tablelayout 的话如果打开多到一定的程度就会布满整个面板我想在按了按钮后再创建新的对象把原来的panel换了 再重绘下整个frame 可是效果没出来...