如何创建JPanel作为主类

时间:2014-09-12 02:37:06

标签: java swing

这是从Jpanel扩展的子类。目前我没有主要的驱动程序,即Jpanel。你能帮我创一个吗?

这是该程序的子类,它显示了当用户单击applet时滚动的一对骰子。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Shows a pair of dice that are rolled when the user clicks on the
 * applet.  It is assumed that the panel is 100-by-100 pixels.
 */
public class DicePanel extends JPanel {

    private int die1 = 4;  // The values shown on the dice.
    private int die2 = 3;

    /**
     *  The constructor adds a mouse listener to the panel.  The listener
     *  will roll the dice when the user clicks the panel.  Also, the
     *  background color and the preferred size of the panel are set.
     */
    public DicePanel() {
       setPreferredSize( new Dimension(100,100) );
       setBackground( new Color(200,200,255) );  // light blue
       addMouseListener( new MouseAdapter() {
           public void mousePressed(MouseEvent evt) {
               roll();
           }
       });
    }

    /**
     * Draw a die with upper left corner at (x,y).  The die is
     * 35 by 35 pixels in size.  The val parameter gives the
     * value showing on the die (that is, the number of dots).
     */
    private void drawDie(Graphics g, int val, int x, int y) {
       g.setColor(Color.white);
       g.fillRect(x, y, 35, 35);
       g.setColor(Color.black);
       g.drawRect(x, y, 34, 34);
       if (val > 1)  // upper left dot
          g.fillOval(x+3, y+3, 9, 9);
       if (val > 3)  // upper right dot
          g.fillOval(x+23, y+3, 9, 9);
       if (val == 6) // middle left dot
          g.fillOval(x+3, y+13, 9, 9);
       if (val % 2 == 1) // middle dot (for odd-numbered val's)
          g.fillOval(x+13, y+13, 9, 9);
       if (val == 6) // middle right dot
          g.fillOval(x+23, y+13, 9, 9);
       if (val > 3)  // bottom left dot
          g.fillOval(x+3, y+23, 9, 9);
       if (val > 1)  // bottom right dot
          g.fillOval(x+23, y+23, 9,9);
    }


    /**
     * Roll the dice by randomizing their values.  Tell the
     * system to repaint the applet, to show the new values.
     */
    void roll() {
       die1 = (int)(Math.random()*6) + 1;
       die2 = (int)(Math.random()*6) + 1;
       repaint();
    }


    /**
     * The paintComponent method just draws the two dice and draws
     * a one-pixel wide blue border around the panel.
     */
    public void paintComponent(Graphics g) {
       super.paintComponent(g);  // fill with background color.
       g.setColor( Color.BLUE );
       g.drawRect(0,0,99,99);
       g.drawRect(1,1,97,97);
       drawDie(g, die1, 10, 10);
       drawDie(g, die2, 55, 55);
    }

} // end class DicePanel

1 个答案:

答案 0 :(得分:3)

您只需向main添加DicePanel方法并将其包装在JFrame

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import javafx.scene.input.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DicePanel extends JPanel {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DicePanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private int die1 = 4;  // The values shown on the dice.
    private int die2 = 3;

    /**
     * The constructor adds a mouse listener to the panel. The listener will roll the dice when the user clicks the panel. Also, the background color and the
     * preferred size of the panel are set.
     */
    public DicePanel() {
        setPreferredSize(new Dimension(100, 100));
        setBackground(new Color(200, 200, 255));  // light blue
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                roll();
            }
        });
    }

    /**
     * Draw a die with upper left corner at (x,y). The die is 35 by 35 pixels in size. The val parameter gives the value showing on the die (that is, the number
     * of dots).
     */
    private void drawDie(Graphics g, int val, int x, int y) {
        g.setColor(Color.white);
        g.fillRect(x, y, 35, 35);
        g.setColor(Color.black);
        g.drawRect(x, y, 34, 34);
        if (val > 1) // upper left dot
        {
            g.fillOval(x + 3, y + 3, 9, 9);
        }
        if (val > 3) // upper right dot
        {
            g.fillOval(x + 23, y + 3, 9, 9);
        }
        if (val == 6) // middle left dot
        {
            g.fillOval(x + 3, y + 13, 9, 9);
        }
        if (val % 2 == 1) // middle dot (for odd-numbered val's)
        {
            g.fillOval(x + 13, y + 13, 9, 9);
        }
        if (val == 6) // middle right dot
        {
            g.fillOval(x + 23, y + 13, 9, 9);
        }
        if (val > 3) // bottom left dot
        {
            g.fillOval(x + 3, y + 23, 9, 9);
        }
        if (val > 1) // bottom right dot
        {
            g.fillOval(x + 23, y + 23, 9, 9);
        }
    }

    /**
     * Roll the dice by randomizing their values. Tell the system to repaint the applet, to show the new values.
     */
    void roll() {
        die1 = (int) (Math.random() * 6) + 1;
        die2 = (int) (Math.random() * 6) + 1;
        repaint();
    }

    /**
     * The paintComponent method just draws the two dice and draws a one-pixel wide blue border around the panel.
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // fill with background color.
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, 99, 99);
        g.drawRect(1, 1, 97, 97);
        drawDie(g, die1, 10, 10);
        drawDie(g, die2, 55, 55);
    }

} // end class DicePanel

就个人而言,我会创建一个新的Main类,基本上做同样的事情

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DicePanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

这意味着您的DicePanel并未承担不必要的体重,并且您将程序限制为只有一个入口点......但那只是我......