第八章.图形界面设计
主要内容:8.1 Swing基础 8.2 Swing常用组件 8.3 事件 8.4 布局管理器
案例-简易文本编辑器的制作
简易文本编辑器主要功能如图8.1所示,利用简易文本编辑器,可以进行文本的输入、复制、剪切、粘贴及选择操作,还可以根据需要设置文字的大小及字形。
如实现本例功能需使用到本章介绍的多个知识点:
1.Swing常用组件的定义和构造;
2.布局管理器:本例中主要使用了边界布局BorderLayout,流布局FlowLayout及网格布局GridLauout;
3.JAVA事件:事件处理机制,组件的常用事件,事件处理机制的实际应用。
应用效果图如下:
代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.*;
import java.awt.datatransfer.*;
public class TxtEditor extends JFrame
{
JFrame frmAbout;
JTextArea taArea;
String name=null;
String board=null;
private Clipboard cb;
JScrollPane scroll;
JPanel PanelNorth,PanelSouth,PanelWest,PanelEast,PanelCenter,PanelLeftFontSize,PanelLeftFontType;
JButton btnNew,btnOpen,btnSave,btnSaveAnother,btnCopy,btnPaste,btnCut,btnDelete,btnSelectAll,btnHelp;
JRadioButton jrbFontSize10,jrbFontSize20,jrbFontSize30;
JCheckBox jcbBold,jcbItalic;
ButtonGroup jbgFontSize;
JLabel lblTitle;
public TxtEditor() throws Exception{
super("简易文本编辑器");Toolkit kit=Toolkit.getDefaultToolkit();
PanelNorth=new JPanel();PanelSouth=new JPanel();PanelWest=new JPanel();PanelEast=new JPanel();
PanelCenter=new JPanel();PanelLeftFontSize=new JPanel();PanelLeftFontType=new JPanel();
btnNew=new JButton("新建");btnOpen=new JButton("打开");btnSave=new JButton("保存");
btnSaveAnother=new JButton("另存为");btnCopy=new JButton("复制");btnPaste=new JButton("粘贴");
btnCut=new JButton("剪切");btnDelete=new JButton("删除");btnSelectAll=new JButton("全选");
btnHelp=new JButton("帮助");
jrbFontSize10=new JRadioButton("10",true);jrbFontSize20=new JRadioButton("20");jrbFontSize30=new JRadioButton("30");
jcbBold=new JCheckBox("粗体",false);jcbItalic=new JCheckBox("斜体",false);
taArea=new JTextArea(10,50);taArea.setFont(new Font("黑体",Font.BOLD,15));
scroll=new JScrollPane(taArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jbgFontSize=new ButtonGroup();lblTitle=new JLabel("简易文本编辑器");
jbgFontSize.add(jrbFontSize10);jbgFontSize.add(jrbFontSize20);jbgFontSize.add(jrbFontSize30);
PanelNorth.add(lblTitle);
PanelSouth.add(btnNew);PanelSouth.add(btnOpen);PanelSouth.add(btnSave);PanelSouth.add(btnSaveAnother);
PanelSouth.add(btnCopy);PanelSouth.add(btnPaste);PanelSouth.add(btnCut);PanelSouth.add(btnDelete);
PanelSouth.add(btnSelectAll);
PanelSouth.add(btnHelp);
PanelWest.setLayout(new GridLayout(6,1));
PanelLeftFontSize.add(jrbFontSize10);PanelLeftFontSize.add(jrbFontSize20);
PanelLeftFontSize.add(jrbFontSize30);
PanelLeftFontType.add(jcbBold);PanelLeftFontType.add(jcbItalic);
PanelWest.add(PanelLeftFontSize);PanelWest.add(PanelLeftFontType);
PanelCenter.add(scroll);
this.add(PanelNorth,BorderLayout.NORTH);this.add(PanelSouth,BorderLayout.SOUTH);this.add(PanelWest,BorderLayout.WEST);
this.add(PanelEast,BorderLayout.EAST);this.add(PanelCenter,BorderLayout.CENTER);
btnSave.addActionListener(new MyListenerSave());
btnOpen.addActionListener(new MyListenerOpen());
btnNew.addActionListener(new MyListenerNew());
btnSaveAnother.addActionListener(new MyListenerAnother());
btnCopy.addActionListener(new MyListenerCopy());
btnPaste.addActionListener(new MyListenerPaste());
btnCut.addActionListener(new MyListenerCut());
btnDelete.addActionListener(new MyListenerDelete());
btnSelectAll.addActionListener(new MyListenerSelectAll());
jcbBold.addItemListener(new MyListenerjcbBold());
jcbItalic.addItemListener(new MyListenerjcbItalic());
jrbFontSize10.addActionListener(new MyListenerjrbFontSize10());
jrbFontSize20.addActionListener(new MyListenerjrbFontSize20());
jrbFontSize30.addActionListener(new MyListenerjrbFontSize30());
btnHelp.addActionListener(new MyListenerHelp());
this.setSize(800,320);
this.setLocation(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public class MyListenerSave implements ActionListener{
public void actionPerformed(ActionEvent e)
{
try{
saveText();
}catch(Exception ex)
{
}
}
}
public class MyListenerOpen implements ActionListener{
public void actionPerformed(ActionEvent e)
{
try{
openText();
}catch(Exception ex)
{
}
}
}
public class MyListenerNew implements ActionListener{
public void actionPerformed(ActionEvent e)
{
try{
taArea.setText("");
name=null;
}catch(Exception ex)
{}
}
}
public class MyListenerAnother implements ActionListener{
public void actionPerformed(ActionEvent e)
{
anotherSaveText();
}
}
public class MyListenerCopy implements ActionListener{
public void actionPerformed(ActionEvent e)
{
board=taArea.getSelectedText();
cb.setContents(new StringSelection(board),null);
}
}
public class MyListenerPaste implements ActionListener{
public void actionPerformed(ActionEvent e)
{
try{
taArea.setForeground(Color.BLACK);
Transferable content=cb.getContents(null);
String st=(String)content.getTransferData(DataFlavor.stringFlavor);
taArea.replaceRange(st,taArea.getSelectionStart(),taArea.getSelectionEnd());
}catch(Exception ex)
{}
}
}
public class MyListenerCut implements ActionListener{
public void actionPerformed(ActionEvent e)
{
board=taArea.getSelectedText();
cb.setContents(new StringSelection(board),null);
taArea.replaceRange("",taArea.getSelectionStart(),taArea.getSelectionEnd());
}
}
public class MyListenerDelete implements ActionListener{
public void actionPerformed(ActionEvent e)
{
int result=JOptionPane.showConfirmDialog(null,"您确定要删除选定文本?","确认对话框",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION)
{
taArea.replaceRange("",taArea.getSelectionStart(),taArea.getSelectionEnd());
}
}
}
public class MyListenerSelectAll implements ActionListener{
public void actionPerformed(ActionEvent e)
{
taArea.setSelectionStart(0);
taArea.setSelectionEnd(taArea.getText().length());
taArea.setForeground(Color.BLUE);
}
}
public class MyListenerjrbFontSize10 implements ActionListener{
public void actionPerformed(ActionEvent e){
Font oldFont=taArea.getFont();
Font newFont=new Font(oldFont.getFontName(),oldFont.getStyle(),10);
taArea.setFont(newFont);
}
}
public class MyListenerjrbFontSize20 implements ActionListener{
public void actionPerformed(ActionEvent e){
Font oldFont=taArea.getFont();
Font newFont=new Font(oldFont.getFontName(),oldFont.getStyle(),20);
taArea.setFont(newFont);
}
}
public class MyListenerjrbFontSize30 implements ActionListener{
public void actionPerformed(ActionEvent e){
Font oldFont=taArea.getFont();
Font newFont=new Font(oldFont.getFontName(),oldFont.getStyle(),30);
taArea.setFont(newFont);
}
}
public class MyListenerjcbBold implements ItemListener{
public void itemStateChanged(ItemEvent e)
{
Font oldFont=taArea.getFont();
int style=oldFont.getStyle();
if(jcbBold.isSelected()){
style+=Font.BOLD;
}
else
{
style-=Font.BOLD;
}
Font newFont=new Font(oldFont.getFontName(),style,oldFont.getSize());
taArea.setFont(newFont);
}
}
public class MyListenerjcbItalic implements ItemListener{
public void itemStateChanged(ItemEvent e){
Font oldFont=taArea.getFont();
int style=oldFont.getStyle();
if(jcbItalic.isSelected()){
style+=Font.ITALIC;
}
else
{
style-=Font.ITALIC;
}
Font newFont=new Font(oldFont.getFontName(),style,oldFont.getSize());
taArea.setFont(newFont);
}
}
public class MyListenerHelp implements ActionListener{
public void actionPerformed(ActionEvent e){
frmAbout=new JFrame("关于");
frmAbout.setSize(200,100);
frmAbout.setLocation(400,300);
JTextArea area1=new JTextArea("制作人 :邹洪侠\n制作时间: 2014.5.24");
frmAbout.add(area1);
frmAbout.setVisible(true);
}
}
public void openText() //打开
{
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showOpenDialog(TxtEditor.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
setTitle(name);
try{
BufferedReader in=new BufferedReader(new FileReader(name));
String line=null;
String datas="";
while((line=in.readLine())!=null)
{
if(datas=="")
{
datas=datas+line;
}
else
{
datas=datas+"\n"+line;
}
}
taArea.setText(datas);
in.close();
}catch(Exception ex)
{
}
}
}
public void saveText() //保存
{
if(name==null)
{
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showSaveDialog(TxtEditor.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
try{
OutputStream out=new FileOutputStream(name);
String datas=taArea.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
else
{
try{
OutputStream out=new FileOutputStream(name);
String datas=taArea.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
public void anotherSaveText() //另存为
{
JFileChooser chooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("Files","txt","java");
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(new File("."));
int result=chooser.showSaveDialog(TxtEditor.this);
if(result==JFileChooser.APPROVE_OPTION)
{
name=chooser.getSelectedFile().getPath();
try{
OutputStream out=new FileOutputStream(name);
String datas=taArea.getText();
out.write(datas.getBytes());
out.close();
}catch(Exception ex)
{
}
}
}
/*public void showDialog()
{
dialog=new JDialog();
dialog.setSize(300, 200);
dialog.setLocation(400, 300);
dialog.setModal(true);
dialog.setTitle("确认对话框");
dialog.add(new JLabel("您确实要删除选定文本吗?"),BorderLayout.CENTER);
dialog.add(new JPanel(),BorderLayout.WEST);
JButton btnYes=new JButton("确认");
JButton btnNo=new JButton("取消");
JPanel panel=new JPanel();
panel.add(btnYes);panel.add(btnNo);
dialog.add(panel,BorderLayout.SOUTH);
dialog.setVisible(true);
btnYes.addActionListener(new ActionListener() //确认
{
public void actionPerformed(ActionEvent e)
{
taArea.replaceRange("",taArea.getSelectionStart(),taArea.getSelectionEnd());
dialog.setVisible(false);dialog.dispose();
}
});
btnNo.addActionListener(new ActionListener() //取消
{
public void actionPerformed(ActionEvent e)
{
dialog.setVisible(false);dialog.dispose();
}
});
}*/
public static void main(String[] args)throws Exception
{
new TxtEditor();
}
}
因内容过多,请关注本人的下载资料,有详细的课件