我生成好了2个JTable,怎么让他们在界面上交替显示(按下按钮)?有知道的吗?麻烦告诉一声,谢谢。

解决方案 »

  1.   

    按下按钮后触发事件,在事件里写一个JTable显示(show),另外一个JTable隐藏(hide).
      

  2.   

    import java.awt.*;
    import java.awt.event.*;/**
     * Sample application using Frame.
     *
     * @author 
     * @version 1.00 07/07/14
     */
    public class CardlayoutFrame extends Frame {
        
        /**
         * The constructor.
         */  
    Panel pl = new Panel();
          CardLayout cl = new CardLayout();
         public CardlayoutFrame() {
          pl.setLayout(cl);
            Button b1 = new Button("1");
            Button b2 = new Button("2");
            pl.add(b1,"1");
            pl.add(b2,"2");
            
            class MyActionListener implements ActionListener
            {
             public void actionPerformed(ActionEvent e)
             {
             cl.next(pl);
             }
            }
            
            MyActionListener mal = new MyActionListener();
            b1.addActionListener(mal);
            b2.addActionListener(mal);
            add(pl);                
            MenuBar menuBar = new MenuBar();
            Menu menuFile = new Menu();
            MenuItem menuFileExit = new MenuItem();
            
            menuFile.setLabel("File");
            menuFileExit.setLabel("Exit");
            
            // Add action listener.for the menu button
            menuFileExit.addActionListener
            (
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        CardlayoutFrame.this.windowClosed();
                    }
                }
            ); 
            menuFile.add(menuFileExit);
            menuBar.add(menuFile);
            
            setTitle("Cardlayout");
            setMenuBar(menuBar);
            setSize(new Dimension(400, 400));
            
            // Add window listener.
            this.addWindowListener
            (
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        CardlayoutFrame.this.windowClosed();
                    }
                }
            );  
        }
        
        
        /**
         * Shutdown procedure when run as an application.
         */
        protected void windowClosed() {
        
         // TODO: Check if it is safe to close the application
        
            // Exit application.
            System.exit(0);
        }
    }
    /**
     * AWT Sample application
     *
     * @author 
     * @version 1.00 07/07/14
     */
    public class Cardlayout {
        
        public static void main(String[] args) {
            // Create application frame.
            CardlayoutFrame frame = new CardlayoutFrame();
            
            // Show frame
            frame.setVisible(true);
        }
    }