连接每个JPanel超类(包括JRadioButtons)

时间:2012-01-11 05:47:00

标签: eclipse swing radio-button superclass visual-editor

//这是与每个超类javax.swing.JPanel连接的JFrame(请注意我使用的是Java Eclipse Visual Editor,我有8个超类javax.swing.JPanel)

public class startQuiz extends JFrame {

private static final long serialVersionUID = 1L;
private JScrollPane 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 TestPanel(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 JScrollPane 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 JScrollPane();  
        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;
}

}

//这是我的超类javax.swing.JPanel

之一
public class CQuestion7 extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel jLabelCQn7 = null;
private JRadioButton jRadioButton1 = null;
private JRadioButton jRadioButton2 = null;
private JButton jButtonBack = null;
private JButton jButtonNext = null;
private JFrame myFrame = null;

/**
 * This is the default constructor
 */
public CQuestion7() {
    super();
    initialize();
}
public CQuestion7(JFrame f) {
    this();
    myFrame = f;
}

/**
 * This method initializes this
 * 
 * @return void
 */
private void initialize() {
    jLabelCQn7 = new JLabel();

    jLabelCQn7.setBounds(new Rectangle(5, 4, 647, 38));
    jLabelCQn7.setText("7. Have you ever worked with anything invovled dangerous chemical without adequate protection? ");
    this.setSize(661, 291);
    this.setLayout(null);
    this.add(jLabelCQn7, null);
    this.add(getJRadioButton1(), null);
    this.add(getJRadioButton2(), null);
    this.add(getJButtonBack(), null);
    this.add(getJButtonNext(), null);
}

/**
 * This method initializes jRadioButton1
 *  
 * @return javax.swing.JRadioButton 
 */

ButtonGroup group = new ButtonGroup(); 
JRadioButton aRadioButton;

private JRadioButton getJRadioButton1() {
    if (jRadioButton1 == null) {
        jRadioButton1 = new JRadioButton();
        jRadioButton1.setText("Yes");
        jRadioButton1.setLocation(new Point(8, 48));
        jRadioButton1.setSize(new Dimension(92, 39));
        group.add(jRadioButton1);
    }
    return jRadioButtonYes;
}

/**
 * This method initializes jRadioButton2    
 *  
 * @return javax.swing.JRadioButton
 */
private JRadioButton getJRadioButton2() {
    if (jRadioButton2 == null) {
        jRadioButton2 = new JRadioButton();
        jRadioButton2.setText("No");
        jRadioButton2.setLocation(new Point(8, 80));
        jRadioButton2.setSize(new Dimension(95, 39));
        group.add(jRadioButton2);
    }
    return jRadioButton2;
}

/**
 * This method initializes jButtonBack  
 *  
 * @return javax.swing.JButton  
 */
private JButton getJButtonBack() {
    if (jButtonBack == null) {
        jButtonBack = new JButton();
        jButtonBack.setText("Back");
        jButtonBack.setLocation(new Point(230, 197));
        jButtonBack.setSize(new Dimension(79, 28));
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                backButtonClicked();
            }
            public void backButtonClicked() {
                JPanel panel = new CQuestion6(myFrame);
                myFrame.getContentPane().removeAll();
                myFrame.getContentPane().add(panel);
                myFrame.getContentPane().validate();
                myFrame.getContentPane().repaint();
            }
        });
    }
    return jButtonBack;
}

/**
 * This method initializes jButtonNext  
 *  
 * @return javax.swing.JButton  
 */
private JButton getJButtonNext() {
    if (jButtonNext == null) {
        jButtonNext = new JButton();
        jButtonNext.setText("Next");
        jButtonNext.setLocation(new Point(320, 198));
        jButtonNext.setSize(new Dimension(79, 28));
        jButtonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                nextButtonClicked();
            }
            public void nextButtonClicked() {
                JPanel panel = new CQuestion8(myFrame);
                myFrame.getContentPane().removeAll();
                myFrame.getContentPane().add(panel);
                myFrame.getContentPane().validate();
                myFrame.getContentPane().repaint();
            }
        });
    }
    return jButtonNext;
}

}

//这是我的最后一个超类javax.swing.JPanel,里面有一个提交按钮。单击提交按钮时,我想从每个超类中获取所有选定的单选按钮并记录在我的数据库中。

public class CQuestion8 extends JPanel {

    private static final long serialVersionUID = 1L;
    private JLabel jLabelCQn8 = null;
    private JRadioButton jRadioButton1 = null;
    private JRadioButton jRadioButton2 = null;
    private JButton jButtonBack = null;
    private JButton jButtonSubmit = null;
    private JFrame myFrame = null;

    private String username;
    private int questionID;
    private String answer;

    /**
     * This is the default constructor
     */
    public CQuestion8() {
        super();
        initialize();
    }
    public CQuestion8(JFrame f) {
        this();
        myFrame = f;
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        jLabelCQn8 = new JLabel();
        jLabelCQn8.setBounds(new Rectangle(3, 2, 656, 44));
        jLabelCQn8.setText("8. Do you eat 5 or more servings of fruit and vegetables per day?");
        this.setSize(661, 291);
        this.setLayout(null);
        this.add(jLabelCQn8, null);
        this.add(getJRadioButton1(), null);
        this.add(getJRadioButton2(), null);
        this.add(getJButtonBack(), null);
        this.add(getJButtonSubmit(), null);
    }

    /**
     * This method initializes jRadioButton1
     *  
     * @return javax.swing.JRadioButton 
     */

    ButtonGroup group = new ButtonGroup(); 
    JRadioButton aRadioButton;

    private JRadioButton getJRadioButton1() {
        if (jRadioButton1 == null) {
            jRadioButton1 = new JRadioButton();
            jRadioButton1.setText("Yes");
            jRadioButton1.setLocation(new Point(8, 48));
            jRadioButton1.setSize(new Dimension(100, 32));
            group.add(jRadioButton1);
        }
        return jRadioButton1;
    }

    /**
     * This method initializes jRadioButton2    
     *  
     * @return javax.swing.JRadioButton 
     */
    private JRadioButton getJRadioButton2() {
        if (jRadioButton2 == null) {
            jRadioButton2 = new JRadioButton();
            jRadioButton2.setText("No");
            jRadioButton2.setLocation(new Point(8, 80));
            jRadioButton2.setSize(new Dimension(103, 32));
            group.add(jRadioButton2);
        }
        return jRadioButton2;
    }

    /**
     * This method initializes jButtonBack  
     *  
     * @return javax.swing.JButton  
     */
    private JButton getJButtonBack() {
        if (jButtonBack == null) {
            jButtonBack = new JButton();
            jButtonBack.setText("Back");
            jButtonBack.setLocation(new Point(230, 197));
            jButtonBack.setSize(new Dimension(79, 28));
            jButtonBack.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    backButtonClicked();
                }
                public void backButtonClicked() {
                    JPanel panel = new CQuestion7(myFrame);
                    myFrame.getContentPane().removeAll();
                    myFrame.getContentPane().add(panel);
                    myFrame.getContentPane().validate();
                    myFrame.getContentPane().repaint();
                }
            });
        }
        return jButtonBack;
    }

    /**
     * This method initializes jButtonSubmit    
     *  
     * @return javax.swing.JButton  
     */
    private JButton getJButtonSubmit() {
        if (jButtonSubmit == null) {
            jButtonSubmit = new JButton();
            jButtonSubmit.setText("Submit");
            jButtonSubmit.setLocation(new Point(328, 198));
            jButtonSubmit.setSize(new Dimension(79, 28));
            jButtonSubmit.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    submitButtonClicked();
                }
                public void submitButtonClicked(){
                    username = "John";
                    for (int i=1; i<9; i++) {
                        questionID = i;
                        if(jRadioButton1.isSelected()){answer = jRadioButton1.getText();}
                        else if(jRadioButton2.isSelected()){answer = jRadioButton2.getText();}
                        QuizAnswer q = new QuizAnswer(username,questionID,answer);
                        q.createQuizAnswer();}
                }
            });
        }
        return jButtonSubmit;
    }

}

0 个答案:

没有答案