다음은 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
'main' 메소드를 추가하고'DramePanel'을'JFrame'으로 감싸고 – MadProgrammer