新版java中没有japplet了_Java圖形JApplet

我目前正在爲學校開發一個程序。我們必須製作一個Applet,並選擇了JApplet。出於某種原因,我用來顯示特定字符​​串的面板不會顯示。這可能是什麼問題?請指向正確的方向。另外,我看了一些教程,一些人建議我有「開始」,「停止」和「破壞」方法,有些則沒有。這些方法中的任何一種都會影響我的JPanel爲什麼不顯示圖形?Java圖形JApplet

謝謝

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Shape extends JApplet {

/**

*

*/

private static final long serialVersionUID = 1L;

// making the radiobuttons for the shape choices

JRadioButton squareButton = new JRadioButton("Square",true);

JRadioButton ovalButton = new JRadioButton("Oval",false);

JRadioButton rectangleButton = new JRadioButton("Rectangle",false);

JRadioButton triangleButton = new JRadioButton("Triangle",false);

// making radiobuttons for the color choices

JRadioButton redButton = new JRadioButton("Red",true);

JRadioButton blueButton = new JRadioButton("Blue",false);

JRadioButton greenButton = new JRadioButton("Green",false);

JRadioButton yellowButton = new JRadioButton("Yellow",false);

// making buttons draw and animate

JButton drawButton = new JButton("Draw!");

JButton animateButton = new JButton("Animate!");

// making JTextFields for length and width

JTextField lengthField = new JTextField("Enter a length",15);

JTextField widthField = new JTextField("Enter a width",15);

// making JPanel, in which the radiobuttons will go01

JPanel shapePanel = new JPanel();

JPanel colorPanel = new JPanel();

JPanel buttonPanel = new JPanel();

JPanel textPanel = new JPanel();

drawPanel dPanel;

ButtonGroup shapeGroup = new ButtonGroup();

ButtonGroup colorGroup = new ButtonGroup();

// variables that will dictates the shape, size and color

int length = 200;

int width = 200;

Color color = Color.RED;

String shape = "square";

public void init() {

setLayout(new FlowLayout()); // setting layout for the applet

setSize(680,480);

// setting the layout for the shapePanel - gridlayout 2 rows, 2 cols and space of 5

shapePanel.setLayout(new GridLayout(2,2,5,5));

// adding a border to the shapePanel to indicate to the user what it does "titledBorder"

shapePanel.setBorder(BorderFactory.createTitledBorder("Choose a shape"));

// setting layout for the color panel - gridlayout 2 rows, 2 cols and space of 5

colorPanel.setLayout(new GridLayout(2,2,5,5));

// adding a border to the colorPanel to indicate to the user what it does "titledBorder"

colorPanel.setBorder(BorderFactory.createTitledBorder("Choose a color"));

// setting the layout for the buttonPanel - gridlayout 1 row, 2 cols and space of 5

buttonPanel.setLayout(new GridLayout(1,2,5,5));

// adding a color border

buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red, 2));

// setting the layout of the textField - gridlayout 1 row, 2 cols and space of 5

textPanel.setLayout(new GridLayout(1,2,5,5));

// adding some attributes for lengthField and widthField

lengthField.setFont(new Font("Arial",Font.PLAIN,12));

lengthField.setForeground(new Color(150,150,150));

widthField.setFont(new Font("Arial",Font.PLAIN,12));

widthField.setForeground(new Color(150,150,150));

// using shapegroup to organize the JRadioButtons

shapeGroup.add(squareButton);

shapeGroup.add(ovalButton);

shapeGroup.add(rectangleButton);

shapeGroup.add(triangleButton);

// using colorgroup to organize the color radiobuttons

colorGroup.add(redButton);

colorGroup.add(blueButton);

colorGroup.add(greenButton);

colorGroup.add(yellowButton);

// add the shape buttons to the panel so they appear in a square form

shapePanel.add(squareButton);

shapePanel.add(ovalButton);

shapePanel.add(rectangleButton);

shapePanel.add(triangleButton);

// adding color buttons to the color panel

colorPanel.add(redButton);

colorPanel.add(blueButton);

colorPanel.add(greenButton);

colorPanel.add(yellowButton);

// adding jbuttons

buttonPanel.add(drawButton);

buttonPanel.add(animateButton);

// adding textfields to the textPanel

textPanel.add(lengthField);

textPanel.add(widthField);

dPanel = new drawPanel();

// adding panels to the applet

add(shapePanel);

add(colorPanel);

add(buttonPanel);

add(textPanel);

add(dPanel);

// adding focus listener to lengthField and widthField

lengthField.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {

lengthField.setText("");

lengthField.setForeground(Color.black);

}

public void focusLost(FocusEvent e) {}

});

widthField.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {

widthField.setText("");

widthField.setForeground(Color.black);

}

public void focusLost(FocusEvent e) {}

});

drawButton.addActionListener(new drawListener());

}

// when the person presses paint, this will be executed to paint the specific shape, color with the width and length

class drawListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

int mylength = 5;

int mywidth = 5;

try {

mylength = Integer.parseInt(lengthField.getText());;

mywidth = Integer.parseInt(widthField.getText());;

}catch(Exception ex) {

JOptionPane.showMessageDialog(null,""+ex,"",JOptionPane.ERROR_MESSAGE);

}

if((mylength > 200 || mylength < 5)) {

JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",

"Invalid length message", JOptionPane.ERROR_MESSAGE);

}else if((mywidth > 200 || mywidth < 5)) {

JOptionPane.showMessageDialog(null, "Please make sure the number is above 5 and less than 200",

"Invalid width message", JOptionPane.ERROR_MESSAGE);

}else {

length = mylength;

width = mywidth;

// checking which color button is selected

if(redButton.isSelected()) {

color = Color.RED;

}else if(blueButton.isSelected()) {

color = Color.BLUE;

}else if(greenButton.isSelected()) {

color = Color.GREEN;

}else if(yellowButton.isSelected()) {

color = Color.YELLOW;

}

// checking which shape has been selected

if(rectangleButton.isSelected()) {

shape = "rectangle";

}else if(triangleButton.isSelected()) {

shape = "triangle";

}else if(ovalButton.isSelected()) {

shape = "oval";

}else if(squareButton.isSelected()) {

shape = "square";

}

//System.out.printf("%3d %3d %s %s \n",length,width,shape,color);

}

}

}

// This will be used to do the painting

class drawPanel extends JPanel {

private static final long serialVersionUID = 1L;

//Paint Method

public void paintComponent(Graphics g){

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setColor(Color.black);

g2.drawString("My awesome string", 200, 200);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值