我正在尝试使用SWT库创建程序,但是当我尝试运行它时,我得到以下异常:
线程“main”中的异常java.lang.NoClassDefFoundError: 组织/蚀/ SWT /部件/布局 在il.ac.tau.cs.sw1.trivia.TriviaMain.main(TriviaMain.java:9) 引起:java.lang.ClassNotFoundException:org.eclipse.swt.widgets.Layout at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ......还有1个
它甚至没有开始运行程序,它只是立即崩溃。可以做些什么来解决它?
我添加了我的代码:
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class TriviaGUI {
private static final int MAX_ERRORS = 3;
private Shell shell;
private Label scoreLabel;
private Composite questionPanel;
private Label startupMessageLabel;
private Font boldFont;
private String lastAnswer;
// Currently visible UI elements.
Label instructionLabel;
Label questionLabel;
private List<Button> answerButtons = new LinkedList<>();
private Button passButton;
private Button fiftyFiftyButton;
//other
private String path="";
private List<String> currentAnswers;
private SortedMap<String,List<String>> fullQuestion = new TreeMap<>();
private Map<String,String> questionPlusAnswer = new HashMap<>();
private ArrayList<String> numberedQuestions = new ArrayList<>();
private int score;
private int wrongAnswers;
private int numOfQuestions;
private boolean usedFiftyFifty = false;
private boolean usedPass = false;
public void open() {
createShell();
runApplication();
}
/**
* Creates the widgets of the application main window
*/
private void createShell() {
Display display = Display.getDefault();
shell = new Shell(display);
shell.setText("Trivia");
// window style
Rectangle monitor_bounds = shell.getMonitor().getBounds();
shell.setSize(new Point(monitor_bounds.width / 3,
monitor_bounds.height / 4));
shell.setLayout(new GridLayout());
FontData fontData = new FontData();
fontData.setStyle(SWT.BOLD);
boldFont = new Font(shell.getDisplay(), fontData);
// create window panels
createFileLoadingPanel();
createScorePanel();
createQuestionPanel();
}
/**
* Creates the widgets of the form for trivia file selection
*/
private void createFileLoadingPanel() {
final Composite fileSelection = new Composite(shell, SWT.NULL);
fileSelection.setLayoutData(GUIUtils.createFillGridData(1));
fileSelection.setLayout(new GridLayout(4, false));
final Label label = new Label(fileSelection, SWT.NONE);
label.setText("Enter trivia file path: ");
// text field to enter the file path
final Text filePathField = new Text(fileSelection, SWT.SINGLE
| SWT.BORDER);
filePathField.setLayoutData(GUIUtils.createFillGridData(1));
// "Browse" button
final Button browseButton = new Button(fileSelection, SWT.PUSH);
browseButton.setText("Browse");
browseButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
path = GUIUtils.getFilePathFromFileDialog(shell);
if(path!=null)
filePathField.setText(path);
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
// "Play!" button
final Button playButton = new Button(fileSelection, SWT.PUSH);
playButton.setText("Play!");
playButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
if(!filePathField.getText().equals(path))
path = filePathField.getText();
try {
BufferedReader br = new BufferedReader(new FileReader(new File(path)));
score = 0;
scoreLabel.setText(String.valueOf(score));
wrongAnswers = 0;
for(String str = br.readLine(); str!=null; str = br.readLine()) {
String[] arr = str.split("\t");
numberedQuestions.add(arr[0]);
questionPlusAnswer.put(arr[0], arr[1]);
List<String> anwersForThisQuestion = new ArrayList<>();
for(int i=1; i<str.length(); i++) {
anwersForThisQuestion.add(arr[i]);
}
fullQuestion.put(arr[0], anwersForThisQuestion);
updateQuestionPanel(arr[0], anwersForThisQuestion);
}
startPlaying();
br.close();
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
GUIUtils.showErrorDialog(shell, "File " + path + " Not Found");
e1.printStackTrace();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
}
protected void startPlaying() {
numOfQuestions = 0;
if(usedFiftyFifty && score<1)
fiftyFiftyButton.setEnabled(false);
if(usedPass && score<1)
passButton.setEnabled(false);
Collections.shuffle(numberedQuestions);
for(int i=0; i<numberedQuestions.size(); i++) {
numOfQuestions++;
String question = numberedQuestions.get(i);
updateQuestionPanel(question,fullQuestion.get(question));
if(wrongAnswers>=MAX_ERRORS) {
break;
}
if(usedFiftyFifty && score<1)
fiftyFiftyButton.setEnabled(false);
else
fiftyFiftyButton.setEnabled(true);
if(usedPass && score<1)
passButton.setEnabled(false);
else
passButton.setEnabled(true);
}
GUIUtils.showInfoDialog(shell, "Game Over", "Your Final Score is " + score +" After " + numOfQuestions + "Questions");
}
/**
* Creates the panel that displays the current score
*/
private void createScorePanel() {
Composite scorePanel = new Composite(shell, SWT.BORDER);
scorePanel.setLayoutData(GUIUtils.createFillGridData(1));
scorePanel.setLayout(new GridLayout(2, false));
final Label label = new Label(scorePanel, SWT.NONE);
label.setText("Total score: ");
// The label which displays the score; initially empty
scoreLabel = new Label(scorePanel, SWT.NONE);
scoreLabel.setLayoutData(GUIUtils.createFillGridData(1));
}
/**
* Creates the panel that displays the questions, as soon as the game
* starts. See the updateQuestionPanel for creating the question and answer
* buttons
*/
private void createQuestionPanel() {
questionPanel = new Composite(shell, SWT.BORDER);
questionPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
true, true));
questionPanel.setLayout(new GridLayout(2, true));
// Initially, only displays a message
startupMessageLabel = new Label(questionPanel, SWT.NONE);
startupMessageLabel.setText("No question to display, yet.");
startupMessageLabel.setLayoutData(GUIUtils.createFillGridData(2));
}
/**
* Serves to display the question and answer buttons
*/
private void updateQuestionPanel(String question, List<String> answers) {
// Save current list of answers.
currentAnswers = answers;
// clear the question panel
Control[] children = questionPanel.getChildren();
for (Control control : children) {
control.dispose();
}
// create the instruction label
instructionLabel = new Label(questionPanel, SWT.CENTER | SWT.WRAP);
instructionLabel.setText(lastAnswer + "Answer the following question:");
instructionLabel.setLayoutData(GUIUtils.createFillGridData(2));
// create the question label
questionLabel = new Label(questionPanel, SWT.CENTER | SWT.WRAP);
questionLabel.setText(question);
questionLabel.setFont(boldFont);
questionLabel.setLayoutData(GUIUtils.createFillGridData(2));
// create the answer buttons
for (int i = 0; i < 4; i++) {
Button answerButton = new Button(questionPanel, SWT.PUSH | SWT.WRAP);
answerButton.setText(answers.get(i));
GridData answerLayoutData = GUIUtils.createFillGridData(1);
answerLayoutData.verticalAlignment = SWT.FILL;
answerButton.setLayoutData(answerLayoutData);
answerButtons.add(answerButton);
}
// create the "Pass" button to skip a question
passButton = new Button(questionPanel, SWT.PUSH);
passButton.setText("Pass");
GridData data = new GridData(GridData.END, GridData.CENTER, true,
false);
data.horizontalSpan = 1;
passButton.setLayoutData(data);
// create the "50-50" button to show fewer answer options
fiftyFiftyButton = new Button(questionPanel, SWT.PUSH);
fiftyFiftyButton.setText("50-50");
data = new GridData(GridData.BEGINNING, GridData.CENTER, true,
false);
data.horizontalSpan = 1;
fiftyFiftyButton.setLayoutData(data);
// two operations to make the new widgets display properly
questionPanel.pack();
questionPanel.getParent().layout();
}
/**
* Opens the main window and executes the event loop of the application
*/
private void runApplication() {
shell.open();
Display display = shell.getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
boldFont.dispose();
}
}
这是第一堂课。
主要班级:
public class TriviaMain {
/**
* Execute to play trivia!
*/
public static void main(String[] args) {
TriviaGUI gui = new TriviaGUI();
gui.open();
}
}
当我尝试调试时,程序甚至没有进入TriviaGUI类。它立即在TriviaMain中崩溃。
答案 0 :(得分:0)
您缺少包含该类的eclipse库,请在Eclipse Tye Search按钮中查看它,或使用http://www.findjar.com或类似服务来确定JAR的调用方式。