gpt4 book ai didi

java - JTree - 无法转换为 javax.swing.tree.MutableTreeNode

转载 作者:行者123 更新时间:2023-12-01 13:30:18 24 4
gpt4 key购买 nike

我对 Java GUI 制作非常陌生,我正在尝试在这里尝试。基本上,我有另一个类从 Excel 表中读取有关自动化测试包的数据。我想做的是将这些测试添加到这个资源树中。但是当我尝试使用

   addTest(new testInfo
("TESTING",
"TESTING MESSAGE"));

看来是在提示。我的代码:

package com.main;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class ExcelReaderGui extends JPanel
implements TreeSelectionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JEditorPane htmlPane;
private JTree tree;
private URL helpURL;
private static boolean DEBUG = false;
private static ReadExcel re = new ReadExcel();
private List<testInfo> testsToAdd = new ArrayList<testInfo>();;

//Optionally play with line styles. Possible values are
//"Angled" (the default), "Horizontal", and "None".
private static boolean playWithLineStyle = false;
private static String lineStyle = "Horizontal";

//Optionally set the look and feel.
private static boolean useSystemLookAndFeel = false;

public ExcelReaderGui() {
super(new GridLayout(1,0));

//Create the nodes.
addTest(new testInfo
("TESTING",
"TESTING MESSAGE"));
DefaultMutableTreeNode top =
new DefaultMutableTreeNode("Automated tests");
createNodes(top);


//Create a tree that allows one selection at a time.
tree = new JTree(top);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);

//Listen for when the selection changes.
tree.addTreeSelectionListener(this);

if (playWithLineStyle) {
System.out.println("line style = " + lineStyle);
tree.putClientProperty("JTree.lineStyle", lineStyle);
}

//Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);

//Create the HTML viewing pane.
htmlPane = new JEditorPane();
htmlPane.setEditable(false);
initHelp();
JScrollPane htmlView = new JScrollPane(htmlPane);

//Add the scroll panes to a split pane.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(htmlView);

Dimension minimumSize = new Dimension(100, 50);
htmlView.setMinimumSize(minimumSize);
treeView.setMinimumSize(minimumSize);
splitPane.setDividerLocation(150);
splitPane.setPreferredSize(new Dimension(700, 400));

//Add the split pane to this panel.
add(splitPane);
}

/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();

if (node == null) return;

Object nodeInfo = node.getUserObject();
if (node.isLeaf()) {
testInfo book = (testInfo)nodeInfo;
displayURL(book.resultOfTest);
if (DEBUG) {
System.out.print(book.resultOfTest + ": \n ");
}
} else {
displayURL("Drill down in the resource trees above to find tests with their results");
}
if (DEBUG) {
System.out.println(nodeInfo.toString());
}
}

private class testInfo {
public String testName;
public String resultOfTest;

public testInfo(String test, String testResult) {
testName = test;
resultOfTest = testResult;
}

public String toString() {
return testName;
}
}

private void initHelp() {
String s = "TreeDemoHelp.html";
helpURL = getClass().getResource(s);
if (helpURL == null) {
System.err.println("Couldn't open help file: " + s);
} else if (DEBUG) {
System.out.println("Help URL is " + helpURL);
}

displayURL("Drill down in the resource trees above to find tests with their results");
}

private void displayURL(String url) {
htmlPane.setText(url);
}

public void addTest(testInfo testToAdd) {
testsToAdd.add(testToAdd);
}

private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode category = null;
DefaultMutableTreeNode test = null;

category = new DefaultMutableTreeNode("14 February 2014");
top.add(category);

for (testInfo newTest : testsToAdd) {
category.add((MutableTreeNode) newTest);
}

category = new DefaultMutableTreeNode("13 february 2014");
top.add(category);

//TODO Tests for previous dates
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
if (useSystemLookAndFeel) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use system look and feel.");
}
}

//Create and set up the window.
JFrame frame = new JFrame("My Excel Reader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add content to the window.
frame.add(new ExcelReaderGui());

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
re.setInputFile("D:\\Users\\liagordo\\java\\RMG\\dashboard.xls");
re.read();
} catch (IOException e) {
System.out.println("Error reading excel file");
e.printStackTrace();
}
createAndShowGUI();
}
});
}
}

当我运行之前的代码时,我收到错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
com.main.ExcelReaderGui$testInfo cannot be cast to javax.swing.tree.MutableTreeNode
at com.main.ExcelReaderGui.createNodes(ExcelReaderGui.java:157)
at com.main.ExcelReaderGui.<init>(ExcelReaderGui.java:53)
at com.main.ExcelReaderGui.createAndShowGUI(ExcelReaderGui.java:186)
at com.main.ExcelReaderGui.access$1(ExcelReaderGui.java:171)
at com.main.ExcelReaderGui$1.run(ExcelReaderGui.java:205)

最佳答案

您正在尝试将 testInfo 对象转换为此代码中的 MutableTreeNode

category.add((MutableTreeNode) newTest);

这是不可能的。因为要将 testInfo 转换为 MutableTreeNodetestInfoMutableTreeNode 应该具有继承关系但在你的代码中它们之间没有任何继承关系

关于java - JTree - 无法转换为 javax.swing.tree.MutableTreeNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21608291/

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