java问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java问题相关的知识,希望对你有一定的参考价值。

我想写一个树型菜单,就类似于WINDOWS中的资源管理器右边的那些菜单,用什么类实现,只不过,我让这些菜单的功能不一样,功能就交级事件处理类了,就是这样的菜单怎么实现?高手们指点指点!谢谢了

给你个小例子看看就会了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TreeEditTest
public static void main(String[] args)
JFrame frame = new TreeEditFrame();
frame.show();



class TreeEditFrame extends JFrame implements ActionListener
public TreeEditFrame()
setTitle("TreeEditTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);

// construct tree

TreeNode root = makeSampleTree();
model = new DefaultTreeModel(root);
tree = new JTree(model);
DefaultTreeCellRenderer renderer=new DefaultTreeCellRenderer();
renderer.setLeafIcon(new ImageIcon("1.gif"));
renderer.setClosedIcon(new ImageIcon("2.gif"));
renderer.setOpenIcon(new ImageIcon("3.gif"));
//renderer.setBackgroundNonSelectionColor(Color.BLUE);
//renderer.setBackgroundSelectionColor(Color.RED);
renderer.setBorderSelectionColor(Color.RED);

tree.setCellRenderer(renderer);

// add scroll pane with tree to content pane

Container contentPane = getContentPane();
JScrollPane scrollPane = new JScrollPane(tree);
contentPane.add(scrollPane, "Center");

// make button panel

JPanel panel = new JPanel();
addSiblingButton = new JButton("Add Sibling");
addSiblingButton.addActionListener(this);
panel.add(addSiblingButton);
addChildButton = new JButton("Add Child");
addChildButton.addActionListener(this);
panel.add(addChildButton);
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
panel.add(deleteButton);
contentPane.add(panel, "South");


public TreeNode makeSampleTree()
DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
root.add(country);
DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
country.add(state);
DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
state.add(city);
city = new DefaultMutableTreeNode("Cupertino");
state.add(city);
state = new DefaultMutableTreeNode("Michigan");
country.add(state);
city = new DefaultMutableTreeNode("Ann Arbor");
state.add(city);
country = new DefaultMutableTreeNode("Germany");
root.add(country);
state = new DefaultMutableTreeNode("Schleswig-Holstein");
country.add(state);
city = new DefaultMutableTreeNode("Kiel");
state.add(city);
return root;


public void actionPerformed(ActionEvent event)
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent();

if (selectedNode == null)
return;

if (event.getSource().equals(deleteButton))
if (selectedNode.getParent() != null)
model.removeNodeFromParent(selectedNode);
// model.removeFromParent(selectedNode);
return;


// add new node as sibling or child

DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");

if (event.getSource().equals(addSiblingButton))
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
.getParent();

if (parent != null)
int selectedIndex = parent.getIndex(selectedNode);
model.insertNodeInto(newNode, parent, selectedIndex + 1);

else if (event.getSource().equals(addChildButton))
model.insertNodeInto(newNode, selectedNode, selectedNode
.getChildCount());


// now display new node

TreeNode[] nodes = model.getPathToRoot(newNode);
TreePath path = new TreePath(nodes);
tree.scrollPathToVisible(path);


private DefaultTreeModel model;

private JTree tree;

private JButton addSiblingButton;

private JButton addChildButton;

private JButton deleteButton;

private JButton editButton;
参考技术A JTree

【java】BufferedReader的问题

下面是我写的一个测试程序,如果不把Input1.close();屏蔽调的话,就无法输入Input2,报的错误是:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at test.main(test.java:33)
请问这是怎么回事啊?我第一个BufferedReader使用完后关闭,然后第二个BufferedReader是重新申请的啊?为什么第一个关闭了,第二个会提示Stream closed呢?
以下是我的程序test.java,请了解的朋友给解释一下,谢谢!
import java.io.*;

public class test
static int num;
static String str;
public static void main(String[] args)
try
System.out.print("Input number: ");
BufferedReader Input1 = new BufferedReader(new InputStreamReader(System.in));
num = Integer.parseInt(Input1.readLine());
Input1.close();//如果屏蔽该句则可以输入Input2

catch(IOException e1)
e1.printStackTrace();


try
System.out.print("Input string: ");
BufferedReader Input2 = new BufferedReader(new InputStreamReader(System.in));
str = Input2.readLine();
Input2.close();

catch(IOException e2)
e2.printStackTrace();


System.out.println("number = " + num + "\nString = " + str);

BufferedReader Input1 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader Input2 = new BufferedReader(new InputStreamReader(System.in));
这两句话中的Input1和Input2都是由System.in封装而来。这是设计模式中的装饰模式的应用,顾名思义,装饰模式就是对最原始的东西进行装饰,只改变了外表,但实质并没有改变。System.in就是最原始的东西,Input1和Input2只是他被装饰后的外表,所以程序中的Input1和Input2的实质上都是System.in。调用Input1.close(),会自动调用Input1的实质System.in.close(),所以对于Input2来说他的实质System.in已经关闭掉,所以...
参考技术A 你关闭流最好做个finally 参考技术B 高手 但是我不明白为什么要关掉流呢

以上是关于java问题的主要内容,如果未能解决你的问题,请参考以下文章

java问题

java 小问题

java问题

java实现日期排序问题

java问题

Java中String问题