-2

在我的代码中使用了实现后,如何解决 VS-Code 中发生的这个错误有什么错误帮助我解决这个问题,我只是想在这个 YouTube 频道的帮助下在 QuizGame 上制作一个项目:https ://youtu.be/EMTkeefioMg 我尝试了所有方法,但仍然多次出现错误,请帮助我解决此代码

    import javax.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.plaf.FontUIResource;
    
    public class QuizGame extends JFrame implements ActionListener {
    
        JButton b1, b2;
    
        QuizGame ()          //Constructor
        {
            setBounds(200, 100, 1000, 500);
            getContentPane().setBackground(Color.WHITE);
            setLayout(null);
    
           /* ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("2021 project/Java project/GUI Programs/quizlogo.jpg"));
            JLabel l1 = new JLabel(i1);
            
            l1.setBounds(0, 0, 500, 500);
            add(l1);*/
    
            JLabel l2 = new JLabel("Quiz Game");
              l2.setFont(new FontUIResource("Mongolian Baiti",Font.BOLD,
55));
              l2.setForeground(Color.BLUE);
              l2.setBounds(620, 40, 300, 45);
              add(l2);
    
            JLabel l3 = new JLabel("Enter Your Name:");
              l3.setFont(new FontUIResource("Mongolian Baiti", Font.BOLD, 25));
              l3.setForeground(Color.BLUE);
              l3.setBounds(650, 180, 250, 45);
              add(l3);
    
            JTextField t1 = new JTextField();
              t1.setBounds(625, 240, 250, 35);
              t1.setFont(new FontUIResource("Times New Roman", Font.BOLD, 20));
              add(t1);
    
            b1 = new JButton("Rules");
            b1.setBounds(600, 320, 120, 25);
            b1.setBackground(new Color(30, 144, 254));
            b1.setForeground(Color.WHITE);
            b1.addActionListener(this);
            add(b1);
    
            b2 = new JButton("Exit");
            b2.setBounds(800, 320, 120, 25);
            b2.setBackground(new Color(255, 0, 0));
            b2.setForeground(Color.WHITE);
            b2.addActionListner(this);
            add(b2);
            
            setVisible(true);
    
        }
    
        public class Rules extends JFrame{
             
           Rules(){
            setBounds(200, 100, 1000, 500);
            getContentPane().setBackground(Color.WHITE);
            setLayout(null);
    
            JLabel l1 = new JLabel("Welcome"+ username + "to Quiz Game");
            l1.setBounds(50, 20, 700, 30);
            add(l1);
    
            setVisible(true);
           }
      }
        public void actionPerformed(ActionEvent ae){
    
         if(ae.getSource() == b1){
    
            }
            else{
                System.exit(0);
            }
    
        }
          public static void main(String[] args){ //(Line 83)
            new QuizGame(); //object created 
        }
    }
    
    
    
     
4

1 回答 1

0

Command PaletteJava: Clean Java Language Server Workspace之后,这是一个简单的更正,使您的代码至少显示第一个窗口:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.plaf.FontUIResource;

public class QuizGame extends JFrame implements ActionListener {

    JButton b1, b2;
    JTextField t1;

    QuizGame ()          //Constructor
    {
        setBounds(200, 100, 1000, 500);
        getContentPane().setBackground(Color.WHITE);
        setLayout(null);

       /* ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("2021 project/Java project/GUI Programs/quizlogo.jpg"));
        JLabel l1 = new JLabel(i1);
        
        l1.setBounds(0, 0, 500, 500);
        add(l1);*/

        JLabel l2 = new JLabel("Quiz Game");
          l2.setFont(new FontUIResource("Mongolian Baiti",Font.BOLD,
55));
          l2.setForeground(Color.BLUE);
          l2.setBounds(620, 40, 300, 45);
          add(l2);

        JLabel l3 = new JLabel("Enter Your Name:");
          l3.setFont(new FontUIResource("Mongolian Baiti", Font.BOLD, 25));
          l3.setForeground(Color.BLUE);
          l3.setBounds(650, 180, 250, 45);
          add(l3);

        t1 = new JTextField();
          t1.setBounds(625, 240, 250, 35);
          t1.setFont(new FontUIResource("Times New Roman", Font.BOLD, 20));
          add(t1);

        

        b1 = new JButton("Rules");
        b1.setBounds(600, 320, 120, 25);
        b1.setBackground(new Color(30, 144, 254));
        b1.setForeground(Color.WHITE);
        b1.addActionListener(this);
        add(b1);

        b2 = new JButton("Exit");
        b2.setBounds(800, 320, 120, 25);
        b2.setBackground(new Color(255, 0, 0));
        b2.setForeground(Color.WHITE);
        b2.addActionListener(this);
        add(b2);
        
        setVisible(true);
        

    }

    public class Rules extends JFrame{
         
       Rules(){
        setBounds(200, 100, 1000, 500);
        getContentPane().setBackground(Color.WHITE);
        setLayout(null);

        String username=t1.getText();  
        JLabel l1 = new JLabel("Welcome" +username+"to Quiz Game");
        
        l1.setBounds(50, 20, 700, 30);
        add(l1);
        setVisible(true);
       }
  }
    public void actionPerformed(ActionEvent ae){

     if(ae.getSource() == b1){

        }
        else{
            System.exit(0);
        }

    }
      public static void main(String[] args){ //(Line 83)
        new QuizGame(); //object created 
    }
}

在此处输入图像描述

您提到的教程非常明确,作者创建了独立版rules.java以使代码更简洁。请仔细按照教程进行操作,然后重试。

于 2022-01-10T07:50:24.857 回答