📄 ebookmakerswing.java
字号:
/*
* Created on 2004-10-26
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.zmc.ebook.maker;
import java.awt.FileDialog;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import java.awt.BorderLayout;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
/**
*EBookMakerSwing <br>
* Copyright (c) 2000, 2004 e-Channels Corporation<br>
* <br>
* Created on 2004-10-26<br>
* @author ZHONGMC<br>
*<br>
* Last modified on 2004-10-26<br>
* Last modified by ZHONGMC<br>
*
*<p>Comment here</p>
*
*/
public class EBookMakerSwing extends JFrame
{
private JTextField chapterNameText;
private JTextArea contentText;
private JList chapterList;
private JTextField bookNameText;
private ListDataModel dataModel;
/**
* @throws java.awt.HeadlessException
*/
public EBookMakerSwing() throws HeadlessException
{
super();
final JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
final JMenu menu = new JMenu();
menuBar.add(menu);
menu.setText("File");
final JMenuItem menuItem_4 = new JMenuItem();
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
menu.add(menuItem_4);
menuItem_4.setText("Open");
final JMenuItem menuItem = new JMenuItem();
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
menu.add(menuItem);
menuItem.setText("Save");
menu.addSeparator();
final JMenuItem menuItem_6 = new JMenuItem();
menu.add(menuItem_6);
menuItem_6.setText("Build");
menuItem_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buildJar();
}
});
menu.addSeparator();
final JMenuItem menuItem_1 = new JMenuItem();
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
menu.add(menuItem_1);
menuItem_1.setText("Exit");
final JMenu menu_2 = new JMenu();
menuBar.add(menu_2);
menu_2.setText("Option");
final JMenuItem menuItem_5 = new JMenuItem();
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
menu_2.add(menuItem_5);
menuItem_5.setText("Option");
final JMenu menu_1 = new JMenu();
menuBar.add(menu_1);
menu_1.setText("Help");
final JMenuItem menuItem_2 = new JMenuItem();
menu_1.add(menuItem_2);
menuItem_2.setText("Content");
menu_1.addSeparator();
final JMenuItem menuItem_3 = new JMenuItem();
menu_1.add(menuItem_3);
menuItem_3.setText("About");
final JSplitPane splitPane = new JSplitPane();
getContentPane().add(splitPane, BorderLayout.CENTER);
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(100, 0));
panel.setLayout(new BorderLayout());
splitPane.setLeftComponent(panel);
final JLabel label = new JLabel();
panel.add(label, BorderLayout.NORTH);
label.setText("Chapters:");
final JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, BorderLayout.CENTER);
chapterList = new JList();
chapterList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
setActivateChapter();
}
});
scrollPane.setViewportView(chapterList);
dataModel = new ListDataModel();
chapterList.setModel( dataModel );
final JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout());
splitPane.setRightComponent(panel_1);
final JPanel panel_3 = new JPanel();
panel_3.setPreferredSize(new Dimension(0, 60));
panel_1.add(panel_3, BorderLayout.NORTH);
final JLabel label_4 = new JLabel();
panel_3.add(label_4);
label_4.setText("BookName:");
bookNameText = new JTextField();
bookNameText.setPreferredSize(new Dimension(200, 25));
panel_3.add(bookNameText);
final JLabel label_1 = new JLabel();
panel_3.add(label_1);
label_1.setText("ChapterName:");
chapterNameText = new JTextField();
chapterNameText.setPreferredSize(new Dimension(200, 25));
panel_3.add(chapterNameText);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addChapter();
}
});
panel_3.add(button);
button.setText("Add");
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
deleteChapter();
}
});
panel_3.add(button_1);
button_1.setText("Delete");
final JButton button_2 = new JButton();
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateChapter();
}
});
panel_3.add(button_2);
button_2.setText("Update");
final JScrollPane scrollPane_1 = new JScrollPane();
panel_1.add(scrollPane_1, BorderLayout.CENTER);
contentText = new JTextArea();
scrollPane_1.setViewportView(contentText);
final JPanel panel_2 = new JPanel();
panel_2.setPreferredSize(new Dimension(0, 25));
getContentPane().add(panel_2, BorderLayout.SOUTH);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
*/
public EBookMakerSwing(GraphicsConfiguration arg0)
{
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
* @throws java.awt.HeadlessException
*/
public EBookMakerSwing(String arg0) throws HeadlessException
{
super(arg0);
// TODO Auto-generated constructor stub
}
/**
* @param arg0
* @param arg1
*/
public EBookMakerSwing(String arg0, GraphicsConfiguration arg1)
{
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
private void openFile()
{
FileDialog dlg = new FileDialog(this, "Open ebook Def file");
dlg.setMode( FileDialog.LOAD);
dlg.show();
String fileName = dlg.getFile();
if( fileName == null )
return;
}
private void saveFile()
{
FileDialog dlg = new FileDialog(this, "Open ebook Def file");
dlg.setMode( FileDialog.SAVE);
dlg.show();
String fileName = dlg.getFile();
if( fileName == null )
return;
}
private void buildJar()
{
FileDialog dlg = new FileDialog(this, "Open ebook Def file");
dlg.setMode( FileDialog.SAVE);
dlg.show();
String fileName = dlg.getDirectory() + "/" + dlg.getFile();
if( fileName == null )
return;
try{
FileOutputStream fo = new FileOutputStream(fileName);
ZipOutputStream zo = new ZipOutputStream( fo );
this.writeFileToZip("com/zmc/ebook/reader/About.class", zo);
this.writeFileToZip("com/zmc/ebook/reader/PageContent.class", zo);
this.writeFileToZip("com/zmc/ebook/reader/ReaderFrame.class", zo);
this.writeFileToZip("com/zmc/ebook/reader/TextReader.class", zo);
ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
zo.putNextEntry(entry);
String genInfo = "Manifest-Version: 1.0\n" +
"MIDlet-1: eBook Reader,, com.zmc.ebook.reader.TextReader\n" +
"MIDlet-Data-Size: 256\n" +
"MIDlet-Description: eBook reader\n" +
"MIDlet-Name:" + this.bookNameText.getText()+ "\n" +
"MIDlet-Permissions: javax.microedition.io.Connector.http\n" +
"MIDlet-Vendor: e-Channels, Inc.\n" +
"MIDlet-Version: 1.0.10\n" +
"MicroEdition-Configuration: CLDC-1.0\n" +
"MicroEdition-Profile: MIDP-2.0\n";
zo.write( genInfo.getBytes("UTF-8"));
for( int i=0; i< dataModel.getSize(); i++)
{
Chapter chapter = (Chapter)dataModel.getElementAt(i);
String chapterFile = "Chapter-" + (i+1) + ":/Chapter" + (i+1) + ".txt\r\n";
zo.write(chapterFile.getBytes());
String chapterTitle = "ChapterTitle-" + (i+1 )+ ":" + chapter.chapterName + "\r\n";
zo.write( chapterTitle.getBytes("UTF-8"));
}
zo.closeEntry();
for( int i=0; i< dataModel.getSize(); i++)
{
Chapter chapter = (Chapter)dataModel.getElementAt(i);
ZipEntry entry1 = new ZipEntry("Chapter" + (i+1) + ".txt");
zo.putNextEntry(entry1);
zo.write( chapter.ChapterContent.getBytes("UTF-8"));
zo.closeEntry();
}
zo.close();
fo.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
private void setActivateChapter()
{
int idx = chapterList.getSelectedIndex();
if( idx == -1 )
return;
Chapter chapter = (Chapter)chapterList.getSelectedValue();
this.chapterNameText.setText( chapter.chapterName);
this.contentText.setText( chapter.ChapterContent );
}
private void addChapter()
{
String chapterName = chapterNameText.getText();
String content = contentText.getText();
if( chapterName.length() == 0 )
return;
Chapter chapter = new Chapter(chapterName, content);
this.chapterNameText.setText( chapter.chapterName);
this.contentText.setText( chapter.ChapterContent );
this.dataModel.addItem( chapter );
}
private void deleteChapter()
{
Chapter chapter = (Chapter)this.chapterList.getSelectedValue();
if( chapter != null )
{
this.dataModel.removeItem( chapter );
}
}
private void updateChapter()
{
Chapter chapter = (Chapter)this.chapterList.getSelectedValue();
if( chapter != null )
{
String chapterName = this.chapterNameText.getText();
String content = this.contentText.getText();
if( chapterName.length() == 0 )
return;
chapter.chapterName = chapterName;
chapter.ChapterContent = content;
this.dataModel.updateItem( chapter );
}
}
private void writeFileToZip(String src, ZipOutputStream zo)
{
try{
ZipEntry entry = new ZipEntry(src);
zo.putNextEntry(entry);
FileInputStream fi = new FileInputStream( src );
byte[] tmp = new byte[256];
while( true )
{
int readLen = fi.read(tmp );
if( readLen <= 0 )
break;
zo.write(tmp, 0, readLen);
}
zo.closeEntry();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
EBookMakerSwing frame = new EBookMakerSwing();
frame.setSize(800,600);
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -