我正在開發我的第一個Java項目,而且我一直很困惑。這將打開一個對話框,以獲取0-255之間的數字,檢查它是一個整數並且它在範圍內,然後使用int爲圖形小程序的背景製作灰色陰影。我擁有它應該做的一切!但它不繪製小程序。程序在上次調用JOptionPane後終止。爲什麼此java applet在繪製圖形之前退出?
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.Color;
@SuppressWarnings("serial")
public class DrawingShapes extends JApplet
{
private Shade shade = new Shade();
private void getColor()
{
int rgb = 0;
boolean useful = false;
String number = JOptionPane.showInputDialog("Make this easy for me.\n"
+ "Type an integer between 0 and 255");
{
try
{
rgb = Integer.parseInt(number);
if (rgb > 0 && rgb < 255)
{
useful = true;
shade.setColor(rgb);
}
else
{
useful = false;
number = JOptionPane.showInputDialog(null, "\"" + number + "\""
+ " is not between 0 and 255!\n"
+ "Lrn2 be doin' it right!");
}
}
catch (NumberFormatException nfe)
{
number = JOptionPane.showInputDialog(null, "\"" + number + "\""
+ " is not an integer!\n"
+ "Lrn2 be doin' it right!");
}
}
if (useful)
{
JOptionPane.showMessageDialog(null, rgb + " will be the shade of gray.");
//WHEN this message is closed, the program seems to quit.
//System.exit(0);
}
}
public static void main(String[] args)
{
new DrawingShapes().getColor();
}
public class Shade
{
private int color;
public void setColor(int col)
{
color = col;
System.out.println("color: " + color);
System.out.println("col: " + col); //IT prints these lines....
}
public void paint (Graphics g) //Everything after this is sadly ignored.
{
int size = 500;
setSize(size, size);
int rgb = color;
Color gray = new Color(rgb,rgb,rgb);
setBackground(gray);
System.out.println(rgb + " This should be the third time");
g.drawOval(0, 0, size, size);
}
}
}
我找不出'public void paint(Graphics g)'有什麼問題,但它不會導致任何事情發生。我會從任何歡迎大家指正,我敢肯定,我犯了一個可笑的錯誤,因爲我不是用語言很舒服......
*「正在處理我的第一個Java項目......小程序」*災難配方 - 配料:1個新鮮程序員,1個小程序。說明:輕輕混合並服務。 –
在這一點上,編寫命令行應用程序(或者'基於JFrame的應用程序 - 如果你感覺很勇敢的話)「會更好。 –