用於測驗的代碼是在一個名爲tviewSetvisible()將不起作用。錯誤的位置:QUIZIQ
package tview.quiz;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
class Quiz extends JFrame implements ActionListener{
JPanel panel;
JPanel panelresult;
JRadioButton choice1;
JRadioButton choice2;
JRadioButton choice3;
JRadioButton choice4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
String[][] qpa;
String[][] qca;
int qaid;
HashMap<Integer, String> map;
Quiz(){
initializedata();
setTitle("Quiz Program");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(430,350);
setLocation(300,100);
setResizable(false);
Container cont=getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
bg=new ButtonGroup();
choice1=new JRadioButton("Choice1",true);
choice2=new JRadioButton("Choice2",false);
choice3=new JRadioButton("Choice3",false);
choice4=new JRadioButton("Choice4",false);
bg.add(choice1);
bg.add(choice2);
bg.add(choice3);
bg.add(choice4);
lblmess=new JLabel("Choose a correct anwswer");
lblmess.setForeground(Color.BLUE);
lblmess.setFont(new Font("Arial", Font.BOLD, 11));
btnext=new JButton("Next");
btnext.setForeground(Color.GREEN);
btnext.addActionListener(this);
panel=new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLocation(10,10);
panel.setSize(400,300);
panel.setLayout(new GridLayout(6,2));
panel.add(lblmess);
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);
panel.add(choice4);
panel.add(btnext);
cont.add(panel);
setVisible(true);
qaid=0;
readqa(qaid);
}
public void actionPerformed(ActionEvent e){
if(btnext.getText().equals("Next")){
if(qaid<9){
map.put(qaid,getSelection());
qaid++;
readqa(qaid);
}
else {
map.put(qaid,getSelection());
btnext.setText("Show answers");
}
}
else if(btnext.getText().equals("Show answers"))
new Report();
}
public void initializedata(){
//qpa stores pairs of question and its possible answers
qpa=new String[10][5];
qpa[0][0]="Test";
qpa[0][1]="A";
qpa[0][2]="B";
qpa[0][3]="C";
qpa[0][4]="D";
//qca stores pairs of question and its correct answer
qca=new String[10][2];
//create a map object to store pairs of question and selected answer
map=new HashMap<Integer, String>();
}
public String getSelection(){
String selectedChoice=null;
Enumeration<AbstractButton> buttons=bg.getElements();
while(buttons.hasMoreElements())
{
JRadioButton temp=(JRadioButton)buttons.nextElement();
if(temp.isSelected())
{
selectedChoice=temp.getText();
}
}
return(selectedChoice);
}
public void readqa(int qid){
lblmess.setText(" "+qpa[qid][0]);
choice1.setText(qpa[qid][1]);
choice2.setText(qpa[qid][2]);
choice3.setText(qpa[qid][3]);
choice4.setText(qpa[qid][4]);
choice1.setSelected(true);
}
public void reset(){
qaid=0;
map.clear();
readqa(qaid);
btnext.setText("Next");
}
public int calCorrectAnswer(){
int qnum=10;
int count=0;
for(int qid=0;qid<qnum;qid++)
if(qca[qid][1].equals(map.get(qid))) count++;
return count;
}
public class Report extends JFrame{
Report(){
setTitle("Answers");
setSize(850,550);
setBackground(Color.WHITE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
reset();
}
});
Draw d=new Draw();
add(d);
setVisible(true);
}
class Draw extends Canvas{
public void paint(Graphics g){
int qnum=10;
int x=10;
int y=20;
for(int i=0;i<qnum;i++){
//print the 1st column
g.setFont(new Font("Arial",Font.BOLD,12));
g.drawString(i+1+". "+qca[i][0], x,y);
y+=30;
g.setFont(new Font("Arial",Font.PLAIN,12));
g.drawString(" Correct answer:"+qca[i][1], x,y);
y+=30;
g.drawString(" Your answer:"+map.get(i), x,y);
y+=30;
//print the 2nd column
if(y>400)
{y=20;
x=450;
}
}
//Show number of correct answers
int numc=calCorrectAnswer();
g.setColor(Color.BLUE);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Number of correct answers:"+numc,300,500);
}
}
}
}
public class QuizIQ{
public static void main(String args[]){
Quiz qz = new Quiz();
JFrame frame = new JFrame();
frame.setSize(300, 300); // Set the size of the window
frame.add(qz);
frame.setVisible(true);
}
}
接着一個的JavaSource包我有一個按鈕被點擊時,我的主包內調用接口的JFrame應該顯示競猜型的變量QZ 。
private void quizBttnActionPerformed(java.awt.event.ActionEvent evt)
{
new QuizIQ().setVisible(true);
}
我不斷收到,說找不到符號錯誤, 符號:方法調用setVisible(布爾) 位置:類型的變量QZ QUIZIQ
可能有人請幫助我這個錯誤出來嗎?
Java GUI必須使用不同語言環境中使用不同PLAF的不同OS,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –
1)爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)使用合乎邏輯的一致形式縮進代碼行和塊。縮進旨在使代碼的流程更易於遵循! 3)源代碼中的單個空白行是需要的。 '{'之後或'}'之前的空行通常也是多餘的。 4)請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/418556) –