循环列表gui [英] looping in the list gui

查看:170
本文介绍了循环列表gui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我对第一个RadioButton中JList中的循环有问题它是正确的但是在第二个示例中如果我试图做另一个星号,那么循环将会出错,这是我的代码到目前为止。

Hello to all I have a problem about the loops in the JList in the first RadioButton it is correct but in the second sample if I am trying to do another asterisk the loop will get wrong this is my code so far .

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

public class LOOPING extends JFrame implements ItemListener
{
    JFrame jeframe = new JFrame("LOOPING");
    JPanel jenel = new JPanel();
    JLabel let = new JLabel("Choose a letter");
    JRadioButton first = new JRadioButton("A");
    JRadioButton second = new JRadioButton("B");
    JRadioButton third = new JRadioButton("C");
    JRadioButton fourth = new JRadioButton("D");
    JList asterisk = new JList();
    JLabel je = new JLabel();

    DefaultListModel aslist = new DefaultListModel();

    public LOOPING()
    {
        jenel.setLayout(null);
        jeframe.setVisible(true);
        jeframe.setBounds(330,100,200,370);
        jeframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        let.setBounds(10,10,100,20);
        first.setBounds(20,50,50,30);
        second.setBounds(20,110,50,30);
        third.setBounds(100,50,50,30);
        fourth.setBounds(100,110,50,30);
        asterisk.setBounds(07,150,170,170);
        je.setBounds(07,90,110,170);

        jenel.add(let);
        jenel.add(first);
        jenel.add(second);
        jenel.add(third);
        jenel.add(fourth);
        jenel.add(asterisk);
        jenel.add(je);

        first.addItemListener(this);
        second.addItemListener(this);
        third.addItemListener(this);
        fourth.addItemListener(this);
        getContentPane().add(jenel);
        jeframe.add(jenel);
    }
    public void itemStateChanged(ItemEvent e)
    {
        ItemSelectable beu;
        beu = e.getItemSelectable();
        String s = "*";

        if(beu == first)
        {
            for(int a=0; a<=4; a++)
            {
                for(int b=1; b<a; b++)
                System.out.print(" ");

                je.setText(je.getText() + s);
                aslist.addElement(je.getText());
                asterisk.setModel(aslist);
            }

        }
        if(beu == second)
        {
            for(int v=1; v<=5; v++)
                    {
            for(int j=v; j<=5; j++)
                je.setText(s);
                System.out.println();
                                aslist.addElement(je.getText());
                                asterisk.setModel(aslist);
                    }

        }
        String boo = "*";
        if(beu == third)

        {
            for(int u=5; u>0; u--)
            {
                for(int i=u; i>0; i--)
                {
                je.setText(je.getText() + boo);
                System.out.print(" ");
                }

                aslist.addElement(je.getText());
                asterisk.setModel(aslist);
            }


        }
    }
    public static void main(String [] args)
    {
        LOOPING lup = new LOOPING();
    }
}


推荐答案

尝试这适用于 beu == second 。您需要连接 * 。您目前只使用一个设置文本 *

Try this for your beu == second. You need to concatenate the *s. You're currently only setting the text with one *

    if (beu == second) {
        for (int v = 1; v <= 5; v++) {
            String stars = "";
            for (int j = v; j <= 5; j++) {
                stars += s;
            }
            je.setText(stars);
            System.out.println();
            aslist.addElement(je.getText());
            asterisk.setModel(aslist);
        }
    }

输出

*****
****
***
**
*

对于此

    *
   **
  ***
 ****
*****

你需要两个循环,1个用于空格,另一个用于星号。试试这个

You need two loops, 1 for spaces and the other for stars. Try this

  if (beu == third) {
        for (int m = 0; m < 6; m++) {
            String stars = "";
            for (int k = 6; k >= m; k--) {
                System.out.print(" ");
                stars += " ";
            }
            for (int i = 1; i <= m; i++) {
                System.out.print("*");
                stars += "*";

            }
            je.setText(stars);
            aslist.addElement(je.getText());
            asterisk.setModel(aslist);

            System.out.println();
        }
    }

这篇关于循环列表gui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆