我正在开发一个我正在创建GUI翻书的项目。我想要做的是在文本区域放置一些文本。然后去一个干净的幻灯片。加入更多文字。我保存了所有"幻灯片"在数组列表中。我在GUI上有按钮,允许用户在幻灯片之间来回切换。当我点击"之前的"我遇到了一个错误。按钮。我在线程' AWT-EventQueue-0'中得到了一个"异常。 java.util.ConcurrentModficationException"在那之下有更多的错误。
这段代码仍处于严谨的构建状态,因此其中一些有点粗糙。类的名称有点奇怪 - 我正在修改教科书中的GUI程序版本。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//import java.util.Scanner;
import java.util.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class MetricConverter extends JFrame {
private JPanel panel; //To reference a panel
private JLabel messageLabel; //To reference a label
private JTextArea kiloTextField; //To refrence a text field - this will become the JtextArea
private JButton Previous;
private JButton Menu;
private JButton Load;
private JButton Save;
private JButton Next;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 400;
LinkedList ll = new LinkedList();//Created Linked List
ArrayList al = new ArrayList();//Create an array list
Iterator itr = al.iterator();
ListIterator litr = al.listIterator();
public MetricConverter()
{
setTitle("Java Flipbook");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();//Build the panel and add it to the frame
add(panel);//Add the panel to the frame's content pane
setVisible(true);//Display the window
}
/* The build panel label method adds a label, a text field, and a button to a panel */
private void buildPanel()
{ //create the buttons
messageLabel = new JLabel("Make a flipbook!");
kiloTextField = new JTextArea(10,15);//Create the canvas
//Create a button for the Previous Slide
Previous = new JButton("Previous");
Previous.addActionListener(new PrevButton());
//Create a button to bring up the menu
Menu = new JButton("Menu");
Load = new JButton("Load");
Save = new JButton("Save");
//Create a button for the Next Slide
Next = new JButton("Next");
Next.addActionListener(new Next());
//Create a JPanel object and let the panel field reference it
panel = new JPanel();
//Add the label, text field, and button components to the panel.
//panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(Previous);
// panel.add(Menu);
panel.add(Load);
panel.add(Save);
panel.add(Next);
} //end buildPanel class
private class Next implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = kiloTextField.getText();
al.add(s);//add to array list
//ll.add(s);//add whatever is in the JtextArea to the linked list
kiloTextField.setText(null);//"clear" the canvas
}
}
private class PrevButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = kiloTextField.getText();//
//Object element = litr.previous();
//System.out.print(element);
//System.out.print(ll);
System.out.println(litr.previous());
}
}
// Main Method
public static void main(String[] args)
{
new MetricConverter();
}
}
答案 0 :(得分:0)
要启用它,您需要在类中声明一个Integer变量(如全局),以跟踪光标的位置。这将使您能够移动到前一个和前一个,直到第一个。
public class MetricConverter extends JFrame {
private JPanel panel; //To reference a panel
private JLabel messageLabel; //To reference a label
private JTextArea kiloTextField; //To refrence a text field - this will become the JtextArea
private JButton Previous;
private JButton Menu;
private JButton Load;
private JButton Save;
private JButton Next;
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 400;
LinkedList ll = new LinkedList();//Created Linked List
ArrayList al = new ArrayList();//Create an array list
Iterator itr = al.iterator();
ListIterator litr = al.listIterator();
int previousIndex = 0; //<====================================Changes made here
public MetricConverter() {
//no changes were made here
}
/* The build panel label method adds a label, a text field, and a button to a panel */
private void buildPanel() { //create the buttons
//no changes were made here
} //end buildPanel class
private class Next implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = kiloTextField.getText();
al.add(s);//add to array list
litr = al.listIterator(); //<====================================Changes made here
//ll.add(s);//add whatever is in the JtextArea to the linked list
kiloTextField.setText(null);//"clear" the canvas
//navigates to the last Object in the list
while (litr.hasNext()) { //<====================================Changes made here
litr.next();
}
//keeps track of where the cursor is
previousIndex = litr.previousIndex();//<====================================Changes made here
}
}
private class PrevButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = kiloTextField.getText();//
//Object element = litr.previous();
//System.out.print(element);
//System.out.print(ll);
//System.out.println("arr : "+al.toString());
if (previousIndex > -1) { //<====================================Changes made here
litr = al.listIterator(previousIndex+1);//sets the cursor to the previous object's index
System.out.println(litr.previous());
previousIndex--;
}
}
}
// Main Method
public static void main(String[] args) {
new MetricConverter();
}
}
注意我在哪里进行了更改。
答案 1 :(得分:-1)
发生错误是因为您有一个迭代器,但是在创建它之后直接修改它的列表。您只应在迭代后通过迭代器修改列表。
一般来说,将Iterator作为一个类的成员是没有意义的。你需要重新考虑这一切。