public class Quiz {
private static void homePage() {
JFrame homePage = new JFrame("QUIZ");
Container c = homePage.getContentPane();
homePage.setVisible(true);
homePage.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
homePage.setBounds(600,180,700,400);
homePage.setResizable(false);
homePage.setContentPane(c);
JLabel benvenuto = new JLabel("Benvenuto!");
benvenuto.setFont(new Font("Serif", Font.BOLD, 30));
benvenuto.setBounds(265,0,180,120);
benvenuto.setOpaque(true);
JLabel label1 = new JLabel("Inserisci il tuo nome:");
label1.setBounds(215,100,120,100);
label1.setOpaque(true);
JTextArea areaNome = new JTextArea();
areaNome.setEditable(true);
areaNome.setFont(new Font("Serif", Font.BOLD, 16));
areaNome.setBounds(345,142,120,20);
javax.swing.border.Border borderP = BorderFactory.createLineBorder(Color.BLACK);
areaNome.setBorder(borderP);
JLabel labelStarting = new JLabel();
labelStarting.setBounds(230,230,280,120);
labelStarting.setForeground(Color.RED);
JButton startButton = new JButton("Inizia");
startButton.setBounds(280,200,120,50);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String gt1 = areaNome.getText();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(Quiz.class.getName()).log(Level.SEVERE, null, ex);
}
labelStarting.setText("Preparati "+gt1+", il gioco sta per iniziare.");
JFrame quizPage = new JFrame("QUIZ");
quizPage.setBounds(573,150,750,450);
quizPage.setVisible(true);
quizPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
quizPage.setResizable(false);
Container c2 = quizPage.getContentPane();
quizPage.setContentPane(c2);
JLabel domanda1 = new JLabel("1) Qual è la capitale d'Italia?");
domanda1.setBounds(0,0,280,120);
JLabel domanda2 = new JLabel("2) Quanto fa 2^8?");
domanda2.setBounds(0,55,280,120);
JLabel domanda3 = new JLabel("3) Quando è stata scoperta l'America da Colombo?");
domanda3.setBounds(0,110,380,120);
JLabel domanda4 = new JLabel("4) Dove si trova l'Empire State Building?");
domanda4.setBounds(0,165,280,120);
JLabel domanda5 = new JLabel("5) Qual è il nome dell'autore della Divina Commedia?");
domanda5.setBounds(0,220,380,120);
JLabel labelNull2 = new JLabel();
labelNull2.setBounds(230,230,280,120);
JTextArea r1 = new JTextArea();
r1.setBounds(320,50,120,20);
javax.swing.border.Border border1 = BorderFactory.createLineBorder(Color.BLACK);
r1.setBorder(border1);
JTextArea r2 = new JTextArea();
r2.setBounds(320,105,120,20);
javax.swing.border.Border border2 = BorderFactory.createLineBorder(Color.BLACK);
r2.setBorder(border2);
JTextArea r3 = new JTextArea();
r3.setBounds(320,160,120,20);
javax.swing.border.Border border3 = BorderFactory.createLineBorder(Color.BLACK);
r3.setBorder(border3);
JTextArea r4 = new JTextArea();
r4.setBounds(320,217,120,20);
javax.swing.border.Border border4 = BorderFactory.createLineBorder(Color.BLACK);
r4.setBorder(border4);
JTextArea r5 = new JTextArea();
r5.setBounds(320,270,120,20);
javax.swing.border.Border border5 = BorderFactory.createLineBorder(Color.BLACK);
r5.setBorder(border5);
JButton br1 = new JButton("Conferma");
br1.setBounds(475,49,120,25);
JButton br2 = new JButton("Conferma");
br2.setBounds(475,105,120,25);
JButton br3 = new JButton("Conferma");
br3.setBounds(475,160,120,25);
JButton br4 = new JButton("Conferma");
br4.setBounds(475,216,120,25);
JButton br5 = new JButton("Conferma");
br5.setBounds(475,270,120,25);
JLabel lp1 = new JLabel();
lp1.setBounds(635,45,100,30);
lp1.setForeground(Color.GREEN);
JLabel lp2 = new JLabel();
lp2.setBounds(635,103,100,30);
lp2.setForeground(Color.GREEN);
JLabel lp3 = new JLabel();
lp3.setBounds(635,158,100,30);
lp3.setForeground(Color.GREEN);
JLabel lp4 = new JLabel();
lp4.setBounds(635,213,100,30);
lp4.setForeground(Color.GREEN);
JLabel lp5 = new JLabel();
lp5.setBounds(635,269,100,30);
lp5.setForeground(Color.GREEN);
JLabel lp11 = new JLabel();
lp11.setBounds(635,45,100,30);
lp11.setForeground(Color.RED);
JLabel lp22 = new JLabel();
lp22.setBounds(635,103,100,30);
lp22.setForeground(Color.RED);
JLabel lp33 = new JLabel();
lp33.setBounds(635,158,100,30);
lp33.setForeground(Color.RED);
JLabel lp44 = new JLabel();
lp44.setBounds(635,213,100,30);
lp44.setForeground(Color.RED);
JLabel lp55 = new JLabel();
lp55.setBounds(635,269,100,30);
lp55.setForeground(Color.RED);
int number = 0;
br1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr1 = r1.getText();
if(sr1.equals("Roma")||sr1.equals("roma")) {
lp1.setText("ESATTO");
number += 1;
}else {
lp11.setText("ERRATO");
}
}
});
br2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr2 = r2.getText();
if(sr2.equals("256")) {
lp2.setText("ESATTO");
}else {
lp22.setText("ESATTO");
}
}
});
br3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr3 = r3.getText();
if(sr3.equals("1492")) {
lp3.setText("ESATTO");
}else {
lp33.setText("ERRATO");
}
}
});
br4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr4 = r4.getText();
if(sr4.equals("New York")||sr4.equals("new york")) {
lp4.setText("ESATTO");
}else {
lp44.setText("ERRATO");
}
}
});
br5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String sr5 = r5.getText();
if(sr5.equals("Dante")||sr5.equals("dante")) {
lp5.setText("ESATTO");
}else {
lp55.setText("ERRATO");
}
}
});
JLabel risultato = new JLabel("Il tuo punteggio finale è: "+number+"/5");
risultato.setBounds(10,320,300,60);
c2.add(domanda1);
c2.add(domanda2);
c2.add(domanda3);
c2.add(domanda4);
c2.add(domanda5);
c2.add(r1);
c2.add(r2);
c2.add(r3);
c2.add(r4);
c2.add(r5);
c2.add(br1);
c2.add(br2);
c2.add(br3);
c2.add(br4);
c2.add(br5);
c2.add(lp1);
c2.add(lp2);
c2.add(lp3);
c2.add(lp4);
c2.add(lp5);
c2.add(lp11);
c2.add(lp22);
c2.add(lp33);
c2.add(lp44);
c2.add(lp55);
c2.add(risultato);
c2.add(labelNull2);
}
});
JLabel labelNull = new JLabel();
c.add(benvenuto);
c.add(label1);
c.add(areaNome);
c.add(startButton);
c.add(labelStarting);
c.add(labelNull);
}
public static void main(String[] args) {
homePage();
}
}
在按钮br1的动作侦听器之前有变量(int number = 0;),如果9行向下,它会增加但它会给我一个错误:从内部类引用的局部变量必须是最终的或有效的final ...如果我让数字到最后(最终的int数= 0;)增加给我另一个错误:不能给最终变量号赋值...其中' s错误??
答案 0 :(得分:0)
在number
方法之外定义您的homepage
变量,这样可以正常工作而不是最终
修改强>
首先从int number = 0;
方法中删除此homepage
,然后在方法外定义数字:
public class Quiz {
static int number = 0;
private static void homePage() {
//homepage body
}
}
这应该使它编译
现在很少有评论
JLabel risultato = new JLabel("Il tuo punteggio finale è: "+number+"/5");
如果您的号码会更改,则此标签不会更新,为了使其正常工作,您需要更新标签内的文字
答案 1 :(得分:-1)
我学到的一个技巧是制作一个包含1个元素的数组。这样,你可以这样做:
final int[] number = { 0 };
number[0] = 4; // random number picked
现在您可以将值设置为最终变量:)