gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:25:24 27 4
gpt4 key购买 nike

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

  1. 右键单击面板,单击“添加”选项。它需要在 JLabel ArrayList 中动态添加 JLabels。
  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();
}
}

最佳答案

Hello. I tried also that and apparently now it iterates multiple times the array of Strings. For the purpose of debugging I have added the println to know the length of both arrays and the string one goes 1,3,6,10,15 while the JLabel array is going correctly from 1,2,3,4,5. Example underneath: Size of JLabel Array labelList is: 6 Size of String Array stringArray is: 15 ArrayList is: [0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4] . I have updated initial code with this error

是的,只需替换此代码:

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

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

}

这样:

stringArray.add(label.getText());

关于java - 将字符串从 JLabel 数组存储到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029090/

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