gpt4 book ai didi

java - 在列表 gui 中循环

转载 作者:行者123 更新时间:2023-11-30 04:00:34 25 4
gpt4 key购买 nike

大家好,我对第一个 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();
}
}

最佳答案

为你的beu == second尝试一下这个。您需要连接 *。您目前仅使用 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);
}
}

输出

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

为此

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

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

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

关于java - 在列表 gui 中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22092196/

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