2012-09-03 43 views
2

我正在開發我的第一個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)'有什麼問題,但它不會導致任何事情發生。我會從任何歡迎大家指正,我敢肯定,我犯了一個可笑的錯誤,因爲我不是用語言很舒服......

+0

*「正在處理我的第一個Java項目......小程序」*災難配方 - 配料:1個新鮮程序員,1個小程序。說明:輕輕混合並服務。 –

+0

在這一點上,編寫命令行應用程序(或者'基於JFrame的應用程序 - 如果你感覺很勇敢的話)「會更好。 –

回答

4

這不是一個applet程序 - 是的,它擴展JApplet的,但沒有init方法,而是有一個main方法 - 一種不會在applet程序中調用的方法。在做其他事情之前,請先通過JApplet Tutorial

其他建議:

  • 同樣,你需要一個適當的init()覆蓋。
  • 不要直接在JApplet或任何頂層Window組件中繪製。
  • 取而代之的是JApplet中顯示的JPanel的paintComponent(...)方法。
  • 不要在paint(...)paintComponent(...)方法中設置背景顏色或更改GUI的狀態。
  • 你要打電話給你paintComponent(...)方法覆蓋內的super.paintComponent(...)方法,而通常這是你的方法的第一個方法調用。
  • 同時你會想讀在同一地點,因爲它會幫助你很多 Swing的圖形教程。
  • 如果不是一個小程序,你想創建一個桌面應用程序,然後把你的你的攻擊會在一點點不同,你不會有一個init()方法重寫,你將需要一個主要方法,而你將您的組件放入JFrame作爲您的頂級窗口。
  • 爲了獲得更好的幫助,請在您的問題中發表較少的藉口和道歉(我已經刪除了大部分內容),而是專注於以儘可能清晰的方式提供有用的信息。我們對您的情況不感興趣,因爲我們有興趣幫助解決您的問題,所以請幫助我們幫助您。

運氣!

+2

+1,特別是最後一點。 –

+0

我的感覺比我一個小時前的可憐少得多。感謝指針! – KebertX

+0

@Kebert:不客氣,祝你好運! –

相關問題