循环列表gui

时间:2014-02-28 10:14:26

标签: java swing loops user-interface

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

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();
    }
}

1 个答案:

答案 0 :(得分:2)

试试beu == second。您需要连接*。您目前只使用一个 *

设置文字
    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代表空格,另一个代表星星。试试这个

  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();
        }
    }