2013-03-03 49 views
1

My programJava- AWT Applets-如何更改我的程序中形状的大小?

该程序的设计目的是根据用户设定的形状,根据输入改变其大小。

我有两个问题。一个是用户必须输入它作为一个字符串,但大小的值是一个整数。如果我将整数转换为字符串,转换时会给我一个空例外。 (java.lang.Integer.parseInt(Unknown Source)Exception)。)

另一个问题是我不知道要在actionPerfomed方法中添加什么。由于所有的信息只需要使用paint方法。我如何将整数值传输到Paint方法中。

import java.io.*; 
import java.util.*; 
import java.text.*; 

import java.awt.*; 
import java.awt.event.*; 
import java.applet.*; 
import java.applet.Applet; 


public class ChangeSize extends Applet 
    implements ActionListener 
{ 
    Button bttn1 = new Button ("Circle"); 
    Button bttn2 = new Button ("Square"); 
    Button bttn3 = new Button ("Triangle"); 
    Button bttn4 = new Button ("Rectangle"); 

    Label lab1; // text within applet 
    TextField t1; // where user inputs text 
    String input; 

    int choice; 

    public void init() 
    { 
     this.setSize (500, 300); 

     setBackground (Color.lightGray); 

     lab1 = new Label ("Insert the size of the shape:"); 
     //int Size = Integer.parseInt (input); 
     add (lab1); 
     t1 = new TextField(); 
     add (t1); 

     bttn1.addActionListener (this); // circle button 
     bttn2.addActionListener (this); // square button 
     bttn3.addActionListener (this); // triangle button 
     bttn4.addActionListener (this); // rectangle button 

     add (bttn1); 
     add (bttn2); 
     add (bttn3); 
     add (bttn4); 

    } 
    public void paint (Graphics g) 
    { 
     int xpoints[] = {25, 145, 25, 145, 25}; \ 
     int ypoints[] = {25, 25, 145, 145, 25}; 
     int npoints = 5; 

     switch (choice) 
     { 
      case 1: 
       if (choice == 1) 
        g.setColor (Color.red); 
       g.fillOval (30, 40, 20, 20); // I want it to be (30,40, 20, size). Instead 


      case 2: 
       if (choice == 2) 
        g.fillRect (20, 40, 100, 100); 
      case 3: 
       if (choice == 3) 
        g.fillPolygon (xpoints, ypoints, npoints); 
      case 4: 
       if (choice == 4) 
        g.fillRect (20, 40, 50, 100); 

       break; 
     } 

     showStatus ("Please seclect an option."); 
    } 
    public void actionPerformed (ActionEvent evt) 
    { 
     if (evt.getSource() == bttn1) 
      choice = 1; 
     else if (evt.getSource() == bttn2) 
      choice = 2; 
     else if (evt.getSource() == bttn3) 
      choice = 3; 
     else if (evt.getSource() == bttn4) 
      choice = 4; 

      Size = t1.getText(); 

      // I dont know what to put here 

     repaint(); 
    } 

} 
+0

为什么在2013年AWT? Swing是第3个千年(开始时)使用的GUI工具包。 'this.setSize(500,300);'不要在applet中执行此操作(无论是Applet ***还是*** JApplet),大小设置为HTML(可能由JavaScript编写)。 – 2013-03-03 02:03:09

回答

2

您已接近解决方案,您的评论// I want it to be (30,40, 20, size). Instead 是关键。

  • 给你的应用程序中的int类或多个字段来保存您的图纸尺寸。
  • 使用该尺寸在绘画例程中设置形状的大小。
  • 允许用户更改actionPerformed方法中的尺寸。
  • 您得到的文本将是一个字符串,并且您需要将其解析为一个int,然后才能通过Integer.parseInt(...)方法使用它。

关于:

我有两个问题。一个是用户必须将其作为字符串输入,但大小值是整数。如果我将整数转换为字符串,转换时会给我一个空例外。 (java.lang.Integer.parseInt(Unknown Source)Exception)。)

问题是你试图解析int的地方。您在init方法中执行此操作,该方法是在用户有时间将输入添加到文本字段之前创建并构建小程序的方法,该方法为方式。在actionPerformed方法中分析会更好。