Java 将JDialog转换为Jframe/JPanel
所以我有下面的代码来制作一个测试程序。我使用的代码扩展了JDialog。我想知道是否有办法将其转换为JPanel,因为当我尝试运行代码时,窗口都很小,我似乎无法将完成的按钮移动到“下一步”按钮旁边。如果我想让问题变得随机,人们会怎么做 多谢各位Java 将JDialog转换为Jframe/JPanel,java,swing,jframe,jpanel,jdialog,Java,Swing,Jframe,Jpanel,Jdialog,所以我有下面的代码来制作一个测试程序。我使用的代码扩展了JDialog。我想知道是否有办法将其转换为JPanel,因为当我尝试运行代码时,窗口都很小,我似乎无法将完成的按钮移动到“下一步”按钮旁边。如果我想让问题变得随机,人们会怎么做 多谢各位 import javax.swing.*; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.util.Random; import javax.swing.JR
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.JRadioButton;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultipleChoice extends JDialog {
int correctAns;
List<Question> questions = new ArrayList<Question>();
//asnwers
JRadioButton[] responses;
ButtonGroup group = new ButtonGroup();
//bottom
JButton next = new JButton("Next");
JButton finish = new JButton("Finish");
public MultipleChoice(){
super();
setModal(true);
setLayout( new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); //setting up boxlayout
questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead"));
questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver"));
questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360"));
questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange"));
questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2"));
questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny"));
questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True"));
//bottom
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
}
});
finish.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
}
});
}
public int beginQuiz(){
int score=0;
for (Question q : questions){
displayQuestion(q);
if (group.getSelection().getActionCommand().equals(q.getans())){
score++;
}
}
dispose();
return score;
}
private void displayQuestion(Question q){
getContentPane().removeAll();
for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){
group.remove((AbstractButton)buttonGroup.nextElement());
}
JLabel questionText = new JLabel(q.getquestion());
getContentPane().add(questionText);
for (String answer : q.getanswers()){
JRadioButton radio = new JRadioButton(answer);
radio.setActionCommand(answer);
group.add(radio);
getContentPane().add(radio);
}
getContentPane().add(next);
getContentPane().add(finish);
pack();
setVisible(true);
}
public static void showSummary(int score){
JOptionPane.showMessageDialog(null,"All Done :), here are your results"+
"\nNumber of incorrect Answers: \t"+(7-score)+
"\nNumber of Correct Answers: \t"+(score)+
"\nPercent Correct: \t\t"+(int)(((float)(score)/7)*100)+"%"
);
if (((int)(((float)(score)/7)*100))> 0.6)
JOptionPane.showMessageDialog(null,"You Passed!");
else {
JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader");
}
}
public static void main(String[] args){
MultipleChoice quiz = new MultipleChoice();
int score = quiz.beginQuiz();
showSummary(score);
}
}
class Question {
private String question;
private String[] answers;
private String ans;
String getquestion(){ //getter
return question;
}
void setquestion(String str){ //setter
question = str;
}
String[] getanswers(){ //getter
return answers;
}
void setanswers(String[] str2){ //setter
answers = str2;
}
String getans(){ //getter
return ans;
}
void setans(String str3){ //setter
ans = str3;
}
public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){ //constructor
question = possibleQuestion;
answers = possibleAnswer;
ans = correctAnswer;
}
}
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import java.awt.CardLayout;
import javax.swing.JRadioButton;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultipleChoice {
int correctAns;
List<Question> questions = new ArrayList<Question>();
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int wrongs=0;
int total=0;
public static void main(String[] args){
new MultipleChoice();
}
public MultipleChoice(){
JFrame frame = new JFrame("Are you Smarter than a 5th Grader?");
frame.setResizable(true);
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead"));
questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver"));
questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360"));
questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange"));
questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2"));
questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny"));
questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True"));
p.setLayout(cards);
numQs=questions.size();
for(int i=0;i<numQs;i++){
p.add(questions[i],"q"+i);
}
Random r=new Random();
int i=r.nextInt(numQs);
cards.show(p,"q"+i);
frame.add(p);
frame.setVisible(true);
}
public void next(){
if((total-wrongs)==numQs){
showSummary();
}else{
Random r=new Random();
boolean found=false;
int i=0;
while(!found){
i=r.nextInt(numQs);
if(!questions[i].used){
found=true;
}
}
cards.show(p,"q"+i);
}
}
public void showSummary(){
JOptionPane.showMessageDialog(null,"All Done :), here are your results"+
"\nNumber of incorrect Answers: \t"+wrongs+
"\nNumber of Correct Answers: \t"+(total-wrongs)+
"\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%"
);
if (((int)(((float)(total-wrongs)/total)*100))> 0.6)
JOptionPane.showMessageDialog(null,"You Passed!");
else {
JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader");
}
System.exit(0);
}
}
class Question extends JPanel {
int correctAns;
MultipleChoice Choices;
int selected;
boolean used;
//questions
JPanel qPanel=new JPanel();
//answers
JPanel aPanel=new JPanel();
JRadioButton[] responses;
//bottom
JPanel botPanel=new JPanel();
JButton next=new JButton("Next");
JButton finish=new JButton("Finish");
private String question;
private String[] answers;
private String ans;
public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){ //constructor
this.Choices=Choices;
question = possibleQuestion;
answers = possibleAnswer;
ans = correctAnswer;
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//question
qPanel.add(new JLabel(question));
add(qPanel);
//answer
responses=new JRadioButton[answers.length];
//display possible answer and answers
aPanel.add(responses[i]);
add(aPanel);
//bottom
next.addActionListener(this);
finish.addActionListener(this);
botPanel.add(next);
botPanel.add(finish);
add(botPanel);
}
public Question(String possibleQuestion ,String[] possibleAnswer){ //constructor overloading
question = possibleQuestion;
answers = possibleAnswer;
}
String getquestion(){ //getter
return question;
}
void setquestion(String str){ //setter
question = str;
}
String[] getanswers(){ //getter
return answers;
}
void setanswers(String[] str2){ //setter
answers = str2;
}
String getans(){ //getter
return ans;
}
void setans(String str3){ //setter
ans = str3;
}
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
//next button
if(src.equals(next)){
if(selected==correctAns){
used=true;
Choices.next();
}
}
//finish button
if(src.equals(finish)){
Choices.showSummary();
}
}
}
import javax.swing.*;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入java.util.Random;
导入javax.swing.JRadioButton;
导入java.util.ArrayList;
导入java.util.Enumeration;
导入java.util.List;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类多路复用扩展JDialog{
内校正;
列出问题=新建ArrayList();
//阿斯沃斯
JRadioButton[]响应;
ButtonGroup=新建ButtonGroup();
//底部
JButton next=新JButton(“next”);
JButton finish=新JButton(“finish”);
公共多重切迹(){
超级();
setModal(真);
setLayout(新建BoxLayout(getContentPane(),BoxLayout.Y_轴));//设置BoxLayout
问题。添加(新问题(“以下哪一个不是五大湖之一?”,新字符串[]{“米德湖”、“休伦湖”、“密歇根湖”、“伊利湖”},“米德湖”);
问题。添加(新问题(“科罗拉多州的首府是什么?”,新字符串[]{“博尔德”、“阿斯彭”、“丹佛”、“夏延”},“丹佛”);
问题。添加(新问题(“棒球钻石的每一面都有90英尺长。棒球钻石周围有多远?”,新字符串[]{“270”、“360”、“180”、“390”},“360”);
添加(新问题(“当你将等量的红色颜料和等量的黄色颜料混合在一起时,你会得到什么颜色?”,新字符串[]{“蓝色”、“橙色”、“绿色”、“粉色”},“橙色”);
添加(新问题(“梯形有多少边?”,新字符串[]{“3”,“4”,“5”,“6”},“2”));
问题。添加(新问题(“哪一个是明喻?”,新字符串[]{“我妹妹像兔子一样可爱”,“我妈妈有男子气概的鼾声”,“我爸爸比你好”},“我妹妹像兔子一样可爱”);
添加(新问题(“北极熊吃潘昆?”,新字符串[]{“真”,“假”},“真”);
//底部
next.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
setVisible(假);
}
});
finish.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
setVisible(假);
}
});
}
public int beginquirz(){
智力得分=0;
关于(问题q:问题){
问题(q);
if(group.getSelection().getActionCommand().equals(q.getans())){
分数++;
}
}
处置();
返回分数;
}
私人问题(问题q){
getContentPane().removeAll();
对于(枚举buttonGroup=group.getElements();buttonGroup.hasMoreElements();){
移除((AbstractButton)按钮group.nextElement());
}
JLabel questionText=新的JLabel(q.getquestion());
getContentPane().add(问题文本);
for(字符串答案:q.getanswers()){
JRadioButton radio=新的JRadioButton(应答);
radio.setActionCommand(应答);
添加组(收音机);
getContentPane().add(收音机);
}
getContentPane().add(下一步);
getContentPane()。添加(完成);
包装();
setVisible(真);
}
公共静态无效显示摘要(整数分数){
showMessageDialog(null,“全部完成:),以下是您的结果”+
“\n错误答案的数量:\t”+(7分)+
“\n正确答案的数目:\t”+(分数)+
“\n正确率:\t\t”+(整型)(浮动)(分数)/7)*100+”%
);
如果((整型)((浮动)(分数)/7)*100))>0.6)
showMessageDialog(null,“您通过了!”);
否则{
showMessageDialog(null,“你不比五年级学生聪明”);
}
}
公共静态void main(字符串[]args){
多回音测验=新的多回音测验();
int score=quick.beginquirz();
展示总结(得分);
}
}
课堂提问{
私有字符串问题;
私有字符串[]答案;
私有字符串ans;
字符串getquestion(){//getter
返回问题;
}
void setquestion(字符串str){//setter
问题=str;
}
字符串[]getanswers(){//getter
返回答案;
}
void setanswers(字符串[]str2){//setter
答案=str2;
}
字符串getans(){//getter
返回ans;
}
void setans(字符串str3){//setter
ans=str3;
}
公共问题(字符串possibleQuestion,字符串[]possibleAnswer,字符串correctAnswer){//构造函数
问题=可能的问题;
答案=可能答案;
ans=正确答案;
}
}
使用新编辑进行更新 在我编辑并尝试将其转换为JPanel并使用cardlayout(如建议的)后,我遇到了一些错误,似乎无法找到如何让字符串[]可能答案和字符串正确答案显示出来,以便系统知道并匹配正确答案。这是我的更新代码 多谢各位
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.JRadioButton;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultipleChoice extends JDialog {
int correctAns;
List<Question> questions = new ArrayList<Question>();
//asnwers
JRadioButton[] responses;
ButtonGroup group = new ButtonGroup();
//bottom
JButton next = new JButton("Next");
JButton finish = new JButton("Finish");
public MultipleChoice(){
super();
setModal(true);
setLayout( new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); //setting up boxlayout
questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead"));
questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver"));
questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360"));
questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange"));
questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2"));
questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny"));
questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True"));
//bottom
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
}
});
finish.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
}
});
}
public int beginQuiz(){
int score=0;
for (Question q : questions){
displayQuestion(q);
if (group.getSelection().getActionCommand().equals(q.getans())){
score++;
}
}
dispose();
return score;
}
private void displayQuestion(Question q){
getContentPane().removeAll();
for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){
group.remove((AbstractButton)buttonGroup.nextElement());
}
JLabel questionText = new JLabel(q.getquestion());
getContentPane().add(questionText);
for (String answer : q.getanswers()){
JRadioButton radio = new JRadioButton(answer);
radio.setActionCommand(answer);
group.add(radio);
getContentPane().add(radio);
}
getContentPane().add(next);
getContentPane().add(finish);
pack();
setVisible(true);
}
public static void showSummary(int score){
JOptionPane.showMessageDialog(null,"All Done :), here are your results"+
"\nNumber of incorrect Answers: \t"+(7-score)+
"\nNumber of Correct Answers: \t"+(score)+
"\nPercent Correct: \t\t"+(int)(((float)(score)/7)*100)+"%"
);
if (((int)(((float)(score)/7)*100))> 0.6)
JOptionPane.showMessageDialog(null,"You Passed!");
else {
JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader");
}
}
public static void main(String[] args){
MultipleChoice quiz = new MultipleChoice();
int score = quiz.beginQuiz();
showSummary(score);
}
}
class Question {
private String question;
private String[] answers;
private String ans;
String getquestion(){ //getter
return question;
}
void setquestion(String str){ //setter
question = str;
}
String[] getanswers(){ //getter
return answers;
}
void setanswers(String[] str2){ //setter
answers = str2;
}
String getans(){ //getter
return ans;
}
void setans(String str3){ //setter
ans = str3;
}
public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){ //constructor
question = possibleQuestion;
answers = possibleAnswer;
ans = correctAnswer;
}
}
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Random;
import java.awt.CardLayout;
import javax.swing.JRadioButton;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MultipleChoice {
int correctAns;
List<Question> questions = new ArrayList<Question>();
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int wrongs=0;
int total=0;
public static void main(String[] args){
new MultipleChoice();
}
public MultipleChoice(){
JFrame frame = new JFrame("Are you Smarter than a 5th Grader?");
frame.setResizable(true);
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
questions.add(new Question("Which of the following is NOT one of the 5 great lakes?",new String[]{"Lake Mead","Lake Huron","Lake Michigan","Lake Erie"}, "Lake Mead"));
questions.add(new Question("What is the Capital of Colorado?",new String[]{"Boulder","Aspen","Denver","Cheyenne"},"Denver"));
questions.add(new Question("Each side of a baseball diamond is 90 feet in length. How far is it around the Baseball Diamond?",new String[]{"270","360","180","390"},"360"));
questions.add(new Question("What color do you get when you combine an equal amount of red paint with an equal amount of yellow paint?",new String[]{"Blue","Orange","Green","Pink"},"Orange"));
questions.add(new Question("How many sides does a trapezoid have?",new String[]{"3","4","5","6"},"2"));
questions.add(new Question("Which is a simile?",new String[]{"My sister is cute like a bunny","My mom has a manly snore","My dad is better than yours"},"My sister is cute like a bunny"));
questions.add(new Question("Polar bears eat penquins?",new String[]{"True","False"},"True"));
p.setLayout(cards);
numQs=questions.size();
for(int i=0;i<numQs;i++){
p.add(questions[i],"q"+i);
}
Random r=new Random();
int i=r.nextInt(numQs);
cards.show(p,"q"+i);
frame.add(p);
frame.setVisible(true);
}
public void next(){
if((total-wrongs)==numQs){
showSummary();
}else{
Random r=new Random();
boolean found=false;
int i=0;
while(!found){
i=r.nextInt(numQs);
if(!questions[i].used){
found=true;
}
}
cards.show(p,"q"+i);
}
}
public void showSummary(){
JOptionPane.showMessageDialog(null,"All Done :), here are your results"+
"\nNumber of incorrect Answers: \t"+wrongs+
"\nNumber of Correct Answers: \t"+(total-wrongs)+
"\nPercent Correct: \t\t"+(int)(((float)(total-wrongs)/total)*100)+"%"
);
if (((int)(((float)(total-wrongs)/total)*100))> 0.6)
JOptionPane.showMessageDialog(null,"You Passed!");
else {
JOptionPane.showMessageDialog(null,"You are NOT Smarter than a 5th Grader");
}
System.exit(0);
}
}
class Question extends JPanel {
int correctAns;
MultipleChoice Choices;
int selected;
boolean used;
//questions
JPanel qPanel=new JPanel();
//answers
JPanel aPanel=new JPanel();
JRadioButton[] responses;
//bottom
JPanel botPanel=new JPanel();
JButton next=new JButton("Next");
JButton finish=new JButton("Finish");
private String question;
private String[] answers;
private String ans;
public Question(String possibleQuestion ,String[] possibleAnswer , String correctAnswer){ //constructor
this.Choices=Choices;
question = possibleQuestion;
answers = possibleAnswer;
ans = correctAnswer;
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//question
qPanel.add(new JLabel(question));
add(qPanel);
//answer
responses=new JRadioButton[answers.length];
//display possible answer and answers
aPanel.add(responses[i]);
add(aPanel);
//bottom
next.addActionListener(this);
finish.addActionListener(this);
botPanel.add(next);
botPanel.add(finish);
add(botPanel);
}
public Question(String possibleQuestion ,String[] possibleAnswer){ //constructor overloading
question = possibleQuestion;
answers = possibleAnswer;
}
String getquestion(){ //getter
return question;
}
void setquestion(String str){ //setter
question = str;
}
String[] getanswers(){ //getter
return answers;
}
void setanswers(String[] str2){ //setter
answers = str2;
}
String getans(){ //getter
return ans;
}
void setans(String str3){ //setter
ans = str3;
}
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
//next button
if(src.equals(next)){
if(selected==correctAns){
used=true;
Choices.next();
}
}
//finish button
if(src.equals(finish)){
Choices.showSummary();
}
}
}
import javax.swing.*;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入java.util.Random;
导入java.awt.CardLayout;
导入javax.swing.JRadioButton;
导入java.util.ArrayList;
导入java.util.Enumeration;
导入java.util.List;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类多路复用{
内校正;
列出问题=新建ArrayList();
JPanel p=新的JPanel();
CardLayout卡=新的CardLayout();
int numQs;
int错误=0;
int-total=0;
公共静态void main(字符串[]args){
新的多重回声();
}
公共多重切迹(){
JFrame=newJFrame(“你比五年级学生聪明吗?”);
frame.setresizeable(true);
框架。设置尺寸(500400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
问题。添加(新问题(“以下哪项不是5个gr中的一个