我创建了一个类来创建我的程序的UserInterface
,我的程序有一个UserInterface
的实例。
UserIterface
类有一些监听器,我现在的问题是,当我用于创建用户界面的实例变量引发事件时,我需要从我的程序主类知道,例如tree valueChanged。
这是我的userInterface类:
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTree;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import org.eclipse.swt.widgets.Tree;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
public class UserInterfaceWindow implements TreeSelectionListener{
public JFrame frame;
public JTree tree;
public JPanel panel;
public JFileChooser fileChooser;
public IffFile iffFile;
public CustomRanAccFile inputRandAccFile;
/**
* Create the application.
*/
public UserInterfaceWindow() {
initialize();
frame.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 758, 549);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolBar = new JToolBar();
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
JButton btnOpenFile = new JButton("Open File");
btnOpenFile.addActionListener(new OpenButtonClick());
toolBar.add(btnOpenFile);
JButton button = new JButton("New button");
toolBar.add(button);
JSplitPane splitPane = new JSplitPane();
splitPane.setMinimumSize(new Dimension(300, 25));
frame.getContentPane().add(splitPane, BorderLayout.CENTER);
tree = new JTree();
tree.setModel(new DefaultTreeModel(
new DefaultMutableTreeNode("File") {
/**
*
*/
private static final long serialVersionUID = 1L;
{
}
}
));
tree.setMinimumSize(new Dimension(200, 0));
tree.setMaximumSize(new Dimension(200, 64));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(this);
splitPane.setLeftComponent(tree);
panel = new JPanel();
splitPane.setRightComponent(panel);
}
public class OpenButtonClick implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(fileChooser==null)
setUpFileChooser();
if(fileChooser.showOpenDialog(frame)==JFileChooser.APPROVE_OPTION){
try {
if(inputRandAccFile!=null)
inputRandAccFile.close();
inputRandAccFile= new CustomRanAccFile(fileChooser.getSelectedFile(),"rw");
iffFile=IffUtils.createIffFileInfo(inputRandAccFile);
IffUtils.obtainSubFileList2K14(iffFile, inputRandAccFile);
UserInterfaceWindow.this.setTreeElements(iffFile,UserInterfaceWindow.this.tree);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public void setTreeElements(IffFile iffFile, JTree tree){
DefaultMutableTreeNode iffNode= new DefaultMutableTreeNode(iffFile);
DefaultTreeModel model= new DefaultTreeModel(iffNode, true);
for(IffBaseSubFile subFile: iffFile.getSubFileList()){
iffNode.add(new DefaultMutableTreeNode(subFile,false));
}
tree.setModel(model);
}
public void setUpFileChooser(){
fileChooser= new JFileChooser();
fileChooser.setDialogTitle("Select File");
FileNameExtensionFilter iffcdfFilter= new FileNameExtensionFilter("IFF/CDF Files","iff","CDF Files","cdf");
fileChooser.addChoosableFileFilter(iffcdfFilter);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setFileFilter(iffcdfFilter);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
// TODO Auto-generated method stub
}
}
这是Myprogram:
public class MyMain {
UserInterfaceWindow uiWindow;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyMain myMain= new MyMain();
myMain.uiWindow= new UserInterfaceWindow();
}
}
答案 0 :(得分:0)
当用户点击树的节点时,将触发事件TreeSelectionEvent。