2014-09-12 2 views
0

다음은 Jpanel에서 확장되는 Subclass입니다. 지금은 Jpanel이라는 주요 드라이버 프로그램이 없습니다. 하나 만들 수 있도록 도와 주시겠습니까?메인 클래스로 JPanel을 생성하는 방법

다음은 사용자가 애플릿을 클릭 할 때 굴리는 주사위 쌍을 보여주는 프로그램의 하위 클래스입니다.

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 
+2

'main' 메소드를 추가하고'DramePanel'을'JFrame'으로 감싸고 – MadProgrammer

답변

3

당신은 단순히 나는 기본적으로 같은 일을했던 새로운 Main 클래스를 만드는 것, 개인적으로 비록 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 

DicePanelmain 방법을 추가하고 포장 할 수

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은 불필요한 무게를 지니고 있지 않으며 프로그램을 havi로 제한합니다. 하나의 엔트리 포인트를 ... 그러나 그것은 단지 나를 ...

+0

+1을 노크합니다. 왜'Main JPanel' 연장 ;-) –

+0

@peeskillet 복사하여 붙여 넣기 오류 : P – MadProgrammer

관련 문제