急!!!本人编写个记事本有点功能实现不了,请高手指点
下面是本人编写的一个记事本,编写的这个记事本基本功能已实在,比如打开,关闭,剪切,复制等。我想在工具栏上放一个组合框来达到设置字体的效果,但一直实现不了,请那位高手指点指点!代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import *;
import javax.swing.JOptionPane; //消息对话框
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JTextArea;
class WindowText extends Frame implements ItemListener,ChangeListener //建立主框架窗口
{
//Frame mainWindow;
//Panel panelMenu;
//Panel panelEdit;
MenuBar menuB; //菜单栏
//菜单
Menu menuFile;
Menu menuEdit;
Menu menuFormat;
Menu menuView;
Menu menuHelp;
//文件下的菜单项
MenuItem itemNew;
MenuItem itemOpen;
MenuItem itemSave;
MenuItem itemSaveAs;
MenuItem itemPageSetup;
MenuItem itemPrint;
MenuItem itemExit;
//编辑下的菜单项
MenuItem itemUndo;
MenuItem itemCut;
MenuItem itemCopy;
MenuItem itemPaste;
MenuItem itemDelete;
MenuItem itemFind;
MenuItem itemFindNext;
MenuItem itemReplace;
MenuItem itemGoto;
MenuItem itemCheckAll;
MenuItem itemTimeAndDate;
//格式下的菜单项
MenuItem itemLineWrap;
MenuItem itemFont;
//查看下的菜单项
MenuItem itemStatusBar;
//帮助下的菜单项
MenuItem itemHelpTopic;
MenuItem itemAboutNotepad;
//工具栏
JToolBar jt_ToolBar;
// JPanel jp_Panel; //工具栏面板
JButton jb_New;
JButton jb_Open; //打开按钮
JButton jb_Close; //关闭按钮
JButton jb_Save; //保存按钮
JButton jb_Print; //打印按钮
//JPanel jp_CBAFTPanel; //存放标签和组合框的面板
JLabel jl_FontTypeLabel; //标签"字体类型"
//JComboBox jc_ComboBox; //字体类型组合框
Choice jc_ComboBox;
//JButton jb_ComboBoxAndFontType; //字体类型按钮
//JPanel jp_SAFSPanel; //存放标签和滑杆的面板
JLabel jl_FontSizeLabel; //标签”字体大小“
JSlider js_Slider; //滑杆
//JButton jb_SliderAndFontSize; //字体大小按钮
//定义剪帖板对象
Clipboard clipboard1=null; //定义剪帖板对象
//MenuItem itemOpen;
//MenuItem itemSave;
//定义两个对话框(打开,关闭)对象
FileDialog filedialog_save;
FileDialog filedialog_load;
//定义一个“帮助”下的"关于记事本"的对话框对象
MyDialog mydialog;
//文本编辑框
//TextArea edit;
JTextArea edit;
//WindowText winText;
//定义输入输出流
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter tofile;
//构造函数
WindowText(String s)
{
super(s);
//定义一个菜单栏对象
menuB=new MenuBar();
Frame winText=new Frame("我的编辑器");
//定义两个对话框,一个打开,一个关闭对话框.
//fd=new FileDialog(winText,"open");
filedialog_save=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
filedialog_load=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
//定义关于记事本对话框
mydialog=new MyDialog(this,"关于记事本",true);
//获取系统剪贴板
clipboard1=getToolkit().getSystemClipboard();
//菜单
menuFile=new Menu("文件(F)");
menuEdit=new Menu("编辑(E)");
menuFormat=new Menu("格式(O)");
menuView=new Menu("查看(V)");
menuHelp=new Menu("帮助(H)");
//文件下的菜单项
itemNew=new MenuItem("新建(N)");
itemOpen=new MenuItem("打开(O)...");
itemSave=new MenuItem("保存(S)");
itemSaveAs=new MenuItem("另存为(A)...");
itemPageSetup=new MenuItem("页面设置(U)");
itemPrint=new MenuItem("打印...");
itemExit=new MenuItem("退出(X)");
//编辑下的菜单项
itemUndo=new MenuItem("撤销(U)");
itemCut=new MenuItem("剪切(T)...");
itemCopy=new MenuItem("复制(C)");
itemPaste=new MenuItem("粘贴(V)...");
itemDelete=new MenuItem("删除(L)");
itemFind=new MenuItem("查找(F)...");
itemFindNext=new MenuItem("查找下一个(N)");
itemReplace=new MenuItem("替换(R)...");
itemGoto=new MenuItem("转到(G)...");
itemCheckAll=new MenuItem("全选(A)");
itemTimeAndDate=new MenuItem("时间/时期(D)");
//格式下的菜单项
itemLineWrap=new MenuItem("自动转行(W)");
itemFont=new MenuItem("字体(F)...");
//查看下的菜单项
itemStatusBar=new MenuItem("状态栏(S)");
//帮助下的菜单项
itemHelpTopic=new MenuItem("帮助主题(H)");
itemAboutNotepad=new MenuItem("关于记事本(A)");
//“文件”下的菜单项的快捷键
itemNew.setShortcut(new MenuShortcut(KeyEvent.VK_N));
itemOpen.setShortcut(new MenuShortcut(KeyEvent.VK_O));
itemSave.setShortcut(new MenuShortcut(KeyEvent.VK_S));
itemPrint.setShortcut(new MenuShortcut(KeyEvent.VK_P));
//”编辑“下的菜单项的快捷键
itemUndo.setShortcut(new MenuShortcut(KeyEvent.VK_Z));
itemCut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
itemCopy.setShortcut(new MenuShortcut(KeyEvent.VK_C));
itemPaste.setShortcut(new MenuShortcut(KeyEvent.VK_V));
itemDelete.setShortcut(new MenuShortcut(KeyEvent.VK_DELETE));
itemFind.setShortcut(new MenuShortcut(KeyEvent.VK_F));
itemFindNext.setShortcut(new MenuShortcut(KeyEvent.VK_F3));
itemReplace.setShortcut(new MenuShortcut(KeyEvent.VK_H));
itemGoto.setShortcut(new MenuShortcut(KeyEvent.VK_G));
itemCheckAll.setShortcut(new MenuShortcut(KeyEvent.VK_A));
itemTimeAndDate.setShortcut(new MenuShortcut(KeyEvent.VK_F5));
//添加文件下的菜单项
menuFile.add(itemNew);
menuFile.add(itemOpen);
menuFile.add(itemSave);
menuFile.add(itemSaveAs);
menuFile.addSeparator(); //加分隔符
menuFile.add(itemPageSetup);
menuFile.add(itemPrint);
menuFile.addSeparator(); //加分隔符
menuFile.add(itemExit);
//添加编辑下的菜单项
menuEdit.add(itemUndo);
menuEdit.addSeparator(); //加分隔符
menuEdit.add(itemCut);
menuEdit.add(itemCopy);
menuEdit.add(itemPaste);
menuEdit.add(itemDelete);
menuEdit.addSeparator(); //加分隔符
menuEdit.add(itemFind);
menuEdit.add(itemFindNext);
menuEdit.add(itemReplace);
menuEdit.add(itemGoto);
menuEdit.addSeparator(); //加分隔符
menuEdit.add(itemCheckAll);
menuEdit.add(itemTimeAndDate);
//添加格式下的菜单项
menuFormat.add(itemLineWrap);
menuFormat.add(itemFont);
//添加查看下的菜单项
menuView.add(itemStatusBar);
//添加帮助下的菜单项
menuHelp.add(itemHelpTopic);
menuHelp.addSeparator(); //加分隔符
menuHelp.add(itemAboutNotepad);
//在菜单栏中添加主菜单
menuB.add(menuFile);
menuB.add(menuEdit);
menuB.add(menuFormat);
menuB.add(menuView);
menuB.add(menuHelp);
setMenuBar(menuB);
//编辑框生成
edit=new JTextArea();
edit.setForeground(Color.black);
//工具栏生成
JToolBar jt_ToolBar=new JToolBar();
//JPanel jp_Panel=new JPanel(new FlowLayout()); //工具栏面板
JButton jb_New=new JButton(new ImageIcon("new.gif"));
JButton jb_Open=new JButton(new ImageIcon("open2.gif"));
JButton jb_Close=new JButton(new ImageIcon("close2.gif"));
JButton jb_Save=new JButton(new ImageIcon("save2.gif"));
JButton jb_Print=new JButton(new ImageIcon("print2.gif"));
//LPanel lp_CBAFTPanel=new LPanel(new FlowLayout()); //存放标签和组合框的面板
JLabel jl_FontTypeLabel=new JLabel("字体类型:"); //设置标签”字体类型“
//JComboBox jc_ComboBox=new JComboBox(); //设置字体类型的组合框
Choice jc_ComboBox=new Choice();
//为组合框添加字体类型
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
for(int i=0;i<fontName.length;i++)
{
jc_ComboBox.add(fontName[i]);
}
JLabel jl_FontSizeLabel=new JLabel("字体大小:"); //设置标签”字体大小“
JSlider js_Slider=new JSlider(); //设置一个滑杆
js_Slider.putClientProperty("JSlider.isFilled",Boolean.TRUE);
js_Slider.setPaintLabels(true);
js_Slider.setPaintTicks(true);
js_Slider.setMajorTickSpacing(20);
js_Slider.setMinorTickSpacing(5);
//JPanel jp_SAFSPanel=new JPanel(new FlowLayout()); //存放标签和滑杆的面板
//JButton jb_SliderAndFontSize=new JButton(); //
//lb_ComboBoxAndFontType.setVerticalTextPosition(AbstractButton.BOTTOM);
//lb_ComboboxAndFontType.setHorizontalTextPosition(AbstractButton.LEFT);
//jp_CBAFTPanel.add(jl_FontTypeLabel); //将标签添加到面板中
//jp_CBAFTPanel.add(jc_ComboBox); //将组合框添加到面板中
//jb_ComboBoxAndFontType.add(jp_CBAFTPanel); //将面板添加到按钮中
//jp_SAFSPanel.add(jl_FontSizeLabel); //将标签添加到面板中
//jp_SAFSPanel.add(js_Slider); //将滑杆添加到面板中
//jb_SliderAndFontSize.add(jp_SAFSPanel); //将面板添加到按钮中
jt_ToolBar.add(jb_New); //将新建按钮添加到面板中
jt_ToolBar.add(jb_Open);//将打开按钮添加到面板中
jt_ToolBar.add(jb_Close);//将关闭按钮添加到面板中
jt_ToolBar.add(jb_Save);//将保存按钮添加到面板中
jt_ToolBar.add(jb_Print);//将打印按钮添加到面板中
jt_ToolBar.add(jl_FontTypeLabel);
jt_ToolBar.add(jc_ComboBox);
jt_ToolBar.add(jl_FontSizeLabel);
jt_ToolBar.add(js_Slider);
//jt_ToolBar.add(jb_ComboBoxAndFontType); //将字体类型添加到面板中
//jt_ToolBar.add(jb_SliderAndFontSize); //将字体大小添加到面板中
//给工具栏中的按钮添加事件
jb_New.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
new WindowText("我的编辑器");
}
});
jb_Open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
filedialog_load.setVisible(true);
edit.setText(null);
String s;
if(filedialog_load.getFile()!=null)
{ try{ File file= new File(filedialog_load.getDirectory(),filedialog_load. getFile());
file_reader=new FileReader(file);
in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
edit.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOException e2){}
}
}
});
jb_Close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(edit.getText()!=null)
{
int selection = JOptionPane.showConfirmDialog(null,"该文件还没有保存,是否保存?", "消息对话框",JOptionPane.YES_NO_CANCEL_OPTION);
//如果选择了“是”按钮,则弹出“保存”对话框
if (selection == JOptionPane.YES_OPTION)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{
try
{ File file=new File(filedialog_save.getDirectory(),filedialog_save. getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(edit.getText(),0,(edit.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
System.exit(0);
}
//如果选择了“否”按钮,则退出窗口
else if(selection==JOptionPane.NO_OPTION)
{
System.exit(0);
}
else
filedialog_save.setVisible(false);
}
else
{
System.exit(0);
}
}
});
jb_Save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{
try
{ File file=new File(filedialog_save.getDirectory(),filedialog_save. getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(edit.getText(),0,(edit.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
});
jb_Print.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
//给组合框添加事件
jc_ComboBox.addItemListener(this);
//给滑杆添加事件
js_Slider.addChangeListener(this);
/* jc_ComboBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e)
{
String name=(String)(jc_ComboBox.getSelectedItem());
Font f=new Font(name,Font.BOLD,32);
edit.setFont(f);
}
});
*/
/*js_Slider.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e)
{
int a=js_Slider.getValue();
String name=(String)(jc_ComboBox.getSelectedItem());
Font f=new Font(name,Font.BOLD,a);
edit.setFont(f);
}
});
*/
//setToolBar(jt_ToolBar);
//panelMenu=new Panel(new FlowLayout());
//panelEdit=new Panel(new FlowLayout());
//panelMenu.add(menuB);
//panelEdit.add(edit);
setLayout(new BorderLayout());
//add(panelMenu);
//add(panelEdit);
//add(menuB,BorderLayout.NORTH);
//add(jp_Panel,BorderLayout.NORTH);
add(jt_ToolBar,BorderLayout.NORTH);
add(edit,BorderLayout.CENTER);
//对两个(打开,关闭)对话框设置事件
filedialog_save.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false);
}
});
filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器
{public void windowClosing(WindowEvent e)
{ filedialog_load.setVisible(false);
}
});
/*addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
*/
//"文件"下的菜单事件
itemNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
new WindowText("我的编辑器");
}
});
itemOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
filedialog_load.setVisible(true);
edit.setText(null);
String s;
if(filedialog_load.getFile()!=null)
{ try{ File file= new File(filedialog_load.getDirectory(),filedialog_load. getFile());
file_reader=new FileReader(file);
in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
edit.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOException e2){}
}
}
});
itemSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{ try { File file=new File(filedialog_save.getDirectory(),filedialog_save. getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(edit.getText(),0,(edit.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
});
itemSaveAs.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{ try { File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(edit.getText(),0,(edit.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
});
itemPrint.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemPageSetup.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//"编辑"下的菜单事件
itemUndo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemCopy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
try{
String temp=edit.getSelectedText();
StringSelection edit1=new StringSelection(temp);
clipboard1.setContents(edit1,null);
}catch(Exception ee){System.out.println(ee);}
}
});
itemCut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// System.exit(0);
try{
String temp=edit.getSelectedText();
StringSelection edit1=new StringSelection(temp);
clipboard1.setContents(edit1,null);
int start=edit.getSelectionStart();
int end=edit.getSelectionEnd();
edit.replaceRange("",start,end);
}catch(Exception ee){System.out.println(ee);}
}
});
itemPaste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
//try{
Transferable contents=clipboard1.getContents(this);
DataFlavor flavor=DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor))
try{
//{
String str;
str=(String)contents.getTransferData(flavor);
edit.append(str);
//}
}
catch(Exception ee){System.out.println(ee);}
}
});
itemDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//System.exit(0);
//try{
try{
//String temp=edit.getSelectedText();
//StringSelection edit1=new StringSelection(temp);
//clipboard1.setContents(edit1,null);
int start=edit.getSelectionStart();
int end=edit.getSelectionEnd();
edit.replaceRange("",start,end);
}catch(Exception ee){System.out.println(ee);}
}
});
itemFind.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemFindNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemReplace.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemGoto.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemCheckAll.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemTimeAndDate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
//格式下的菜单事件
itemLineWrap.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemFont.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
//"查看"下的菜单事件
itemStatusBar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
//"帮助"下的菜单事件
itemHelpTopic.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
}
});
itemAboutNotepad.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null,"此项功能还没有完成!!","警告对话框", JOptionPane.WARNING_MESSAGE);
mydialog.setVisible(true);
}
});
//窗口退出
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//System.exit(0);
//新建确认型对话框
if(edit.getText()!=null)
{
int selection = JOptionPane.showConfirmDialog(null,"该文件还没有保存,是否保存?", "消息对话框",JOptionPane.YES_NO_CANCEL_OPTION);
//如果选择了“是”按钮,则弹出“保存”对话框
if (selection == JOptionPane.YES_OPTION)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{ try { File file=new File(filedialog_save.getDirectory(),filedialog_save. getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(edit.getText(),0,(edit.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
System.exit(0);
}
//如果选择了“否”按钮,则退出窗口
else if(selection==JOptionPane.NO_OPTION)
{
System.exit(0);
}
else
filedialog_save.setVisible(false);
}
else
{
System.exit(0);
}
}
});
pack();
setBounds(150,150,800,500);
setVisible(true);
//validate();
}//与105行对应
//给字体类型组合框设置事件
/*public void itemStateChanged(ItemEvent e)
{
String name=list.getSelectedItem();
Font f=new Font(name,Font.BOLD,32);
text.setFont(f);
text.setText("\nWelcome 欢迎");
}
*/
public void itemStateChanged(ItemEvent e)
{
//String text=jc_ComboBox.getText();
String name=jc_ComboBox.getSelectedItem();
Font f=new Font("宋体",Font.BOLD,50);
//edit.setText("胡");
edit.setFont(f);
//validate();
}
//给滑杆设置事件
public void stateChanged(ChangeEvent e)
{
int a=js_Slider.getValue();
String name=(String)(jc_ComboBox.getSelectedItem());
//Font name=getFont();
//String aa=(String)(name);
Font f=new Font(name,Font.BOLD,a);
edit.setFont(f);
}
}//与第10行的对应
class MyDialog extends JDialog //建立关于记事本对话框
{
MyDialog(Frame f,String s,boolean b)
{
super(f,s,b);
// 得到image图像文件
//Image image1 = Toolkit.getDefaultToolkit().getImage("java8.jpg");
JButton but=new JButton("确定");
JLabel lab1=new JLabel("创建人:李建鹤");
JLabel lab2=new JLabel("日期:2008.10.05");
ImagePanel imagepanel=new ImagePanel();
JPanel lab_panel=new JPanel(null);
lab_panel.add(lab1);
lab1.setBounds(20,30,150,100);
lab_panel.add(lab2);
lab2.setBounds(20,50,150,100);
//lab_panel.add(null);
JPanel center_panel=new JPanel(new GridLayout(1,2));
center_panel.add(imagepanel);
center_panel.add(lab_panel);
JPanel but_panel=new JPanel(new BorderLayout());
but_panel.add(but,BorderLayout.EAST);
setLayout(new BorderLayout());
Container contentPane = getContentPane();
contentPane.add(center_panel,BorderLayout.CENTER);
// add(imagepanel,BorderLayout.WEST);
//contentPane.add(lab,BorderLayout.NORTH);
contentPane.add(but_panel,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
setBounds(100,100,300,200);
//setVisible(false);
}
}//与789行对应
class ImagePanel extends JPanel
{
ImagePanel()
{
// 得到image图像文件
image1 = Toolkit.getDefaultToolkit().getImage("java8.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image1, 1);
try
{
tracker.waitForID(1);
}
catch (InterruptedException exception){}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//显示image
g.drawImage(image1,10, 10, null);
}
private Image image1;
}//与840行对应
public class Win_Text
{
public static void main(String argv[])
{
new WindowText("我的记事本");
}
}//与864行对应