浏览带有卡片布局的小程序(需要一些指导和代码修复)

时间:2016-03-24 21:09:07

标签: java swing applet jframe cardlayout

首先,我想说,就我的java知识而言,我是一个初学者。所以,我正在开发一个applet,使用Card Layout可以有不同的页面。我设法从在线资源中收集了一些代码,但是,我正在努力完成一些事情。我会把它分成几个问题。

  1. 如何使按钮按下时,用户将被带到指定的卡而不是下一张卡?
  2. 在“主要”课程中,如何设置我想要的卡片的可见性,而不是列表中的第一张?
  3. 在页面类(Menu,FirstPage等)中,如何只使用一种按钮处理方法而不必重复相同的代码?
  4. 如果我在其中有绘画方法,我的页面类是否会起作用?
  5. 如何将此应用程序转换为applet?目前它是JFrame。
  6. 如果有人能帮助我,我真的很感激。

    主要课程

     public class Main
    {
        private Menu menu;
        private FirstPage page1;
        private SecondPage page2;
        private ThirdPage page3;
        private FourthPage page4;
    
        private void showGUI()
        {
            JFrame frame = new JFrame();
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new CardLayout());
            contentPane.setPreferredSize(new Dimension(240,600));
    
            menu = new Menu(contentPane);
            page1 = new FirstPage(contentPane);
            page2 = new SecondPage(contentPane);
            page3 = new ThirdPage(contentPane);
            page4 = new FourthPage(contentPane);
    
            contentPane.add(menu, "Menu"); 
            contentPane.add(page1, "First page");
            contentPane.add(page2, "Second page");
            contentPane.add(page3, "Third page");
            contentPane.add(page4, "Fourth page");
    
            frame.setContentPane(contentPane);      
            frame.pack();   
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Main().showGUI();
                }
            });
        }
    }
    

    菜单页面类

    class Menu extends javax.swing.JPanel 
    {
        private javax.swing.JLabel textLabel;
        private JPanel contentPane;
        private JButton pageOne;
        private JButton pageTwo;
        private JButton pageThree;
        private JButton pageFour;
    
        public Menu(JPanel cp) 
        {
            this.contentPane = cp;
              initComponents();
        }
    
        private void initComponents() 
        {
            setBackground(Color.WHITE);
            setLayout(null);
    
            textLabel = new JLabel("This is the menu page", JLabel.CENTER);
            textLabel.setForeground(Color.BLACK);
    
            pageOne = new JButton("Flowers");
            pageTwo = new JButton("Trees");
            pageThree = new JButton("Rivers");
            pageFour = new JButton("Seas");
    
    
            pageOne.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageOneAction(ae); } });
            pageTwo.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
            pageThree.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
            pageFour.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
            textLabel.setBounds(20, 20, 200, 60);
    
            pageOne.setBounds(20, 100, 200, 60);
            pageTwo.setBounds(20, 170, 200, 60);
            pageThree.setBounds(20, 240, 200, 60);
            pageFour.setBounds(20, 310, 200, 60);
    
            add(pageOne);
            add(pageTwo);
            add(pageThree);
            add(pageFour);
            add(textLabel);
        }                   
    
        private void pageOneAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageTwoAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageThreeAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageFourAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
    }
    

    第一页课

    class FirstPage extends javax.swing.JPanel 
    {
        private javax.swing.JLabel textLabel;
        private JPanel contentPane;
        private JButton menu;
        private JButton pageTwo;
        private JButton pageThree;
        private JButton pageFour;
    
        public FirstPage(JPanel cp) 
        {
            this.contentPane = cp;
            this.setSize(500, 500);
            initComponents();
        }
    
        private void initComponents() 
        {
            setBackground(Color.WHITE);
            setLayout(null);
    
            textLabel = new JLabel("This is the flowers page", JLabel.CENTER);
            textLabel.setForeground(Color.BLACK);
    
            menu = new JButton("Back to Menu");
            pageTwo = new JButton("Trees");
            pageThree = new JButton("Rivers");
            pageFour = new JButton("Seas");
    
    
            menu.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { menuAction(ae); } });
            pageTwo.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageTwoAction(ae); } });
            pageThree.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageThreeAction(ae); } });
            pageFour.addActionListener(new ActionListener(){ @Override  public void actionPerformed(ActionEvent ae) { pageFourAction(ae); } });
    
            textLabel.setBounds(20, 20, 200, 60);
            menu.setBounds(20, 100, 200, 60);
            pageTwo.setBounds(20, 170, 200, 60);
            pageThree.setBounds(20, 240, 200, 60);
            pageFour.setBounds(20, 310, 200, 60);
    
            add(menu);
            add(pageTwo);
            add(pageThree);
            add(pageFour);
            add(textLabel);
        }                   
    
        private void menuAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageTwoAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageThreeAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
        private void pageFourAction(ActionEvent ae)
        {
            CardLayout layout = (CardLayout)contentPane.getLayout();
            layout.next(contentPane); 
        }
    }
    

    其他页面与上面的页面相同。

0 个答案:

没有答案