Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在按下按钮时运行包含for循环的方法_Java - Fatal编程技术网

Java 如何在按下按钮时运行包含for循环的方法

Java 如何在按下按钮时运行包含for循环的方法,java,Java,我正在尝试制作一个简单的GUI应用程序,当您在文本字段中输入一个整数,即“w”时,它被放入for循环,循环运行“w”次。当它运行时,我希望它在每次循环运行时打印一个“X”。希望当您看到代码时,它会更有意义。我们将非常感谢您的帮助 提前谢谢 import java.awt.event.*; import javax.swing.*; import javax.swing.text.JTextComponent; public class Main { public static void ma

我正在尝试制作一个简单的GUI应用程序,当您在文本字段中输入一个整数,即“w”时,它被放入for循环,循环运行“w”次。当它运行时,我希望它在每次循环运行时打印一个“X”。希望当您看到代码时,它会更有意义。我们将非常感谢您的帮助

提前谢谢

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

public class Main {

public static void main(String[] args) {
    JFrame f = new JFrame();// creating instance of JFrame

    int w = 0;

    JTextField textfield = new JTextField();
    JTextArea textarea = new JTextArea(6, 37);
    JButton bSquare = new JButton("Square");// creating instance of JButton
    JButton bRATriangle = new JButton("Right Angle Triangle");
    JButton bETriangle = new JButton("Equilateral Triangle");

    bSquare.setBounds(0, 100, 200, 40);
    bRATriangle.setBounds(200, 100, 200, 40);
    bETriangle.setBounds(400, 100, 200, 40);
    textarea.setBounds(0, 500, 600, 100);
    textfield.setBounds(200, 200, 200, 80);

    textfield.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try{
                int w = Integer.parseInt(textfield.getText());
                textarea.setText(String.valueOf(w));
            } catch(NumberFormatException nfe){
                textarea.setText("Error!");
            } 
        }
    });

    bSquare.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for(int j = 0; j < w; j++) {
                textarea.append("");
                for(int i = 0; i < w; i++) {
                    textarea.append("X");
                }
            }
        }
    });
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.text.JTextComponent;
公共班机{
公共静态void main(字符串[]args){
JFrame f=new JFrame();//正在创建JFrame的实例
int w=0;
JTextField textfield=新的JTextField();
JTextArea textarea=新的JTextArea(6,37);
JButton bSquare=newjbutton(“Square”);//正在创建JButton的实例
JButton bRATriangle=新JButton(“直角三角形”);
JButton bETriangle=新JButton(“等边三角形”);
b方形立根(0,100,200,40);
立根角(20010020040);
三棱角立根(40010020040);
textarea.setBounds(0,500,600,100);
textfield.setBounds(20020020080);
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
试一试{
int w=Integer.parseInt(textfield.getText());
textarea.setText(String.valueOf(w));
}捕获(NumberFormatException nfe){
setText(“错误!”);
} 
}
});
bSquare.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
对于(int j=0;j
我只需要为
bSquare
按钮执行操作侦听器中的所有逻辑。此外,您不需要使用
int w=0;
,因为它不会在操作侦听器之间正确共享

示例

bSquare.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        // just use a new variable here
        // default to 0 (if a NumberFormatException occurs)
        int val = 0;
        try
        {
            val = Integer.parseInt(textfield.getText());
            textarea.setText(String.valueOf(val));
        }
        catch (NumberFormatException nfe)
        {
            textarea.setText("Error!");
        }

        for (int j = 0; j < val; j++)
        {
            textarea.append("");
            for (int i = 0; i < val; i++)
            {
                textarea.append("X");
            }
        }
    }
});
bSquare.addActionListener(新ActionListener()
{
@凌驾
已执行的公共无效操作(操作事件e)
{
//在这里使用一个新变量
//默认值为0(如果发生NumberFormatException)
int-val=0;
尝试
{
val=Integer.parseInt(textfield.getText());
textarea.setText(String.valueOf(val));
}
捕获(NumberFormatException nfe)
{
setText(“错误!”);
}
对于(int j=0;j
一定要描述你遇到的问题,或者问你有什么具体问题。@jornverne抱歉,我想我已经讲到了我需要的内容。我需要知道当按下按钮时如何进行for循环,然后如何将文本打印到textarea。编辑你的帖子。然后对textfield和textarea使用
final
,并在Actio中nListener of bSquare您应该使用textfield.getText()来检索输入的值,w是不“可见”的。当然,您必须将其解析为IntegerIt。在我看来,您已经在做这些事情了。现在,我可以发现错误(尽管这也可能是问题中的输入错误,所以我只能猜测),但其他人可能不会,所以包含这样的内容很重要。太棒了!非常感谢!但是如何创建换行符?相当于使用System.out.println();?没问题。对于换行符,可以使用:
textarea.append(System.lineSeparator());