2012-09-02 36 views
0

我刚开始学习java。它有点整洁,但我用我想象中的那种痛苦简单的东西撞墙。这里是我工作的一部分:检查并使用对话框输入时出现问题

import javax.swing.JApplet; 
import javax.swing.JOptionPane; 
import java.awt.Graphics; 
import java.awt.Color; 

@SuppressWarnings("serial") 
public class DrawingShapes extends JApplet 
{ 
    public static void main(String [] args) 
    { 
    int rgb = 0; 
    boolean useful = false; 
    String number = JOptionPane.showInputDialog("Make this easy for me.\n" 
    +"Type an integer between 0 and 255"); 
    do 
    { 
     try 
     { 
     rgb = Integer.parseInt(number); 
     if (rgb > 0 && rgb < 255) 
     { 
      useful = true; 
     } 
     else 
     { 
      useful = false; 
     } 
     } 
     catch (NumberFormatException nfe) 
     { 
     number = JOptionPane.showInputDialog(null, "\"" + number + "\"" 
     + " is not an integer between 0 and 255!\n" 
     + "Lrn2 be doin' it right!"); 
     } 
    } 
    while (!useful); 
    JOptionPane.showMessageDialog(null, "The integer is " + rgb); 
    } 
} 

的想法是,该对话框要求输入一个数字,如果它不是一个数字显示错误,并再次询问。实际上,现在很好用。然而,我想要的是使用该数字(rgb)创建一个灰色阴影,用于此类之外的小程序。

Color shade = new Color(rgb,rgb,rgb); 

这不起作用,因为它无法读取另一个类中的变量。或者,它可能可以,但我想我需要帮助弄清楚如何。

总之,我需要阅读输入,验证它,并且仍然可以在以后使用它。我在这个目标上已经有所欠缺了,所以我来寻求指导。

编辑:丹建议真的帮助我的变量,但现在我有一个问题,无法绘制图形。该对话框在退出对话框后立即关闭。所以我仍然有点难住。我只需要绘制一些简单的形状,使用背景输入对话框中的颜色。代码的后半段,现在看起来是这样的:

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); 
    } 
    public void paint (Graphics canvas) 
    { 
     int col = color; 
     Color gray = new Color(col,col,col); 
     setBackground(gray) 
     setSize(500, 500); 
     canvas.drawOval(0, 0, 500, 500); 
     canvas.draw....[blablabla other shapes] 
    } 
    } 

它完全跳过了漆的选择,除非我注释掉一切导致它...

+0

永远不会改变涂料循环中组件的状态! – kleopatra

回答

0

请参阅下面的代码潜在的解决方案。我创建了一个名为Shade的新类,它有一个私有字段,可以使用公共setter方法从另一个类(您的类或任何其他类)设置。

请注意,我改变了你的班级,因为它是不是很好,在主方法,这是静态的一切。

import javax.swing.JApplet; 
import javax.swing.JOptionPane; 

public class DrawingShapes extends JApplet { 
    private Shade shade = new Shade(); 

    private void readColor() { 
    int rgb = 0; 
    boolean useful = false; 
    String number = JOptionPane.showInputDialog("Make this easy for me.\n" + "Type an integer between 0 and 255"); 
    do { 
     try { 
     rgb = Integer.parseInt(number); 
     if (rgb > 0 && rgb < 255) { 
      useful = true; 
      shade.setColor(rgb); 
     } else { 
      useful = false; 
     } 
     } catch (NumberFormatException nfe) { 
     number = JOptionPane.showInputDialog(null, "\"" + number + "\"" + " is not an integer between 0 and 255!\n" 
      + "Lrn2 be doin' it right!"); 
     } 
    } while (!useful); 
    JOptionPane.showMessageDialog(null, "The integer is " + rgb); 
    } 

    public static void main(String[] args) { 
    new DrawingShapes().readColor(); 
    } 

    public class Shade { 
    private int color; 

    public void setColor(int col) { 
     color = col; 
     System.out.println("The chosen color shade is " + color); 
    } 

    } 
} 
+0

感谢您的评论!我的问题与变量似乎解决了,现在我只需要让图形工作吧! – KebertX