2014-03-05 55 views
0

你好我想嘗試做一些forloops,我需要建立星號,但我不知道如何打印到我的JList我打印3模式星號循環,但我需要打印最後一個d單選按鈕,將在這裏處理for循環是到目前爲止我的代碼麻煩在JList星號打印

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將輸出使用for循環

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

這是這個星號模式我的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

先生,我在打印星號表單時遇到了麻煩。 – Batusai

+1

您應該停止刪除問題的正文,就像您在[此處](http://stackoverflow.com/q/22092196/2587435)一樣。這個Q&A格式不僅適用於你的海報,也適用於所有其他同樣出現問題的人。通過刪除所有內容,你1)可能會使答案失效2)不允許讀者看到你所犯的錯誤,也許與他們的錯誤相比。我要把它推回去,和另一個一樣。如果你不想讓你的整個程序發佈 –

回答

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

輸出

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