从JFrame向每个JPanel(如果需要)添加Scrollbar

时间:2012-01-10 09:26:04

标签: java swing jframe jpanel jscrollbar

//这是我的JFrame代码,然后连接到其他Panel。

    public class startQuiz extends JFrame {

        private static final long serialVersionUID = 1L;
        private JPanel jContentPane = null;
        private JButton jButtonGQuiz = null;
        private JFrame myFrame = null;
        private JLabel jLabelTitle = null;
        private JLabel jLabelGQuiz = null;
        private JLabel jLabelCQuiz = null;
        private JButton jButtonCQuiz = null;
        private JMenuBar jJMenuBar = null;
        private JMenu jMenuFile = null;
        private JMenuItem jMenuItemNew = null;
        private JMenuItem jMenuItemImport = null;
        private JScrollPane jScrollPane = null;
        /**
         * This method initializes jButtonGQuiz 
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonGQuiz() {
            if (jButtonGQuiz == null) {
                jButtonGQuiz = new JButton();
                jButtonGQuiz.setBounds(new Rectangle(345, 90, 75, 26));
                jButtonGQuiz.setText("Start");
                jButtonGQuiz.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        startButtonClicked();
                    }
                    public void startButtonClicked() {
                        JPanel panel = new GQuiz(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();
                    }
                });
            }
            return jButtonGQuiz;
        }

        /**
         * This method initializes jButtonCQuiz 
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonCQuiz() {
            if (jButtonCQuiz == null) {
                jButtonCQuiz = new JButton();
                jButtonCQuiz.setBounds(new Rectangle(344, 140, 75, 26));
                jButtonCQuiz.setText("Start");
                jButtonCQuiz.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        startButton1Clicked();
                    }
                    public void startButton1Clicked() {
                        JPanel panel = new CancerQuiz(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();

                    }
                });
            }
            return jButtonCQuiz;
        }

        /**
         * This method initializes jJMenuBar    
         *  
         * @return javax.swing.JMenuBar 
         */
        private JMenuBar getJJMenuBar() {
            if (jJMenuBar == null) {
                jJMenuBar = new JMenuBar();
                jJMenuBar.add(getJMenuFile());
            }
            return jJMenuBar;
        }

        /**
         * This method initializes jMenuFile    
         *  
         * @return javax.swing.JMenu    
         */
        private JMenu getJMenuFile() {
            if (jMenuFile == null) {
                jMenuFile = new JMenu();
                jMenuFile.setText("File");
                jMenuFile.add(getJMenuItemNew());
                jMenuFile.add(getJMenuItemImport());
            }
            return jMenuFile;
        }

        /**
         * This method initializes jMenuItemNew 
         *  
         * @return javax.swing.JMenuItem    
         */
        private JMenuItem getJMenuItemNew() {
            if (jMenuItemNew == null) {
                jMenuItemNew = new JMenuItem();
                jMenuItemNew.setText("New");
                jMenuItemNew.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        newMenuItemClicked();
                    }
                    public void newMenuItemClicked() {
                        JPanel panel = new MyTestDatePicker(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();
                    }
                });
            }
            return jMenuItemNew;
        }

        /**
         * This method initializes jMenuItemImport  
         *  
         * @return javax.swing.JMenuItem    
         */
        private JMenuItem getJMenuItemImport() {
            if (jMenuItemImport == null) {
                jMenuItemImport = new JMenuItem();
                jMenuItemImport.setText("Import");
                jMenuItemImport.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        importMenuItemClicked();
                    }
                    public void importMenuItemClicked() {
                        JPanel panel = new importQuestions(myFrame);
                        myFrame.getContentPane().removeAll();
                        myFrame.getContentPane().add(panel);
                        myFrame.getContentPane().validate();
                        myFrame.getContentPane().repaint();

                    }
                });
            }
            return jMenuItemImport;
        }

        /**
         * This method initializes jScrollPane  
         *  
         * @return javax.swing.JScrollPane  
         */


        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    startQuiz thisClass = new startQuiz();
                    thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    thisClass.setVisible(true);

                }
            });
        }

        /**
         * This is the default constructor
         */
        public startQuiz() {
            super();
            initialize();
            myFrame = this;
            this.setVisible(true);
        }

        /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
            this.setSize(680, 320);
            this.setJMenuBar(getJJMenuBar());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setContentPane(getJContentPane());
            this.setTitle("Quiz");
            this.setVisible(true);


        }

        /**
         * This method initializes jContentPane
         * 
         * @return javax.swing.JPanel
         */
        private JPanel getJContentPane() {
            if (jContentPane == null) {
                jLabelCQuiz = new JLabel();
                jLabelCQuiz.setHorizontalAlignment(SwingConstants.RIGHT);
                jLabelCQuiz.setLocation(new Point(64, 134));
                jLabelCQuiz.setSize(new Dimension(251, 38));
                jLabelCQuiz.setText("Cancer Quiz : ");
                jLabelGQuiz = new JLabel();
                jLabelGQuiz.setBounds(new Rectangle(66, 83, 251, 38));
                jLabelGQuiz.setHorizontalAlignment(SwingConstants.RIGHT);
                jLabelGQuiz.setText("General Quiz :");
                jLabelTitle = new JLabel();
                jLabelTitle.setBounds(new Rectangle(73, 8, 488, 46));
                jLabelTitle.setFont(new Font("Dialog", Font.BOLD, 18));
                jLabelTitle.setHorizontalAlignment(SwingConstants.CENTER);
                jLabelTitle.setText("Choose a Quiz !");
                jContentPane = new JPanel();  
                jContentPane.setLayout(null);
                jContentPane.setBorder(null);
                jContentPane.add(getJButtonGQuiz(), null);
                jContentPane.add(jLabelTitle, null);
                jContentPane.add(jLabelGQuiz, null);
                jContentPane.add(jLabelCQuiz, null);
                jContentPane.add(getJButtonCQuiz(), null);

            }
            return jContentPane;
        }

    }

//这是我之前链接到之前JFrame的面板之一。我在这里添加了滚动条//但它无效。因此,如何使滚动条正常工作。

    import java.awt.GridBagLayout;
    import javax.swing.JPanel;
    import javax.swing.JScrollBar;
    import java.awt.Rectangle;
    import java.awt.Dimension;

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    import javax.swing.JButton;

    import myQuiz.entity.QuizQuestion;
    import javax.swing.SwingConstants;
    import javax.swing.JScrollPane;

    public class GQuiz extends JPanel {

        private static final long serialVersionUID = 1L;
        private JLabel jLabelG1 = null;
        private JRadioButton jRadioButtonTrue = null;
        private JRadioButton jRadioButtonFalse = null;
        private JLabel jLabelG2 = null;
        private JRadioButton jRadioButtonTrue1 = null;
        private JRadioButton jRadioButtonFalse1 = null;
        private JButton jButtonSubmit = null;
        private JFrame myFrame = null;

        private int questionID;
        private String username;
        private String answer;
        /**
         * This is the default constructor
         */
        public GQuiz() {
            super();
            initialize();
        }
        public GQuiz (JFrame f){
            this();
            myFrame = f;
            }

        /**
         * This method initializes this
         * 
         * @return void
         */
        private void initialize() {
            jLabelG2 = new JLabel();
            jLabelG2.setBounds(new Rectangle(5, 104, 724, 37));
            jLabelG2.setText("2. Exercise is a good way to help prevent heart attacks.");
            jLabelG1 = new JLabel();
            jLabelG1.setBounds(new Rectangle(6, 3, 719, 32));
            jLabelG1.setHorizontalAlignment(SwingConstants.LEFT);
            jLabelG1.setText("1. Drinking two glasses of water before meals is a good way to help fill your stomach up which makes you eat less.");
            this.setSize(733, 359);
            this.setLayout(null);
            this.add(jLabelG1, null);
            this.add(getJRadioButtonTrue(), null);
            this.add(getJRadioButtonFalse(), null);
            this.add(jLabelG2, null);
            this.add(getJRadioButtonTrue1(), null);
            this.add(getJRadioButtonFalse1(), null);
            this.add(getJButtonSubmit(), null);
        }

        /**
         * This method initializes jRadioButtonTrue 
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonTrue() {
            if (jRadioButtonTrue == null) {
                jRadioButtonTrue = new JRadioButton();
                jRadioButtonTrue.setBounds(new Rectangle(7, 39, 77, 26));
                jRadioButtonTrue.setText("True");
            }
            return jRadioButtonTrue;
        }

        /**
         * This method initializes jRadioButtonFalse    
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonFalse() {
            if (jRadioButtonFalse == null) {
                jRadioButtonFalse = new JRadioButton();
                jRadioButtonFalse.setBounds(new Rectangle(8, 70, 74, 30));
                jRadioButtonFalse.setText("False");
            }
            return jRadioButtonFalse;
        }

        /**
         * This method initializes jRadioButtonTrue1    
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonTrue1() {
            if (jRadioButtonTrue1 == null) {
                jRadioButtonTrue1 = new JRadioButton();
                jRadioButtonTrue1.setBounds(new Rectangle(6, 145, 75, 30));
                jRadioButtonTrue1.setText("True");
            }
            return jRadioButtonTrue1;
        }

        /**
         * This method initializes jRadioButtonFalse1   
         *  
         * @return javax.swing.JRadioButton 
         */
        private JRadioButton getJRadioButtonFalse1() {
            if (jRadioButtonFalse1 == null) {
                jRadioButtonFalse1 = new JRadioButton();
                jRadioButtonFalse1.setBounds(new Rectangle(6, 177, 77, 31));
                jRadioButtonFalse1.setText("False");
            }
            return jRadioButtonFalse1;
        }

        /**
         * This method initializes jButtonSubmit    
         *  
         * @return javax.swing.JButton  
         */
        private JButton getJButtonSubmit() {
            if (jButtonSubmit == null) {
                jButtonSubmit = new JButton();
                jButtonSubmit.setBounds(new Rectangle(284, 287, 83, 32));
                jButtonSubmit.setText("Submit");


            }
            return jButtonSubmit;
        }

    }  

2 个答案:

答案 0 :(得分:1)

我没有看到你在面板中使用JScrollPane,但基本上这可能不是正确的地方(取决于你想做什么)。

通常,您将滚动窗格添加到框架而不是面板,并将面板放入滚动窗格。这就像这个伪代码:

 JFrame frame = new JFrame();
 JPanel panel = new JPanel();
 JScrollPane scroll = new JScrollPane( panel );
 frame.add( scroll);

答案 1 :(得分:1)

如果您想要为每个添加到JFrame的JPanel添加它,那么您可以执行以下操作:

  1. 创建一个扩展JFrame类的类
  2. 覆盖JFrame的add(component)方法
  3. 在此方法中,检查参数组件是否为JPanel。
  4. 如果JPanel然后创建一个JScrollPane,则向其添加jPanel,然后将scrollPane添加到frame。
  5. 请致电super.add(component);