这是一个大学的Java课程的期末考试题,我刚学Java时间不长,因此做一些考试题来练练手。
题目要求做一个如图所示的简易文本编辑器,左边的区域就是编辑区,右边的区域可以选择字体,大小等等,有一个菜单栏,打开后如右边小图那样,可以选择打开,保存文件,以及退出等等。(图中文字是德语,Neu是“新建”,Oeffen是“打开”,右下的按钮Standardfont是“标准字体”的意思,等等等等)。另外文本编辑区和字体选择树要有滚动条,内定的标准字体名称是“Dialog”,大小是12。
花了一个多小时写完,简单调试了一下,基本上达到了要求。不过奇怪的是右边那个选择字体大小的JComboBox却一直显示不出来下拉框,因此还请高手们指点一二。
完整的代码如下,有兴趣的同学可以拿去做参考。
-------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TextEditor extends JFrame{
JMenuBar mbMenu = new JMenuBar();
JMenu mDatei = new JMenu("Datei");
JMenuItem miNew = new JMenuItem("Neu");
JMenuItem miOpen = new JMenuItem("Oeffen");
JMenuItem miSave = new JMenuItem("Speichern");
JMenuItem miClose = new JMenuItem("Beenden");
JTextArea jta = new JTextArea();
JTree tFontName;
JLabel lblFontGroess = new JLabel("Groess:");
JComboBox cmbFontSize= new JComboBox();
JLabel lblFontSchrift = new JLabel("Schriftschnitt:");
JCheckBox chbFontFett = new JCheckBox("fett");
JLabel lblNoName = new JLabel();
JCheckBox chbFontKursiv = new JCheckBox("kursiv");
JButton btnStand = new JButton("Standartfont");
JScrollPane spText = new JScrollPane(jta);
JScrollPane spFont;
JPanel pnlFontDetail = new JPanel(new GridLayout(3,2));
JPanel pnlFont = new JPanel(new BorderLayout());
Font currentFont = new Font("Dialog",Font.PLAIN,12);
File currentDir = null;
File currentFile = null;
String currentFontName = "Dialog";
int rowOfStand = 0;
public TextEditor(){
super("Editor");
mbMenu.add(mDatei);
mDatei.add(miNew);
mDatei.add(miOpen);
mDatei.add(miSave);
mDatei.addSeparator();
mDatei.add(miClose);
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Scriftart");
DefaultMutableTreeNode child;
String [] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
for (String font : fontNames){
child = new DefaultMutableTreeNode(font);
node.add(child);
}
rowOfStand = java.util.Arrays.binarySearch(fontNames,currentFontName)+1;
tFontName = new JTree(node);
tFontName.setVisibleRowCount(5);
spFont = new JScrollPane(tFontName);
for(int i= 6;i<21;i++)
cmbFontSize.addItem( i );
//cmbFontSize.setPopupVisible(true);//这样可以么?
pnlFontDetail.add(lblFontGroess);
pnlFontDetail.add(cmbFontSize);
pnlFontDetail.add(lblFontSchrift);
pnlFontDetail.add(chbFontFett);
pnlFontDetail.add(lblNoName);
pnlFontDetail.add(chbFontKursiv);
Panel pnlSide = new Panel(new BorderLayout());
Panel pnlButton = new Panel();
pnlButton.add(btnStand);
pnlFont.add(spFont,BorderLayout.NORTH);
pnlFont.add(pnlFontDetail,BorderLayout.CENTER);
pnlSide.add(pnlFont,BorderLayout.NORTH);
pnlSide.add(pnlButton,BorderLayout.CENTER);
add(spText,BorderLayout.CENTER);
add(pnlSide,BorderLayout.EAST);
// add Listener
miNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jta.setText("");
currentFile=null;
}
});
miOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
loadFile();
}
});
miSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
saveFile();
}
});
miClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
tFontName.addTreeSelectionListener(new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent e){
currentFontName = e.getPath().getLastPathComponent().toString();
setFont();
}
});
cmbFontSize.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
setFont();
}
});
chbFontFett.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
setFont();
}
});
chbFontKursiv.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e){
setFont();
}
});
btnStand.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontReset();
}
});
fontReset();
jta.setLineWrap(true);
setJMenuBar(mbMenu);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700,500);
setVisible(true);
}
public void fontReset(){
tFontName.setSelectionRow(rowOfStand);
cmbFontSize.setSelectedItem(12);
chbFontFett.setSelected(false);
chbFontKursiv.setSelected(false);
setFont();
}
public void setFont(){
int fontStyle = 0;
if(chbFontFett.isSelected())
fontStyle = fontStyle + Font.BOLD;
if(chbFontKursiv.isSelected())
fontStyle = fontStyle + Font.ITALIC;
if(fontStyle==0)
fontStyle = Font.PLAIN;
int fontSize = (Integer) (cmbFontSize.getSelectedItem());
currentFont = new Font(currentFontName,fontStyle,fontSize);
jta.setFont(currentFont);
}
public void loadFile(){
JFileChooser jfc = new JFileChooser(currentDir);
jfc.showOpenDialog(this);
if (jfc.getSelectedFile()==null)
return;
currentDir=jfc.getCurrentDirectory();
currentFile=jfc.getSelectedFile();
StringBuffer content = new StringBuffer((int) currentFile.length());
try{
BufferedReader reader = new BufferedReader(new FileReader(currentFile));
char[] buffer = new char[4096];
int read = 0;
while((read = reader.read(buffer))!=-1)
content.append(buffer,0,read);
reader.close();
}
catch(IOException ex){
JOptionPane.showMessageDialog(this," Laden der datei fehlgeschlagen!",
"Feler",JOptionPane.ERROR_MESSAGE);
}
jta.setText(content.toString());
}
public void saveFile(){
if (currentFile==null){
JFileChooser jfc = new JFileChooser(currentDir);
jfc.showSaveDialog(this);
if (jfc.getSelectedFile()==null)
return;
currentDir=jfc.getCurrentDirectory();
currentFile=jfc.getSelectedFile();
}
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(jta.getText());
writer.close();
}
catch(IOException ex){
JOptionPane.showMessageDialog(this,"Spreichern der Datei fehlgeschlagen!",
"Feler",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args){
new TextEditor();
}
}
-------------------------------------------------------------------
俺用的是JDK1.5 + JCreator 3.5。