JList星号打印出现问题

时间:2014-03-05 08:42:47

标签: java swing user-interface

你好我们正在尝试做一些我需要在星号中构建的forloops但是我不知道如何将它打印到我的JList中我打印了3个模式的星号循环但是我需要打印最后一个D单选按钮将处理forloop这里是我的代码到目前为止

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

public class LOOPING extends JFrame implements ItemListener,ActionListener
{
    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");
    ButtonGroup group = new ButtonGroup();
    JButton but = new JButton("Clear");
    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,440);
        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);
        but.setBounds(50,350,80,25);

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

        first.addItemListener(this);
        second.addItemListener(this);
        third.addItemListener(this);
        fourth.addItemListener(this);
        but.addActionListener(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++)
            {
                String stars = "";

                for (int j = v; j <= 5; j++)
                {
                    stars += s;
                }

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

        if (beu == third)
        {
            for (int m = 0; m <5; m++)
            {
                String stars = "";

                for (int k = 5; k > m; k--)
                {
                    stars += " ";   
                }

                for (int i = 0; i <= m; i++) 
                {
                    stars += "*";       
                }

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

            }
        }

        if(beu == fourth)
        {

        }    
    }

    public void actionPerformed(ActionEvent e)
    { 
        if(e.getSource() == but)
        {
            group.clearSelection();
            aslist.removeAllElements();
            je.setText(""); 
        }
    }

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

我想在我的D按钮中构建按钮D将使用forloop输出此星号图案

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

这是我的D radiobutton代码我需要它在星号上方

if(beu == fourth)
{
    for(int v= 1; v<=5; v++)
    {
        String ss= "";

        for(int c=6; c>v; c--)    
            ss +="*";

        System.out.print("*");
        System.out.println();

        for(int c=0; c<v; c++)
            System.out.print(" ");

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

1 个答案:

答案 0 :(得分:2)

试试这个。

   if (beu == fourth) {
        for (int i = 0; i < 5; i++) {
            String stars = "";

            for (int j = 0; j < i; j++) {
                stars += " ";
            }
            for (int k = 5; k > i; k--) {
                stars += "*";
            }

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

输出

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