1

我有这个想要运行的 GUI,当我尝试从类中创建一个对象时,我收到错误无法实例化类型 GUI。那么这是什么意思,我该如何解决?

主类中的代码

public static void main(String[] args){


    //Classes

    GUI go = new GUI();



    //Running Class Methods



    //JFrame
    //JFrame frame = new JFrame("Yatsy");



    go.setVisible(true);
    go.setVisible(true);
    //go.setLocation((dim.width - width) / 2, (dim.height - height) / 2);
    go.setSize(width, height);
    System.out.println(width + " " + height);
    //draw paint = new draw();
    //go.add(paint);

}

GUI 类中的代码

public GUI(){
    super();
    panel = new JPanel();
    roll = new JButton("Roll");
    nextPlayer = new JButton("Next Player");
    bDice1 = new JButton("dice");
    quit = new JButton("Quit");
    use = new JButton("Use");

    roll.addActionListener(
            new ActionListener(){
        public void actionPerformed(ActionEvent event){


            if(turn == true){

                rollResult1 = roll1();
                rollResult2 = roll2();
                rollResult3 = roll3();
                rollResult4 = roll4();
                rollResult5 = roll5();

                sRollResult1 = Integer.toString(rollResult1);
                sRollResult2 = Integer.toString(rollResult2);
                sRollResult3 = Integer.toString(rollResult3);
                sRollResult4 = Integer.toString(rollResult4);
                sRollResult5 = Integer.toString(rollResult5);


                rolls++;
                start = false;
                repaint();
                System.out.println(  rollResult1 + "   " + rollResult2 + "   " + rollResult3 + "   " + rollResult4 + "   " + rollResult5);
                //System.out.println( rollAI );

            }
            if(rolls == 3){
                turn = false;
                System.out.println("Out of rolls");
            }

        }
    });

    nextPlayer.addActionListener(
            new ActionListener(){
        public void actionPerformed(ActionEvent event){

            if(rolls > 0){
                rolls = 0;
                System.out.println("Next player");
                turn = true;
                repaint();
                start = true;
            }
        }

    });

    quit.addActionListener(
            new ActionListener(){
        public void actionPerformed(ActionEvent event){

            System.exit(0);

        }

    });

    add(panel, BorderLayout.CENTER);
    panel.setBackground(Color.BLUE);
    add(quit,  BorderLayout.SOUTH);
    add(roll, BorderLayout.NORTH);
    add(nextPlayer, BorderLayout.EAST);

    Handlerclass handler = new Handlerclass();
    panel.addMouseListener(handler);
}

所以我想知道为什么我会出错?

我正在使用java 1.7!

对于那些想知道这是完整代码的人!!!!

package Christofer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public abstract class GUI extends JFrame implements MouseListener{
    private static final long serialVersionUID = 1L;


    int dice1, dice2, dice3, dice4, dice5;




    private JButton roll;
    private JButton nextPlayer;
    private JButton bDice1;
    private JButton quit;
    private JButton use;
    private JPanel panel;
    public int rolls = 0;
    public boolean turn = true;
    public boolean start = true;

    boolean saved1 = false, saved2 = false, saved3 = false, saved4 = false, saved5 = false;


    int rollResult1;
    int rollResult2;
    int rollResult3;
    int rollResult4;
    int rollResult5;

    String sRollResult1, sRollResult2, sRollResult3, sRollResult4, sRollResult5;


    public GUI(){
        super();
        panel = new JPanel();
        roll = new JButton("Roll");
        nextPlayer = new JButton("Next Player");
        bDice1 = new JButton("dice");
        quit = new JButton("Quit");
        use = new JButton("Use");

        roll.addActionListener(
                new ActionListener(){
            public void actionPerformed(ActionEvent event){


                if(turn == true){

                    rollResult1 = roll1();
                    rollResult2 = roll2();
                    rollResult3 = roll3();
                    rollResult4 = roll4();
                    rollResult5 = roll5();

                    sRollResult1 = Integer.toString(rollResult1);
                    sRollResult2 = Integer.toString(rollResult2);
                    sRollResult3 = Integer.toString(rollResult3);
                    sRollResult4 = Integer.toString(rollResult4);
                    sRollResult5 = Integer.toString(rollResult5);


                    rolls++;
                    start = false;
                    repaint();
                    System.out.println(  rollResult1 + "   " + rollResult2 + "   " + rollResult3 + "   " + rollResult4 + "   " + rollResult5);
                    //System.out.println( rollAI );

                }
                if(rolls == 3){
                    turn = false;
                    System.out.println("Out of rolls");
                }

            }
        });

        nextPlayer.addActionListener(
                new ActionListener(){
            public void actionPerformed(ActionEvent event){

                if(rolls > 0){
                    rolls = 0;
                    System.out.println("Next player");
                    turn = true;
                    repaint();
                    start = true;
                }
            }

        });

        quit.addActionListener(
                new ActionListener(){
            public void actionPerformed(ActionEvent event){

                System.exit(0);

            }

        });

        add(panel, BorderLayout.CENTER);
        panel.setBackground(Color.BLUE);
        add(quit,  BorderLayout.SOUTH);
        add(roll, BorderLayout.NORTH);
        add(nextPlayer, BorderLayout.EAST);

        Handlerclass handler = new Handlerclass();
        panel.addMouseListener(handler);
    }




    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        this.setBackground(Color.BLUE);



        g2d.setColor(Color.WHITE);
        g2d.fillRoundRect(getWidth() / 2 - 75, getHeight() / 2 - 50, 30, 30, 5, 5);   //Dice 1
        g2d.fillRoundRect(getWidth() / 2 - 40, getHeight() / 2 - 50, 30, 30, 5, 5);   //Dice 2
        g2d.fillRoundRect(getWidth() / 2 - 5, getHeight() / 2 - 50, 30, 30, 5, 5);    //Dice 3
        g2d.fillRoundRect(getWidth() / 2 + 30, getHeight() / 2 - 50, 30, 30, 5, 5);    //Dice 4
        g2d.fillRoundRect(getWidth() / 2 + 65, getHeight() / 2 - 50, 30, 30, 5, 5);    //Dice 5


        //Score Board
        g2d.fillRect(30, 70, 370, getHeight() - 125);
        g2d.setColor(Color.BLACK);
        g2d.drawLine(150, 70, 150, getHeight() - 30);
        g2d.drawLine(30, 70, 400, 70);
        g2d.drawString("Ones", 35, 85);
        g2d.drawLine(30, 90, 400, 90);
        g2d.drawString("Twos", 35, 105);
        g2d.drawLine(30, 110, 400, 110);
        g2d.drawString("Threes", 35, 125);
        g2d.drawLine(30, 130, 400, 130);
        g2d.drawString("Fours", 35, 145);
        g2d.drawLine(30, 150, 400, 150);
        g2d.drawString("Fives", 35, 165);
        g2d.drawLine(30, 170, 400, 170);
        g2d.drawString("Sixes", 35, 185);
        g2d.drawLine(30, 190, 400, 190);


        if(start == false){
            g.setColor(Color.BLACK);

            Font font = new Font("Arial", Font.PLAIN, 25);
            g2d.setFont(font);
            g2d.drawString( sRollResult1 + "   " + sRollResult2 + "   " + sRollResult3 + "   " + sRollResult4 + "   " + sRollResult5, getWidth() / 2 - 70 , getHeight() / 2 - 25 );

            if(turn == false){
                g2d.setColor(Color.RED);
                g2d.drawString("Out Of Rolls", getWidth() / 2 - 70, getHeight() / 2 - 75);
            }
        }


    }

    public int roll1(){

        if (saved1 == false){
            dice1 = (int )(Math.random() * 6 + 1);
        }


        return dice1;
    }

    public int roll2(){
        if (saved2 == false){
            dice2 = (int )(Math.random() * 6 + 1);
        }
        return dice2;

    }

    public int roll3(){
        if (saved3 == false){
            dice3 = (int )(Math.random() * 6 + 1);
        }
        return dice3;

    }

    public int roll4(){
        if (saved4 == false){
            dice4 = (int )(Math.random() * 6 + 1);
        }
        return dice4;

    }

    public int roll5(){

        if (saved5 == false){
            dice5 = (int )(Math.random() * 6 + 1);
        }
        return dice5;

    }

    private class Handlerclass implements MouseListener{






    @Override
        public void mouseClicked(MouseEvent e) {

    }

    @Override
        public void mouseEntered(MouseEvent e) {

    }

        public void mouseExited(MouseEvent e) {

    }

        public void mousePressed(MouseEvent e) {
            mx = e.getX();
            my = e.getY();

            System.out.println("X: " + mx + "  Y: " + my);

            if(mx < getWidth() / 2 - 75 && mx > getWidth() / 2 - 45 && my < getHeight() / 2 - 50 && my > getHeight() / 2 - 20){
                System.out.println("saved1 = true");

            }
        }

        public void mouseReleased(MouseEvent e) {

    }

    }

        }

我知道我有一些清理工作要做

4

1 回答 1

7

public abstract class GUI是你的问题。您不能实例化抽象类。

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

您可以创建一个子类并实例化它。

于 2013-06-26T20:56:29.713 回答