【问题标题】:How do I change this Java Applet to use JPanel?如何更改此 Java Applet 以使用 JPanel?
【发布时间】:2012-05-11 02:05:55
【问题描述】:

怎么改成JPanel,这段代码是Java的Applet怎么改?

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.lang.reflect.Array;
public class Hangman extends Applet implements ActionListener{

        static final int DEAD=13;   // amount of errors till loss
        private int errors;        // amount of errors
        private String message;   // error or victorie
        private String information; // information of the message
        private String rword;      // the rword
        private StringBuffer gword;// the gword
        private Button bStart;      // Button "Restart"
        private Button bGo;         // Button "Go"
        private TextField tfLetter; // letter box
        private Font fnt;           // common font

        public void init(){

        fnt = new Font( "Monospaced", 20, 20 );
        setFont(fnt);

                // Create textbox for guess letter

                tfLetter = new TextField();
                // Create buttons and labels

                bStart = new Button("Restart");
                bGo = new Button("Go");
                // Add the graphical elements to the applet

                add(bStart);
                add(new Label("Guess a letter:"));
                add(tfLetter);
                add(bGo);

                // Buttons are events:
                bStart.addActionListener(this);
                bGo.addActionListener(this);

                // Start first game
                initGame();
        }

        public void initGame(){

        /* Setting the errors to 0 */
                errors=0;


        /* Enter the wordslist, separated by a | here: */
                String str = "computer|radio|calculator|teacher|bureau|police|geometry|president|subject|country|enviroment|classroom|animals|province|month|politics|puzzle|instrument|kitchen|language|vampire|ghost|solution|service|software|virus25|security|phonenumber|expert|website|agreement|support|compatibility|advanced|search|triathlon|immediately|encyclopedia|endurance|distance|nature|history|organization|international|championship|government|popularity|thousand|feature|wetsuit|fitness|legendary|variation|equal|approximately|segment|priority|physics|branche|science|mathematics|lightning|dispersion|accelerator|detector|terminology|design|operation|foundation|application|prediction|reference|measurement|concept|perspective|overview|position|airplane|symmetry|dimension|toxic|algebra|illustration|classic|verification|citation|unusual|resource|analysis|license|comedy|screenplay|production|release|emphasis|director|trademark|vehicle|aircraft|experiment";
                String[] temp;

                /* delimiter */
                String delimiter = "\\|";

                /* given string will be split by the argument delimiter provided. */
                temp = str.split(delimiter);

        /* Setting the seed */
        Random randomGenerator = new Random();

        /* Generating random number */
        int randomInt = randomGenerator.nextInt(temp.length);
                rword = new String(temp[randomInt]);
                char positions[] = new char[rword.length()];
                for (int i=0; i<rword.length(); i++) {
                        positions[i] = '.';
                }
                String s = new String(positions);
                gword = new StringBuffer(s);
                tfLetter.setText("");

                // Delete the messages
                message="";
                information = "";
                repaint();
        }
        public void paint(Graphics g) {


                // Draw the hangman
                int baseY = 250;

                if (errors >  0){    // ground
                        g.drawLine(90, baseY,200,baseY);
                }
                if (errors >  1){    // bar up
                        g.drawLine(125,baseY,125,baseY-100);
                }
                if (errors >  2){
                        g.drawLine(110,baseY,125,baseY-15);
                }
                if (errors >  3){
                        g.drawLine(140,baseY,125,baseY-15);
                }
                if (errors >  4){    // side bar
                        g.drawLine(125,baseY-100,175,baseY-100);
                }
                if (errors >  5){
                        g.drawLine(125,baseY-85,140,baseY-100);
                }
                if (errors >  6){    // rope
                        g.drawLine(175,baseY-100,175,baseY-75);
                }
                if (errors >  7){    // body
                        g.drawOval(170,baseY-75,10,12);
                }
                if (errors >  8){
                        g.drawOval(170,baseY-65,15,25);
                }
                if (errors >  9){    // arms
                        g.drawLine(160,baseY-65,170,baseY-60);
                }
                if (errors > 10){
                        g.drawLine(183,baseY-60,193,baseY-65);
                }
                if (errors > 11){    // legs
                        g.drawLine(165,baseY-30,170,baseY-45);
                }
                if (errors > 12){
                        g.drawLine(183,baseY-45,193,baseY-30);
                }

                // Show the messages
                g.drawString( message, 40, baseY+25 );
                g.drawString( information, 25, baseY+45 );
                g.drawString( new String (gword), 110, 60);

        }

        public void actionPerformed(ActionEvent e){
                if (e.getSource() == bStart){
                        initGame();
                }
                if (e.getSource() == bGo){
                        processTurn();
                        // Delete the letter input box
                        tfLetter.setText("");
                        repaint();
                }
        }
        private void processTurn(){
                String s, t;
                char a;

                s = tfLetter.getText();
                a = s.charAt(0);

                if (!Character.isLetter(a)){
                          message="Only enter letters!";
                          return;
                }
                if (s.length()>1){
                          message="One letter at a time!";
                          return;
                }

                // Has the letter been guessed

                t = new String(gword);
                if (t.indexOf(s) != -1){
                        message="Letter has already been guessed";
                        return;
                }

                // If the letter doesn't occur in the rword

                if (rword.indexOf(s) == -1){
                        message="";
                        errors++;
                        if (errors==DEAD){
                                message="You lost!";
                                information = 
                                      "Click on restart for another chance!";
                        }

                        return;
                }

                // Replace dots in gword with the found letter.

                for (int i=0; i<rword.length(); i++){
                        if (rword.charAt(i) == a){
                                gword.setCharAt(i, a);
                        }
                }
                t = new String(gword);

                // If all the dots have been filled, you win

                if (t.indexOf('.') == -1){
                        message="You win!";
                        return;
                }

                // Delete message

                message="";
                repaint();
        }
}

【问题讨论】:

    标签: java swing applet awt jpanel


    【解决方案1】:

    替换这一行:

    public class Hangman extends Applet implements ActionListener{

    由以下人:

    public class Hangman extends JPanel implements ActionListener{

    Applet 扩展了扩展 Container 的 Panel。 JPanel 也间接扩展了 Container。方法add()在容器中声明。

    因此,您唯一需要做的就是自己致电init()。这个方法是java插件在applet时调用的。

    JPanel yourGame = new Hangman(); yourGame.init();

    顺便说一句,您甚至不必让您的游戏扩展 JPanel。您想在单独的框架中运行它吗?在这种情况下,请执行以下操作:

    Frame f - new Frame();
    Panel yourGame = new Hangman();
    yourGame.init();
    f.add(yourGame);
    f.setSize(300, 300);
    f.setVisible(true);
    

    就是这样。这将创建框架并将您的小程序(即面板)放在框架的中间。

    【讨论】:

    • 你在哪里得到 Frame f - new Frame();面板 yourGame = new Hangman();你的游戏.init(); f.add(yourGame); f.setSize(300, 300); f.setVisible(true);
    【解决方案2】:

    查看代码中的无数小改动。请注意,我从游戏中删除了第二个按钮,而是将动作侦听器直接添加到文本字段。

    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    import java.lang.reflect.Array;
    
    public class Hangman extends JPanel implements ActionListener{
    
        static final int DEAD=13;   // amount of errors till loss
        private int errors;        // amount of errors
        private String message;   // error or victorie
        private String information; // information of the message
        private String rword;      // the rword
        private StringBuffer gword;// the gword
        private JButton bStart;      // Button "Restart"
        private JTextField tfLetter; // letter box
        private Font fnt;           // common font
    
        Hangman() {
    
            fnt = new Font( "Monospaced", 20, 20 );
            setFont(fnt);
    
            // Create textbox for guess letter
            tfLetter = new JTextField(4);
            // Create buttons and labels
    
            bStart = new JButton("Restart");
            //bGo = new JButton("Go");
            // Add the graphical elements to the applet
    
            setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
            add(bStart);
            add(new JLabel("Guess a letter:"));
            add(tfLetter);
    
            // Buttons are events:
            bStart.addActionListener(this);
            tfLetter.addActionListener(this);
    
            validate();
    
            // Start first game
            initGame();
            setPreferredSize(new Dimension(300,300));
        }
    
        public void initGame(){
    
            /* Setting the errors to 0 */
            errors=0;
    
    
            /* Enter the wordslist, separated by a | here: */
            String str = "computer|radio|calculator|teacher|bureau|police|geometry|president|subject|country|enviroment|classroom|animals|province|month|politics|puzzle|instrument|kitchen|language|vampire|ghost|solution|service|software|virus25|security|phonenumber|expert|website|agreement|support|compatibility|advanced|search|triathlon|immediately|encyclopedia|endurance|distance|nature|history|organization|international|championship|government|popularity|thousand|feature|wetsuit|fitness|legendary|variation|equal|approximately|segment|priority|physics|branche|science|mathematics|lightning|dispersion|accelerator|detector|terminology|design|operation|foundation|application|prediction|reference|measurement|concept|perspective|overview|position|airplane|symmetry|dimension|toxic|algebra|illustration|classic|verification|citation|unusual|resource|analysis|license|comedy|screenplay|production|release|emphasis|director|trademark|vehicle|aircraft|experiment";
            String[] temp;
    
            /* delimiter */
            String delimiter = "\\|";
    
            /* given string will be split by the argument delimiter provided. */
            temp = str.split(delimiter);
    
            /* Setting the seed */
            Random randomGenerator = new Random();
    
            /* Generating random number */
            int randomInt = randomGenerator.nextInt(temp.length);
            rword = new String(temp[randomInt]);
            char positions[] = new char[rword.length()];
            for (int i=0; i<rword.length(); i++) {
                    positions[i] = '.';
            }
            String s = new String(positions);
            gword = new StringBuffer(s);
            tfLetter.setText("");
    
            // Delete the messages
            message="";
            information = "";
            repaint();
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
    
            // Draw the hangman
            int baseY = 250;
    
            if (errors >  0){    // ground
                    g.drawLine(90, baseY,200,baseY);
            }
            if (errors >  1){    // bar up
                    g.drawLine(125,baseY,125,baseY-100);
            }
            if (errors >  2){
                    g.drawLine(110,baseY,125,baseY-15);
            }
            if (errors >  3){
                    g.drawLine(140,baseY,125,baseY-15);
            }
            if (errors >  4){    // side bar
                    g.drawLine(125,baseY-100,175,baseY-100);
            }
            if (errors >  5){
                    g.drawLine(125,baseY-85,140,baseY-100);
            }
            if (errors >  6){    // rope
                    g.drawLine(175,baseY-100,175,baseY-75);
            }
            if (errors >  7){    // body
                    g.drawOval(170,baseY-75,10,12);
            }
            if (errors >  8){
                    g.drawOval(170,baseY-65,15,25);
            }
            if (errors >  9){    // arms
                    g.drawLine(160,baseY-65,170,baseY-60);
            }
            if (errors > 10){
                    g.drawLine(183,baseY-60,193,baseY-65);
            }
            if (errors > 11){    // legs
                    g.drawLine(165,baseY-30,170,baseY-45);
            }
            if (errors > 12){
                    g.drawLine(183,baseY-45,193,baseY-30);
            }
    
            // Show the messages
            g.drawString( message, 40, baseY+25 );
            g.drawString( information, 25, baseY+45 );
            g.drawString( new String (gword), 110, 60);
    
        }
    
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == bStart){
                initGame();
            }
            if (e.getSource().equals(tfLetter)){
                processTurn();
                // Delete the letter input box
                tfLetter.setText("");
                repaint();
            }
        }
        private void processTurn(){
            String s, t;
            char a;
    
            s = tfLetter.getText();
            a = s.charAt(0);
    
            if (!Character.isLetter(a)){
                      message="Only enter letters!";
                      return;
            }
            if (s.length()>1){
                      message="One letter at a time!";
                      return;
            }
    
            // Has the letter been guessed
    
            t = new String(gword);
            if (t.indexOf(s) != -1){
                    message="Letter has already been guessed";
                    return;
            }
    
            // If the letter doesn't occur in the rword
    
            if (rword.indexOf(s) == -1){
                    message="";
                    errors++;
                    if (errors==DEAD){
                            message="You lost!";
                            information =
                                  "Click on restart for another chance!";
                    }
    
                    return;
            }
    
            // Replace dots in gword with the found letter.
    
            for (int i=0; i<rword.length(); i++){
                    if (rword.charAt(i) == a){
                            gword.setCharAt(i, a);
                    }
            }
            t = new String(gword);
    
            // If all the dots have been filled, you win
    
            if (t.indexOf('.') == -1){
                    message="You win!";
                    return;
            }
    
            // Delete message
    
            message="";
                repaint();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JPanel hangman = new Hangman();
                    JOptionPane.showMessageDialog(null, hangman);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 2014-05-17
      • 1970-01-01
      • 2015-01-18
      • 2018-04-07
      相关资源
      最近更新 更多