将字符串从JLabel数组存储到字符串数组

时间:2019-07-14 16:14:59

标签: java arrays string swing jlabel

我正在努力制作一个小型程序,需要执行以下操作:

  1. 右键单击面板,然后单击“添加”选项。它需要在JLabel ArrayList内部动态添加JLabel。
  2. 同时将JLabel文本存储在字符串的ArrayList中。
  3. 打印该arrayList以测试它是否成功将文本存储在数组中。
  4. 稍后,我将以某种方式进一步保存该文本。到目前为止不是问题。

我被困住了,因为我似乎不知道如何将文本从一个数组转移到另一个数组。

那怎么办?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.MenuElement;

public class A {

    JFrame frame;
    Toolkit toolkit;
    JPopupMenu quickMenu;
    JTextField field1;
    JPanel panel;

    ArrayList<JLabel> labelList;
    ArrayList<String> stringArray;
    String texts;

    JLabel label1, label2, label3;
    ArrayList<JTextField> fieldList;
    int count;

    A() {

        frame = new JFrame("A class");

        panel = new JPanel();
        panel.setLayout(new GridLayout(2, 3, 5, 5));

        JLabel title1 = new JLabel("Title 1");
        panel.add(title1);

        setDimm(frame);

        quickMenu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add row");
        JMenuItem removeRow = new JMenuItem("Remove row");

        quickMenu.add(addRow);
        quickMenu.add(removeRow);
        panel.add(quickMenu);

        panel.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == e.BUTTON3) {
                    quickMenu.show(e.getComponent(), e.getX(), e.getY());
                }

            }
        });

        labelList = new <JLabel>ArrayList();

        // ADD ROWS
    count = 0;
    stringArray = new ArrayList();

    addRow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("Starting count is: " + count); // check if
                                                                // count
            JLabel label = new JLabel ("" + count);                                                 // starts
                                                                // correctly
            labelList.add(label); // adds new
                                                            // JLabels in
                                                            // the labelList
                                                            // array

            // I suspect the problem is here underneath, not sure how to
            // first get the texts inside the arraylist of JLabels and then
            // transfer this text to the array of Strings that I want to
            // print.
            for (int i = 0; i < labelList.size(); i++) {

                stringArray.add(labelList.get(i).getText());

            }

            panel.add(labelList.get(count));
            panel.revalidate();
            panel.repaint();

            count++;

            System.out.println("After Click count is: " + count);
            System.out.println("Size of JLabel Array labelList is: " + labelList.size());
            System.out.println("Size of String Array stringArray is: " + stringArray.size());               
            System.out.print("The content of the String ArrayList is: " + stringArray);
            System.out.println();
            System.out.println();               


        }

    });

        removeRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev)

            {
                /*
                 * panel.remove(labelList.size() - 1); // Will it delete the
                 * last JLabel from the panel?
                 * labelList.remove(labelList.size()-1);
                 * 
                 * 
                 * panel.revalidate(); panel.repaint(); count--;
                 */
                System.out.println(count + " and does nothing yet");

            }

        });

        frame.add(panel);

        // panel.add(button);
        frame.setVisible(true);
    }

    void setDimm(JFrame frame) {
        this.frame = frame;
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.gray);
        frame.setSize(800, 600);
        toolkit = frame.getToolkit();
        Dimension size = toolkit.getScreenSize();
        frame.setLocation((size.width - frame.getWidth()) / 2, (size.height - frame.getHeight()) / 2);

    }

    public static void main(String[] args) {
        new A();
    }
}

3 个答案:

答案 0 :(得分:1)

您使用此代码将文本添加到ArrayList:

stringArray.add(labelList.get(i).toString());

对此替换为标签文本:

stringArray.add(labelList.get(i).getText());

您也不会一直遍历labellist,仅实例新的Label,将其添加到labellist,将label的文本添加到字符串列表:

JLabel label = new JLabel("" + count);
                                                // correctly
labelList.add(label); // adds new
                                                // JLabels in
                                                // the labelList
                                                // array

// I suspect the problem is here underneath, not sure how to
// first get the texts inside the arraylist of JLabels and then
// transfer this text to the array of Strings that I want to
// print.
stringArray.add(label.getText());

答案 1 :(得分:1)

  

你好我也尝试过,显然现在迭代了字符串数组多次。为了进行调试,我添加了println来知道两个数组的长度,并且当JLabel数组从1,2,3,4,5正确开始时,字符串1变为1,3,6,10,15。下面的示例:JLabel Array labelList的大小是:6字符串数组的大小stringArray是:15 ArrayList是:[0,0,1,0,1,2,2,0,1,2,3,0,1,2,3 ,4]。我已经用此错误更新了初始代码

是,仅替换此代码:

for (int i = 0; i < labelList.size(); i++) {

    stringArray.add(labelList.get(i).getText());

} 

与此:

stringArray.add(label.getText());

答案 2 :(得分:0)

由Abra和ZamarianPatrick解决。如果有人可能正在搜索此解决方案,我将发布代码。太简单了...谢谢大家!!!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

public class A {

    JFrame frame;
    Toolkit toolkit;
    JPopupMenu quickMenu;
    JTextField field1;
    JPanel panel;

    ArrayList<JLabel> labelList;
    ArrayList<String> stringArray;
    String texts;

    JLabel label1, label2, label3;
    ArrayList<JTextField> fieldList;
    int count;

    A() {

        frame = new JFrame("A class");

        panel = new JPanel();
        panel.setLayout(new GridLayout(2, 3, 5, 5));

        JLabel title1 = new JLabel("Title 1");
        panel.add(title1);

        setDimm(frame);

        quickMenu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add row");
        JMenuItem removeRow = new JMenuItem("Remove row");

        quickMenu.add(addRow);
        quickMenu.add(removeRow);
        panel.add(quickMenu);

        panel.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == e.BUTTON3) {
                    quickMenu.show(e.getComponent(), e.getX(), e.getY());
                }

            }
        });

        labelList = new <JLabel>ArrayList();

        // ADD ROWS
        count = 0;
        stringArray = new ArrayList();

        addRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.out.println("Starting count is: " + count); // check if
                texts = "" + count; // count
                JLabel label = new JLabel(texts); // starts
                // correctly
                labelList.add(label); // adds new
                                        // JLabels in
                                        // the labelList

                stringArray.add(label.getText());

                panel.add(labelList.get(count));
                panel.revalidate();
                panel.repaint();

                count++;

                System.out.println("After Click count is: " + count);

                System.out.println("Size of JLabel Array labelList is: " + labelList.size());
                System.out.println("Size of String Array stringArray is: " + stringArray.size());
                System.out.print("The content of the String ArrayList is: " + stringArray);
                System.out.println();
                System.out.println();

            }

        });

        removeRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev)

            {
                /*
                 * panel.remove(labelList.size() - 1); // Will it delete the
                 * last JLabel from the panel?
                 * labelList.remove(labelList.size()-1);
                 * 
                 * 
                 * panel.revalidate(); panel.repaint(); count--;
                 */
                System.out.println(count + " and does nothing yet");

            }

        });

        frame.add(panel);

        // panel.add(button);
        frame.setVisible(true);
    }

    void setDimm(JFrame frame) {
        this.frame = frame;
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.gray);
        frame.setSize(800, 600);
        toolkit = frame.getToolkit();
        Dimension size = toolkit.getScreenSize();
        frame.setLocation((size.width - frame.getWidth()) / 2, (size.height - frame.getHeight()) / 2);

    }

    public static void main(String[] args) {
        new A();
    }
}