Я хочу, чтобы мой класс MCQuestions расширял класс Вопросы, но также использовался в качестве JPanel - PullRequest
0 голосов
/ 13 января 2019

Я делаю свою собственную программу-тестер. Я хочу добавить в свой JFrame форму, расширяющую другой класс. У меня есть класс jPanel с именем MCQuestion, который я хочу поместить в JFrame с именем Test, однако JFrame не позволяет мне перетаскивать класс MCQuestions внутри него. Я хочу, чтобы мой класс MCQuestions расширял класс Вопросы, но также использовался в качестве JPanel.

Класс испытаний JFrame:

package questions;

import answers.MCAnswer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author billc
 */
public class Test extends javax.swing.JFrame {

    /**
     * Creates new form Test
     */
    public Test() {
        initComponents();
        ArrayList<String> choices = new ArrayList<String>();
        choices.add("yes");
        choices.add("sure");
        MCQuestion mc = new MCQuestion("What is...?",null, 4, new MCAnswer(2),choices);
        this.add(mc);


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {



        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}


Класс MCQuestions:

package questions;

import answers.MCAnswer;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JRadioButton;

/**
 *
 * @author billc
 */
public class MCQuestion extends Question {

    /**
     * Creates new form MCQuestion
     */
    public List<String> options;

    public MCQuestion(String question, Object resource, int marks, MCAnswer answer, List<String> options) {
        super(question, resource, marks, answer);
        this.options = options;
        initComponents();
        buttonGroup1 = new javax.swing.ButtonGroup();
            LayoutManager H = new GridLayout(1,0);
            this.setLayout(H);


    for (int i = 0; i < options.size(); i++) {
            JRadioButton temp = new JRadioButton(options.get(i));
            buttonGroup1.add(temp);
            this.setLayout(H);
            this.validate();
    }


        System.out.println(buttonGroup1.getButtonCount());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    // End of variables declaration                   
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...