gpt4 book ai didi

java - JList 星号打印中出现问题

转载 作者:行者123 更新时间:2023-12-02 06:07:00 25 4
gpt4 key购买 nike

大家好,我正在尝试做一些需要在星号中构建的 forloop,但我不知道如何将其打印到我的 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 单选按钮代码,我需要它位于星号上方

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

最佳答案

试试这个。

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

输出

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

关于java - JList 星号打印中出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22192572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com