1

我目前正在使用JCreator,但找不到我的代码有什么问题,由于某种原因它没有读取我在JTextField. 我不打算彻底改变我的代码,如果 any1 可以指出我做错了什么,或者给我一些代码示例来说明它应该是什么样子,那就太好了。同样,当他们做同样的事情时,不要寻找“这比这个更好”。

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Wall extends JApplet implements ActionListener {

    double count;
    Boolean Submit = false;
    JButton btn1;
    JTextField  tf,field;
    int x = 0;
    int y = 575;
    int x1 = 50;
    int y1 = 25;
    int textnumber;
    Random randomNum = new Random();//initialize random variable
    int count2;

    public void init() {

        setLayout( new FlowLayout( ) );
        tf = new JTextField(5);
        field = new JTextField( "<===== Enter Brick Rows 1-20", 16 );   
        add( tf );  
        add( field );
        btn1 = new JButton("Submit");
        add(btn1);
        btn1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event){

        if (event.getSource()== btn1){
            Submit = true;
        }
    }

    public void paint(Graphics g, double BrickNumbers) {

        super.paint(g);
        while(Submit == true){
            DrawBrick(g,BrickNumbers);
        }
    }

    public void DrawBrick(Graphics g, double BrickNumbers){

        String Value = tf.getText();
        BrickNumbers = Double.parseDouble(Value);

        if(Submit == true){
            count = BrickNumbers;
            for(double count = BrickNumbers; ((count>=1) && (count <=20)); count--){

                int d = 1+ randomNum.nextInt(255);//get d variable
                int e = 1+ randomNum.nextInt(255);//get e variable
                int f = 1+ randomNum.nextInt(255);//get f variable
                Color randomcolor = new Color(d,e,f);
                g.setColor(randomcolor);
                g.fillRect(x, y, x1, y1);
                g.fillRect(x+ 50, y, x1, y1);
                g.fillRect(x+100, y, x1, y1);
                g.fillRect(x+150, y, x1, y1);
                g.fillRect(x+200, y, x1, y1);
                g.fillRect(x+250, y, x1, y1);   
                y = y - 25;
            }
        }
        repaint();
    }   
}
4

1 回答 1

5

You've got some bad code in your painting method including:

  • you've got a while (true) loop in your paint(...) method which will lock your GUI and prevent it from responding to anything.
  • You're trying to read in from the JTextField in a method called from the paint method. You should never have program logic in your painting code or in methods it calls.
  • You shouldn't be overriding paint(...) of a JApplet to begin with but rather paintComponent(...) in a JPanel that the JApplet holds.
  • Consider adding code to read the JTextField(s) in your actionPerformed method as that seems to be the best place for this logic.

Edit

  • Your paint method won't ever get called since it isn't a true overload of JApplet's paint method. Yours has 2 parameters and a paint method should only have one.
  • In your actionPerformed method, get the value from the JTextField,
  • convert it into an int with Integer.parseInt(...) not a double since you're never going to draw a fraction of a brick
  • and with the int obtained set an int class field, perhaps called brickCount or something like that, and then call repaint().
  • In your JPanel's paintComponent(...) method (which like paint should only have one parameter, Graphics), call paintBricks(), and have this method use the brickCount field value to decide how many bricks to paint.
  • Never call repaint() from within paint(...) paintComponent(...) or from any methods called from within these methods.

Edit 2

Here's an example that doesn't do what your program needs to do, but illustrates how to get information from a JTextField and use it in a drawing:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleApplet extends JApplet {
   @Override
   public void init() {
      getContentPane().add(new SimpleAppletMainPanel());
   }
}

class SimpleAppletMainPanel extends JPanel {
   private static final Color CIRCLE_COLOR = Color.red.darker();
   private static final int CIRCLE_STROKE_WIDTH = 10;
   private static final int GAP = 3;
   private static final Stroke CIRCLE_STROKE = new BasicStroke((float)CIRCLE_STROKE_WIDTH);
   private JTextField textField = new JTextField(5);
   private JButton myButton = new JButton("Submit");
   private int count = 0;

   public SimpleAppletMainPanel() {
      ActionListener actionListener = new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
            try {
               count = Integer.parseInt(textField.getText());
               repaint();
            } catch (NumberFormatException e) {
               e.printStackTrace();
            }
         }
      };
      myButton.addActionListener(actionListener);
      textField.addActionListener(actionListener);
      add(new JLabel("Enter a number, 1-10:"));
      add(textField);
      add(myButton);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      for (int i = 0; i < count; i++) {
         drawCircle(g, i);
      }
   }

   private void drawCircle(Graphics g, int layer) {
      int centerX = getWidth() / 2;
      int centerY = getHeight() / 2;
      int radius = layer * (CIRCLE_STROKE_WIDTH + GAP) + GAP;
      int x = centerX - radius ;
      int y = centerY - radius;

      Graphics2D g2b = (Graphics2D) g.create();
      g2b.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      g2b.setStroke(CIRCLE_STROKE);

      g2b.setColor(CIRCLE_COLOR);
      g2b.drawOval(x, y, radius * 2, radius * 2);

      g2b.dispose();
   }
}

This will result in the following:
enter image description hereenter image description here

于 2012-07-28T01:43:19.780 回答