【问题标题】:How to Convert Applet to JFrame [closed]如何将 Applet 转换为 JFrame [关闭]
【发布时间】:2016-01-19 14:13:03
【问题描述】:

这是我们的游戏代码。我们之前将它用作小程序查看器,我们尝试转换为 jframe,但它不断给我们ClassCastException 错误。请帮助我们从applet 转换为jframe。这是代码,告诉我们该怎么做?

import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.*;


public class gg extends JFrame implements MouseListener
{   
    private JButton table[][];     
    //private boolean bomb[][];    
//  private boolean flag[][];      
    private boolean exposed[][]; 
    //private boolean checkwinbool[][]; 
    private int row = 16, col = 30; 
    private int sizex = 780, sizey = 492;
    private Font f ;
    private JPanel P;
    private JLabel TimeRemaning, NG;
    private JButton RestartE, RestartM, RestartH;
    private GridLayout gl;



    public gg() {

        setLayout (new BorderLayout ());
        gl = new GridLayout (row, col);
        P = new JPanel (gl);
        f = new Font ("ComicSans", Font.BOLD, 17);
        setFont (f);
        TimeRemaning = new JLabel ("");
        NG = new JLabel ("New Game");
        RestartE = new JButton ("Easy");


        table = new JButton [row] [col];
        //flag = new boolean [row] [col];
        exposed = new boolean [row] [col];
        //checkwinbool = new boolean [row] [col];
        for (int x = 0 ; x < row ; x++)
        {
            for (int y = 0 ; y < col ; y++)
            {
                table [x] [y] = new JButton ();
                table [x] [y].addMouseListener (this);
                P.add (table [x] [y]);
            }
        }
        //these for loops set up the buttons and sets all the boolean arrays to = false

        add (P, "Center");
        NG.setBackground(Color.lightGray);
        NG.setForeground(Color.black);
        Restart_Game (row, col, row, col, sizex, sizey);



    }


    public void Restart_Game (int row, int col, int prow, int pcol, int sizex, int sizey)
    {

        setSize (sizex, sizey);
        invalidate();
        validate();
        gl.setRows (row);
        gl.setColumns (col);

        for (int x = 0 ; x < prow ; x++)
        {
            for (int y = 0 ; y < pcol ; y++)
            {
                P.remove (table [x] [y]);
            }
        }
        for (int x = 0 ; x < row ; x++)
        {
            for (int y = 0 ; y < col ; y++)
            {

                table [x] [y].setEnabled (true);

                table [x] [y].setBackground (Color.gray);
                table [x] [y].setForeground (Color.white);
                P.add (table [x] [y]);
                //flag [x] [y] = false;
                exposed [x] [y] = false;
                //checkwinbool [x] [y] = false;
            }
        }
        setSize (sizex, sizey);
        invalidate();
        validate();


    }


    public void mouseClicked (MouseEvent e)
    {
        if (e.getSource () == RestartE)
        {
            row = 10;
            col = 10;
            sizex = 300;
            sizey = 346;
        }
    }
  1. 列表项

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {    }
    public void mouseExited(MouseEvent e) {}}
    

【问题讨论】:

  • "... 告诉我们该怎么做?" - 你阅读了堆栈跟踪,并修复了错误。由于您没有费心将其包含在问题中,因此我们不太可能为您提供帮助。

标签: java swing applet jframe japplet


【解决方案1】:

您无法将一个类转换为另一个类,您需要重新设计程序。像这样的:

public class GameBoard extends JPanel {

     // all your stuff from gg class
}

public class GameFrame extends JFrame {

    public GameFrame() {
        add(new GameBoard());
    }
}

public class GameApplet extends JApplet {

    public GameApplet() {
        add(new GameBoard());
    }
}

【讨论】:

    【解决方案2】:

    JApplet 替换为JFrame 并在最后一个} 之前添加主方法public static void main( String[] args ) { gg gg = new gg(); }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2013-03-25
      • 2013-10-19
      • 2020-05-07
      • 2013-01-10
      • 2010-09-13
      • 2021-03-27
      相关资源
      最近更新 更多