gpt4 book ai didi

Java 图形 JApplet

转载 作者:行者123 更新时间:2023-12-02 00:26:11 30 4
gpt4 key购买 nike

我目前正在开发一个学校项目。我们必须制作一个Applet,我选择了JApplet。由于某种原因,我用来显示特定字符​​串的面板将不会显示。这里可能有什么问题?请指出我正确的方向。另外,我看了一些教程,有些建议我有“启动”、“停止”和“销毁”方法,有些则没有。这些方法是否会导致我的 JPanel 不显示图形?

谢谢

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);
}

}

}

最佳答案

没有组件的 JPanel 的默认大小为 0x0。尝试这个来源:

// <applet code=Shape width=640 height=480></applet>
import java.awt.*;

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

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
// This is set by HTML!
//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();
dPanel.setPreferredSize(new Dimension(500,300));

// 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);
}

}

}

关于Java 图形 JApplet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9983993/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com