📄 menufont.java
字号:
package teditor;import java.awt.*;import java.awt.List;import java.awt.event.*;import javax.swing.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2006</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class MenuFont extends JDialog implements ItemListener,ActionListener,TextListener{ CheckboxGroup gp; Checkbox plain,bold,italic,bolditalic; Choice fontNameChoice=new Choice(); Choice color=new Choice(); TextField tf; Button ok=new Button("ok"); Button cancel=new Button("cancel"); boolean changed=true; int fontStyleInt=0; int fontSizemin=10,fontSizemax=36,ab=2; List fst; MenuFont(Frame frame,String s, boolean modal){ super(frame,s,modal);} public Font myLayout(Font tafont){ this.getContentPane().setLayout(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints();//网格包约束条件对象 gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx=1; gbc.weighty=1; gbc.fill=gbc.HORIZONTAL; gbc.anchor=gbc.CENTER;//锚点 //布局 //字号文本框 tf=new TextField(tafont.getSize()+"",14); tf.selectAll(); tf.addTextListener(this); gbc.gridx=0; gbc.gridy=1; this.getContentPane().add(tf,gbc); //字体复选 gp=new CheckboxGroup(); if (tafont.getStyle()==0) plain =new Checkbox("Plain",true,gp); else plain =new Checkbox("Plain",false,gp); gbc.gridx=0; gbc.gridy=0; getContentPane().add(plain,gbc); plain.addItemListener(this); if (tafont.getStyle()==1) bold =new Checkbox("Bold",true,gp); else bold =new Checkbox("Bold",false,gp); gbc.gridx=1; gbc.gridy=0; getContentPane().add(bold,gbc); plain.addItemListener(this); if (tafont.getStyle()==2) italic =new Checkbox("Italic",true,gp); else italic =new Checkbox("Italic",false,gp); gbc.gridx=2; gbc.gridy=0; getContentPane().add(italic,gbc); plain.addItemListener(this); if (tafont.getStyle()==3) bolditalic =new Checkbox("Bolditalic",true,gp); else bolditalic =new Checkbox("Bolditalic",false,gp); gbc.gridx=3; gbc.gridy=0; getContentPane().add(bolditalic,gbc); plain.addItemListener(this); //字号列表 fst= new List(4,false); fst.addItemListener(this); int index=0; boolean bk=false; for(int i=fontSizemin;i<fontSizemax;i=i+ab) { if(!bk){ if(i!=tafont.getSize()) { index++; }else{bk=true;} } fst.add(i+""); } fst.select(index); gbc.gridx=0; gbc.gridy=2; getContentPane().add(fst,gbc); //字体列表 gbc.gridx=1; gbc.gridy=1; fontNameChoice.addItemListener(this); fontNameChoice.add("Serif"); fontNameChoice.add("Courier"); fontNameChoice.add("Helvetica"); fontNameChoice.add("TimesRoman"); fontNameChoice.select(tafont.getName()); getContentPane().add(fontNameChoice,gbc); //添加按扭 gbc.gridx=2; gbc.gridy=2; gbc.fill=gbc.NONE; ok.addActionListener(this); getContentPane().add(ok,gbc); gbc.gridx=3; gbc.gridy=2; cancel.addActionListener(this); getContentPane().add(cancel,gbc); //窗口监听 this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {dispose();} }); this.setLocation(120,120); this.setResizable(false); this.setSize(470,160); this.setVisible(true); if(changed) return returnFont(); else return tafont; } //ITEM的监听方法 public void itemStateChanged(ItemEvent ie) { if(ie.getSource()==plain) { updateFontTextField(); } else if(ie.getSource()==bold) { updateFontTextField(); } else if(ie.getSource()==italic) { updateFontTextField(); } else if(ie.getSource()==bolditalic) { updateFontTextField(); } else if(ie.getSource()==fontNameChoice) { updateFontTextField(); } else if(ie.getSource()==fst){ List lt=(List)ie.getSource(); tf.setText(lt.getSelectedItem()); updateFontTextField(); } } //按扭监听方法 public void actionPerformed(ActionEvent ee) { if(ee.getSource()==ok) {changed=true; dispose(); } else if(ee.getSource()==cancel) { changed=false; dispose(); } } //文本监听 public void textValueChanged(TextEvent e){ int indexf=0; boolean breakf=false; int thisNum=0; for(int i=fontSizemin;i<fontSizemax;i=i+ab) { if(!breakf){ try{ thisNum=Integer.parseInt(tf.getText()); }catch (NumberFormatException nfe) { thisNum=0; } if(i!=thisNum){indexf++;} else{breakf=true;} } } updateFontTextField(); } //返回当前字体 public Font returnFont(){ updateFontTextField(); return new Font(fontNameChoice.getSelectedItem(),fontStyleInt, Integer.parseInt(fst.getSelectedItem())); } //对字体进行更新 private void updateFontTextField(){ if(gp.getSelectedCheckbox().getLabel().equals("Plain")) { fontStyleInt=Font.PLAIN; } else if(gp.getSelectedCheckbox().getLabel().equals("Bold")) { fontStyleInt=Font.BOLD; } else if(gp.getSelectedCheckbox().getLabel().equals("Italic")) { fontStyleInt=Font.ITALIC; } else if(gp.getSelectedCheckbox().getLabel().equals("Bolditalic")) { fontStyleInt=Font.ITALIC+Font.BOLD; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -