- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个学校项目。我们必须制作一个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/
我正在尝试在 上收听 Java 中的 MouseEvents。小程序 在一个 JFrame 中。我的程序实际上做了更多的事情,但我已经为你简化了: import java.awt.*; import
所以我正在创建一个 JApplet 游戏,并将用户信息保存到 APPDATA 中的 .txt 文件中。有没有更安全的方法来保存他们的信息,不允许他们编辑信息进行作弊? 最佳答案 基本上,防止作弊的唯一
我完全不知道我的时钟程序出了什么问题。 我的程序应该接受用户输入几小时和几分钟。然后,用户的输入会将时针和分针设置到弹出窗口中绘制的圆形时钟上的适当位置。 我的程序似乎没有接受用户输入,而是从Cloc
所以我正在尝试研究简单的动画和虚拟物理等等。我正在尝试制作一个球的动画,使其随着时间的推移慢慢变大。我这里的代码与我在《Java For Dummies》书中的代码几乎完全相同,除了以下几件事:去掉小
我目前正在开发一个学校项目。我们必须制作一个Applet,我选择了JApplet。由于某种原因,我用来显示特定字符串的面板将不会显示。这里可能有什么问题?请指出我正确的方向。另外,我看了一些教程,
我知道这个问题之前已经被问过很多次了,但是每次我访问问题线程并执行提出的解决方案时,它仍然对我不起作用。 所以在我造成任何困惑之前,我遇到的问题是我绘制的屏幕不断闪烁,这是因为每次我绘制屏幕时,我都会
我最近一直在摆弄 awt/swing,试图学习如何做小程序。 我写了以下简短的JApplet example code ,这从表面上看似乎有效。 尝试退出时出现的问题: 按 ESC 不会执行任何操作,
我使用 JApplet 创建了一个简单的 Applet,一切似乎都工作正常,但是当我在小程序中创建名为 ChatUser 的用户定义类的对象时,我收到此错误:- SEVERE: java.lang.C
我花了很多时间试图理解这一点。我创建了一个 JApplet,它在循环中使用 Thread.sleep() 来为小程序设置动画。但当我尝试运行小程序时,屏幕保持空白。当我在单独的线程中绘制动画并调用重绘
我是 Japplets 新手..我想知道我是否有以下内容... classA A = new classA(); //launch Japplet here 并且classA扩展了Japp
我对 JApplet 非常陌生,这就是我现在所做的。 package myform; import java.sql.Connection; import java.sql.DriverManager
我有一个动画类,它只是绘制两个圆和一条线(没有主方法),在另一个类中,我有一个主方法,该方法将动画类作为对象传递,并且应该显示我绘制的两个圆,但是它不显示,它只显示窗口,没有我所做的圆圈或线条,如果我
第一次海报和明显的 n00b java 学生。我一直在研究这个问题,过去两天在互联网上搜索,但我尝试的任何方法都没有结果。这本来是一个使用JFrame的程序,我需要将其转换为Applet。该代码编译良
Java Applet嵌入到JSP中时是否会运行类中的所有代码?这可能听起来很愚蠢,但是当我运行独立小程序时,我可以设法将图像写入路径。但是当我将小程序嵌入到 JSP 页面时,它出现了,但似乎编写图像
我在使用 JApplet 时遇到问题。代码工作得很好,但是当我将其从 JFrame 转换为 JApplet 时,渲染部分停止正常工作。基本上我想做的是简单化的绘图应用程序。当启动小程序时,有一半的时间
我已经制作了一个 Applet 的蓝图,其功能尚未插入。因此,我在类中实例化了各种类型的数组,然后在 init() 函数中进一步实例化了它们的类型。但后来,我在构造函数 GPACalculator 中
import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public
我想构建一个 JApplet 应用程序,包含 5 个页面。一页中的给定数据将被输入到下一页。根据给定的数据,我必须在下一页显示不同的选项。 我完全不熟悉 applet,之前使用过 Web 应用程序。我
我尝试从一个类中创建两个移动对象。我想控制它们,但 JApplet 不打开线程。 我的代码: import java.awt.Graphics; import javax.swing.JApplet;
Applet 以前可以用,现在不行了。我对 GUI 的小程序不是很熟悉。这是 GUI 代码: import javax.swing.*; import java.awt.*; import java.
我是一名优秀的程序员,十分优秀!