//これはJFrameのコードで、他のパネルに接続されています。JFrameから各JPanelへのスクロールバーの追加(必要な場合)
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にリンクされている私のパネルの1つです。 //ここにスクロールバーを追加しましたが、動作しません。したがって、スクロールバーを正しく動作させるにはどうすればよいですか。
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;
}
}
を呼び出し、[SSCCE](http://sscce.org/)を投稿してください。たいていの場合、40〜50行程度のコードしか必要ではないことに注意してください。 –