使用链接列表读取Java中的图像/文本文件

时间:2013-04-23 18:25:13

标签: java swing list

我正在为我的编程模块开发一个学生项目,我们必须在其中构建一个项目,以包含许多不同的Java功能,主要是GUI设计和高级数据结构,因此我正在开发一个用于Java学生培训的系统,他们将在系统中注册,使用Java中的不同教程并进行测试。我现在停留在其中一个教程中,因为我认为这是一个好主意,而不是让多个页面通过教程,当用户按下“下一步”按钮时,教程中的图像和内容将在该页面上更新或“返回键。因此,图像和文本文件的所有名称都将存储在链接列表中(因此顺序正确),教程将从索引1(开始)开始,因此将显示图像1和内容1,如用户点击下一步索引将增加1,图像和内容将更新。 我目前收到

的错误消息
java.lang.NullPointerException at gui.Tutorial1page1.imageSelect(Tutorial1page1.java:66)
at gui.Tutorial1page1.<init>(Tutorial1page1.java:69)
at gui.Tutorial1page1$1.run(Tutorial1page1.java:39)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

任何关于从哪里开始寻找或甚至不可能的建议都将非常感激。也许应该提一下整个事情只是在我添加链表之前阅读图片。谢谢: - )

package gui;

import java.awt.EventQueue;
import java.awt.TextArea;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
 import javax.swing.JButton;
import java.awt.Font;
import javax.swing.BorderFactory;
 import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;


public class Tutorial1page1 extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Tutorial1page1 frame = new Tutorial1page1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//declare the linkedlist for the images
private LinkedList<String> imageContent;

//declare int for the index of what image to select
//this will increase by one when user hits next button & decrease for back button
int index = 1;

//store the names of the files in order, add last will add them to end of list
public void Images()
{
    imageContent = new LinkedList<String>();
    imageContent.addLast("\"IfStatement.png\"");
    imageContent.addLast("\"IfElseStatement.png\"");
}

//select image
public String imageSelect(int index)
{
return imageContent.get(index);
}

String image = imageSelect(index);
/**
 * Create the frame.
 * @throws IOException 
 */
public Tutorial1page1() throws IOException {

    //create, format and locate jframe
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit program when framed closed
    setBounds(300, 75, 800, 600);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    setContentPane(contentPane);
    contentPane.setLayout(null);

    //create, format and position button for going back through pages of tutorial
    //should this be disabled on first page??
    JButton btnBack = new JButton(" < Back ");
    btnBack.setForeground(Color.WHITE);
    btnBack.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnBack.setBackground(Color.BLUE);
    btnBack.setBounds(150,500,120,30);
    contentPane.add(btnBack);

    //create, format and position button for quitting the tutorial
    JButton btnQuit = new JButton("Quit");
    btnQuit.setForeground(Color.WHITE);
    btnQuit.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnQuit.setBackground(Color.BLUE);
    btnQuit.setBounds(350,500,120,30);
    contentPane.add(btnQuit);

    //add button listener for Next button
    btnQuit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            dispose();//close current screen
            //open main screen
            try {
                Main frame = new Main(); 
                //open last page of tutorial, in this case main module page
                frame.setVisible(true);
                //open in centre of screen
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    //create, format and position button for going forward through pages of tutorial
    JButton btnNext = new JButton("Next > ");
    btnNext.setForeground(Color.WHITE);
    btnNext.setFont(new Font("Tahoma", Font.PLAIN, 20));
    btnNext.setBackground(Color.BLUE);
    btnNext.setBounds(550,500,120,30);
    contentPane.add(btnNext);

    //add button listener for Next button
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            dispose();//close current screen
            //open main screen
            try {
                Tutorial1page2 frame = new Tutorial1page2(); //open next page of tutorial
                frame.setVisible(true);
                //open in centre of screen
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    //create, format and position help button on each page of tutorial
    //uses label appearance method to look like linked text
    JButton btnHelp = new JButton("Help");
    btnHelp.setForeground(Color.WHITE);
    btnHelp.setBackground(Color.BLUE);
    btnHelp.setFont(new Font("Tahoma", Font.BOLD, 15));
    btnHelp.setBounds(740,0,50,50);
    contentPane.add(btnHelp);
    Login.labelAppearance(btnHelp);

    //import and position resized icon
    BufferedImage iconresized = ImageIO.read(this.getClass().getResource("/gui/iconresized.png"));
    JLabel picLabel = new JLabel(new ImageIcon(iconresized));
    contentPane.add(picLabel);
    picLabel.setBounds(5,10,50,80);

    //create, format and position label for the Tutorial description
    //this will remain the same on all pages of this tutorial
    JLabel lblTutorial1 = new JLabel("TUTORIAL 1 - Composition : If, switch, while & for statements");
    lblTutorial1.setFont(new Font("Gisha", Font.BOLD, 22));
    lblTutorial1.setForeground(Color.BLUE);
    lblTutorial1.setBounds(60, 40, 675, 44);
    lblTutorial1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
    contentPane.add(lblTutorial1);

    //create, format and position label for the title of this page
    //this changes on each page of each tutorial
    JLabel lblIntroductionT1 = new JLabel("Introduction - Select Statements");
    lblIntroductionT1.setFont(new Font("Gisha", Font.ITALIC, 15));
    lblIntroductionT1.setForeground(Color.RED);
    lblIntroductionT1.setBounds(20, 125, 300, 26);
    contentPane.add(lblIntroductionT1);


    //create new text field for displaying notes on the page
    //this will wrap text to the text field horizontally, but allow a vertical scroll bar
    //if the text goes on too long to fit the field vertically
    TextArea Content1 = new TextArea("", 4, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
    Content1.setBounds(20, 155, 400, 300);
    contentPane.add(Content1);
    //add(textField, BorderLayout.NORTH);
    Content1.setEditable(false);

    //read in text file for content
    FileReader readtextfile = new FileReader("src/gui/Tutorial1content.txt");

    //open scanner
    Scanner FileReaderScan = new Scanner(readtextfile);

    //create sting Tut1content to store what is read in
    String Tut1content ="";

    //while there is another line in text file add it to string
    while(FileReaderScan.hasNextLine()){
        String temp = FileReaderScan.nextLine() + "\n";
        Tut1content = Tut1content + temp;

    }

    //add contents of string to the text area
    Content1.append(Tut1content);

    //close scanner
    FileReaderScan.close();


    //import and position the diagram to go with text
    //both should be parallel to each other

    //read in image
    BufferedImage IfState = ImageIO.read(this.getClass().getResource(imageSelect(index)));

    //set image in JLabel
    JLabel picLabel2 = new JLabel(new ImageIcon(IfState));
    //add border to picture
    picLabel2.setBorder(BorderFactory.createLineBorder(Color.black));
    //add to frame
    contentPane.add(picLabel2);
    //set bounds, location
    picLabel2.setBounds(450,155,300,300);

    //create and position progress bar so user can track where they are in tutorial

    JProgressBar tut1progressBar = new JProgressBar(0,5);
    tut1progressBar.setValue(1); //page 1 of 5
    tut1progressBar.setBounds(220, 550, 380, 14);
    contentPane.add(tut1progressBar);

    //add label to progress bar to show page number currently on
    JLabel lblProgress1 = new JLabel("Page 1/5");
    lblProgress1.setFont(new Font("Gisha", Font.ITALIC, 10));
    lblProgress1.setForeground(Color.RED);
    lblProgress1.setBounds(690, 540, 80, 26);
    contentPane.add(lblProgress1);
}
}

1 个答案:

答案 0 :(得分:0)

如果我弄错了,请纠正我,但是在你的代码中没有任何地方我看到你调用Images方法,这是实例化LinkedList<T>并填充它的方法。由于您在.get引用上致电null,因此您获得NullPointerException

另外请注意,请遵循代码中的Java命名约定(因此方法名称和变量应位于 camelCase 中)。