JApplet Textfield / jbutton /嵌套循环程序,创建一个墙,在textfield中输入行数

时间:2012-07-28 01:32:02

标签: java swing paintcomponent japplet

我目前正在使用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();
    }   
}

1 个答案:

答案 0 :(得分:5)

你的绘画方法中有一些不好的代码,包括:

  • 您的paint(...)方法中有一段时间(真实)循环会锁定您的GUI并阻止它响应任何
  • 您正尝试从paint方法调用的方法中读取JTextField。你不应该在你的绘画代码或它调用的方法中有程序逻辑。
  • 您不应该首先覆盖J paint(...) JApplet,而应该覆盖JApplet所持有的JPanel中的paintComponent(...)
  • 考虑添加代码来读取actionPerformed方法中的JTextField,因为这似乎是此逻辑的最佳位置。

修改

  • 您的paint方法永远不会被调用,因为它不是JApplet绘制方法的真正重载。你的有两个参数,一个paint方法应该只有一个。
  • 在actionPerformed方法中,从JTextField获取值
  • 将它转换为一个Integer.parseInt(...)而不是双倍的int,因为你永远不会画一小块砖
  • 并使用int获取设置一个int类字段,可能称为brickCount或类似的东西,然后调用repaint()。
  • 在JPanel的paintComponent(...)方法中(类似于paint应该只有一个参数Graphics),调用paintBricks(),并使用此方法使用brickCount字段值来决定要绘制的砖块数。 / LI>
  • 切勿在{{1​​}} repaint()内或从这些方法中调用的任何方法中致电paint(...)

修改2

这是一个不执行程序需要做的示例,但说明了如何从JTextField获取信息并在绘图中使用它:

paintComponent(...)

这将导致以下结果:
enter image description here enter image description here