首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组中标签的修改

数组中标签的修改
EN

Stack Overflow用户
提问于 2016-04-26 12:48:38
回答 1查看 352关注 0票数 0

我们希望能够更改JLabel数组上的文本/图像,但是我们无法理解为什么它不能工作。

代码语言:javascript
复制
import java.awt.*;

class layoutWindow extends JFrame implements ActionListener {`



    String[] classStrings = { "EN1A", "EN2A", "EN3A", "EN1B", "EN2B", "EN3B",
            "ES1A", "ES2A", "ES3A", "ES1B", "ES2B", "ES3B", "H1", "H2", "H3",
            "N1A", "N1B", "N1C", "N1D", "N1E", "N2A", "N2B", "N2C", "N2D",
            "N2E","N3A","N3B","N3C","N3D","S1A","S1B","S2A","S2B","S3A","S3B" };
    String[] FormationStrings = { "3-3-3", "3-3-2", "2-4-2", "4-4", "2-2-2-2","3-4-3" };
    // Create the combo box, select item at index 4.

     JLabel[] deskLabels = new JLabel[96];



    JComboBox classList = new JComboBox(classStrings);
    JComboBox formationList = new JComboBox(FormationStrings);


    JButton seatbButton = new JButton(" randomize");
    JButton backButton = new JButton("back");
    JButton groupButton = new JButton("Make groups");

    JLabel classLabel = new JLabel("choose class");
    JLabel formationLabel = new JLabel("choose formation");

    JPanel layoutPanel = new JPanel(new GridLayout(8, 12, 1,15));
    JPanel top = new JPanel(new GridLayout(1, 2, 15,1));



    public layoutWindow() {
        super("layout Example");
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setVisible(true);
        Container contentArea = getContentPane();

        top.setVisible(true);
        layoutPanel.setVisible(true);

            int i = 1;
            for (JLabel label : deskLabels) {
                label = new JLabel();
                if ((i % 12) != 100) {
                    label.setText("" + i);
                    layoutPanel.add(label);}
                else {
                    label.setText("  ");
                    layoutPanel.add(label);
                }
                i++;
     }

        top.add(backButton);
        top.add(classLabel);
        top.add(classList);
        top.add(formationLabel);
        top.add(formationList);
        top.add(seatbButton);

        contentArea.add("North", top);
        contentArea.add("Center", layoutPanel);

        setContentPane(contentArea);
        formationList.addActionListener(this);
        classList.addActionListener(this);
        backButton.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent event) {


        String string = (String) formationList.getSelectedItem();

         if(string == "4-4"){
            deskLabels[5].setText("aasdggnmh");

         }
        }
        }

布局任务窗口

代码语言:javascript
复制
public class layouttaskwindow {

    public static void main(String[] args) {

        layoutWindow win = new layoutWindow();
    }

}

我们不明白为什么这不管用。我们使用for循环从数组中创建所有标签,但是当我们试图更改它们时,work.we并没有试图在ActionEvent中更改它,但是我们没有得到响应,请有人解释一下如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-26 13:11:27

我也认为(就像Hackerdarshi说的,我没有读过帖子,但看上去不错)问题是由前缘循环引起的。不确定,但我认为Java没有给出正确的引用。因此,要解决这个问题,您可以尝试如下:

代码语言:javascript
复制
        for (int i = 0; i < deskLabels.length; i++) {
                deskLabels[i] = new JLabel();
                if ((i % 12) != 100) deskLabels[i].setText("" + i);
                else deskLabels[i].setText("  ");
                layoutPanel.add(deskLabels[i]);
     }

正如伯杰所说的,另一个问题是以下几行:不深入的string == "4-4" (欲了解更多信息,请阅读以下内容):当您使用==时,通常会按值进行比较。但是String是对象,因此在对象上调用==将比较引用而不是其值。在Java中,String是特殊的。因此,这取决于它们如何初始化==返回的内容。所以就用

代码语言:javascript
复制
string.equals("4-4") 

如果您想比较字符串的值

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36865457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档