我在做一个应用程序,可以让你添加文件,然后压缩他们,但如何从我的硬盘驱动器或为此事到我的应用程序的任何硬盘驱动器获得的文件? 我可以通过一个的FileReader拿到文件,但如何把它变成我的GUI?
我读了defaultListModel是去,但我不确定的方式。
public class LockNCompressWindow
{
public static void main(String[] args)
{
LockFrame w = new LockFrame();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(500,500);
w.setResizable(false);
w.setVisible(true);
}
}
class LockFrame extends JFrame implements ActionListener
{
//Declaring MenuBar and components
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem MenuItemClose = new JMenuItem("Close");
//Declaring Panels
JPanel PanelNorth = new JPanel();
JPanel PanelCenter = new JPanel();
JPanel PanelSouth = new JPanel();
//Declaring Buttons
JButton ButtonAddFile = new JButton("Add File");
JButton ButtonDeleteFile = new JButton("Delete File");
JButton ButtonLock = new JButton("Lock");
JButton ButtonUnlock = new JButton("Unlock");
//Declaring FileChooser
JFileChooser chooser = new JFileChooser();
public LockFrame()
{
//Title of the frame
super("Lock and Zip");
//Creating Menu bar
super.setJMenuBar(menuBar);
//Creating the Menu Tab
menuBar.add(menu);
//Creating a Menu Item
menu.add(MenuItemClose);
//Adding North Panel
PanelNorth.setBorder(BorderFactory.createEtchedBorder());
super.add(PanelNorth);
PanelNorth.add(ButtonAddFile);
PanelNorth.add(ButtonDeleteFile);
add(PanelNorth,BorderLayout.NORTH);
//Adding Center Panel to Frame
super.add(PanelCenter);
//Adding Scroll Pane
JScrollPane listScroller = new JScrollPane();
listScroller.setPreferredSize(new Dimension(400,360));
PanelCenter.add(listScroller);
add(PanelCenter, BorderLayout.CENTER);
//Adding South Panel
PanelSouth.setBorder(BorderFactory.createEtchedBorder());
super.add(PanelCenter);
PanelSouth.add(ButtonLock);
PanelSouth.add(ButtonUnlock);
PanelSouth.add(ButtonPassword);
add(PanelSouth,BorderLayout.SOUTH);
//Action Listeners
ButtonAddFile.addActionListener(this);
ButtonPassword.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object Source = e.getSource();
int ReturnValue;
if (Source == ButtonAddFile)
{
ReturnValue = chooser.showOpenDialog(LockFrame.this);
if (ReturnValue == JFileChooser.APPROVE_OPTION())
{
File file = chooser.getSelectedFile();
//Add the file to you center panel
}
}
if (Source == ButtonDeleteFile)
{
}
if (Source == ButtonLock)
{
}
if (Source == ButtonUnlock)
{
}
if (Source == ButtonPassword)
{
}
}
}