📄 mynotepad.java
字号:
return "JAVA类文件(*.class)"; } return ""; }}class Win extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); }}//############################################字体设置类###################################class SetFont extends JFrame implements ActionListener { /** * 模拟系统记事本的格式菜单下的字体设置菜单,并新增加了颜色设置功能 */// private static final long serialVersionUID = 1L; final JDialog fontDialog; final JTextField tfFont, tfSize, tfStyle; final int fontStyleConst[] = {Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD + Font.ITALIC }; final JList listStyle, listFont, listSize; Color backgroundColor, fontColor; JLabel sample; Font newFont; JButton fontOkButton; JTextArea editArea;//构造函数SetFont public SetFont(JTextArea tex) { fontDialog = new JDialog(this, "字体设置", true); Container con = fontDialog.getContentPane(); con.setLayout(new BorderLayout()); editArea = tex; backgroundColor = editArea.getBackground(); fontColor = editArea.getForeground(); JPanel topPanel = new JPanel(); //存放字体、字形、字号、颜色、预览面板的面板 topPanel.setLayout(new GridLayout(2, 3)); JPanel basePanel = new JPanel(); //存放确定和取消按钮的面板 basePanel.setLayout(new FlowLayout()); Font currentFont = editArea.getFont();//获取当前编辑文本的字体 JPanel fontPanel = new JPanel(); //字体设置面板 JPanel stylePanel = new JPanel(); //字形设置面板 JPanel sizePanel = new JPanel(); //字号设置面板 JPanel fontcolorPanel = new JPanel(); //字体颜色设置面板 fontcolorPanel.setLayout(new BorderLayout()); JPanel backgroundcolorPanel = new JPanel(); //背景颜色设置面板 backgroundcolorPanel.setLayout(new BorderLayout()); JPanel demoPanel = new JPanel(new BorderLayout()); //预览面板 final JLabel bgclabel = new JLabel("单击改变背景颜色"); bgclabel.setOpaque(true); //设置透明 bgclabel.setHorizontalAlignment(SwingConstants.CENTER); //设置文字剧中 bgclabel.setBackground(Color.WHITE); //设置背景色为白色,此处为初始化的文本区背景色 bgclabel.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Color color = JColorChooser.showDialog(SetFont.this, "设置背景颜色", Color.BLACK); if (color != null) //若为null值表示用户按下Cancel按钮 { backgroundColor = color; bgclabel.setBackground(backgroundColor); sample.setBackground(backgroundColor); } } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }); final JLabel fclabel = new JLabel("单击改变字体颜色"); fclabel.setHorizontalAlignment(SwingConstants.CENTER); fclabel.setOpaque(true); fclabel.setBackground(Color.BLACK); //此处为设置字体的颜色,初始化为黑色 fclabel.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Color color = JColorChooser.showDialog(SetFont.this, "设置字体颜色", Color.BLACK); if (color != null) //若为null值表示用户按下Cancel按钮 { fontColor = color; fclabel.setBackground(fontColor); sample.setForeground(fontColor); } } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }); JLabel lblFont = new JLabel("字体(F):"); lblFont.setPreferredSize(new Dimension(80, 20)); JLabel lblStyle = new JLabel("字形(Y):"); lblStyle.setPreferredSize(new Dimension(80, 20)); JLabel lblSize = new JLabel("大小(S):"); lblSize.setPreferredSize(new Dimension(80, 20)); tfFont = new JTextField(12); //设置可显示的列数 tfFont.setText(currentFont.getFontName()); tfFont.selectAll(); tfFont.setPreferredSize(new Dimension(20, 20)); tfStyle = new JTextField(10); if (currentFont.getStyle() == Font.PLAIN) { tfStyle.setText("常规"); } else if (currentFont.getStyle() == Font.BOLD) { tfStyle.setText("粗体"); } else if (currentFont.getStyle() == Font.ITALIC) { tfStyle.setText("斜体"); } else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC)) { tfStyle.setText("粗斜体"); } tfFont.selectAll(); tfStyle.setPreferredSize(new Dimension(20, 20)); tfSize = new JTextField(8); tfSize.setText(currentFont.getSize() + ""); tfSize.selectAll(); tfSize.setPreferredSize(new Dimension(20, 20)); final String fontStyle[] = {"常规", "粗体", "斜体", "粗斜体"}; listStyle = new JList(fontStyle); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); final String fontName[] = ge.getAvailableFontFamilyNames(); //获取系统的本地字体 int defaultFontIndex = 0; for (int i = 0; i < fontName.length; i++) { if (fontName[i].equals(currentFont.getFontName())) { defaultFontIndex = i; break; } } listFont = new JList(fontName); listFont.setSelectedIndex(defaultFontIndex); listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //单选 listFont.setVisibleRowCount(5); listFont.setFixedCellWidth(120); listFont.setFixedCellHeight(15); listFont.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { tfFont.setText(fontName[listFont.getSelectedIndex()]); tfFont.requestFocus(); tfFont.selectAll(); updateSample(); } }); listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); if (currentFont.getStyle() == Font.PLAIN) { listStyle.setSelectedIndex(0); } else if (currentFont.getStyle() == Font.BOLD) { listStyle.setSelectedIndex(1); } else if (currentFont.getStyle() == Font.ITALIC) { listStyle.setSelectedIndex(2); } else if (currentFont.getStyle() == (Font.BOLD + Font.ITALIC)) { listStyle.setSelectedIndex(3); } listStyle.setVisibleRowCount(5); listStyle.setFixedCellWidth(110); listStyle.setFixedCellHeight(15); listStyle.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]); tfStyle.requestFocus(); tfStyle.selectAll(); updateSample(); } }); final String fontSize[] = {"8", "9", "10", "12", "14", "15", "16", "18", "20", "21", "22", "24", "26", "28", "30", "36", "48", "54", "72", "89" }; listSize = new JList(fontSize); int defaultFontSizeIndex = 0; for (int i = 0; i < fontSize.length; i++) { if (fontSize[i].equals(currentFont.getSize() + "")) { defaultFontSizeIndex = i; break; } } listSize.setSelectedIndex(defaultFontSizeIndex); listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listSize.setVisibleRowCount(5); listSize.setFixedCellWidth(70); listSize.setFixedCellHeight(15); listSize.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { tfSize.setText(fontSize[listSize.getSelectedIndex()]); tfSize.requestFocus(); tfSize.selectAll(); updateSample(); } }); fontOkButton = new JButton("确定"); fontOkButton.addActionListener(this); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fontDialog.dispose(); } }); sample = new JLabel("Dave记事本"); sample.setOpaque(true); sample.setHorizontalAlignment(SwingConstants.CENTER); sample.setBackground(Color.WHITE); //设置背景色 sample.setForeground(Color.BLACK); //设置前景色,即字的颜色// sample.setPreferredSize(new Dimension(130, 100)); //设置文本域的大小 demoPanel.setBorder(BorderFactory.createTitledBorder("预览")); //这是边框标题 demoPanel.add(sample, BorderLayout.CENTER); fontPanel.add(lblFont); fontPanel.add(tfFont); fontPanel.add(new JScrollPane(listFont)); stylePanel.add(lblStyle); stylePanel.add(tfStyle); stylePanel.add(new JScrollPane(listStyle)); sizePanel.add(lblSize); sizePanel.add(tfSize); sizePanel.add(new JScrollPane(listSize)); fontcolorPanel.setBorder(BorderFactory.createTitledBorder("字体颜色")); fontcolorPanel.add(fclabel, BorderLayout.CENTER); backgroundcolorPanel.setBorder(BorderFactory.createTitledBorder("背景颜色")); backgroundcolorPanel.add(bgclabel, BorderLayout.CENTER); basePanel.add(fontOkButton); basePanel.add(cancelButton); topPanel.add(fontPanel); topPanel.add(stylePanel); topPanel.add(sizePanel); topPanel.add(fontcolorPanel); topPanel.add(backgroundcolorPanel); topPanel.add(demoPanel); con.add(basePanel, BorderLayout.SOUTH); con.add(topPanel, BorderLayout.CENTER); updateSample(); fontDialog.setSize(450, 340); fontDialog.setLocation(200, 200); fontDialog.setResizable(false); fontDialog.setVisible(true); }//构造函数结束//设置文本编辑区的字体 public void actionPerformed(ActionEvent e) { if (e.getSource() == fontOkButton) { newFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText())); fontDialog.dispose(); editArea.setFont(newFont); editArea.setBackground(backgroundColor); editArea.setForeground(fontColor); } }//End method actionPerformed public Font getFont() { return newFont; } public Color getbackgroundColor() { return backgroundColor; } public Color getfontColor() { return fontColor; }//更新示例显示的字体和风格大小等 public void updateSample() { Font sampleFont = new Font(tfFont.getText(), fontStyleConst[listStyle.getSelectedIndex()], Integer.parseInt(tfSize.getText())); sample.setFont(sampleFont); }//End method updateSample} //##########################################################End of class SetFont######################public class MyNotepad { public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); Font font = new Font("Dialog", Font.PLAIN, 12); //一下是改变默认的组建上显示的字体,这样更加美观一些 UIManager.put("MenuBar.font", font); UIManager.put("MenuItem.font", font); UIManager.put("Menu.font", font); UIManager.put("PopupMenu.font", font); UIManager.put("ToolBar.font", font); UIManager.put("ToolTip.font", font); UIManager.put("TabbedPane.font", font); UIManager.put("Label.font", font); UIManager.put("List.font", font); UIManager.put("ComboBox.font", font); UIManager.put("Button.font", font); UIManager.put("Table.font", font); UIManager.put("TableHeader.font", font); UIManager.put("Tree.font", font); UIManager.put("TextField.font", font); UIManager.put("TextArea.font", font); UIManager.put("TitledBorder.font", font); UIManager.put("OptionPane.font", font); UIManager.put("RadioButton.font", font); UIManager.put("CheckBox.font", font); UIManager.put("ToggleButton.font", font); UIManager.put("Dialog.font", font); UIManager.put("Panel.font", font); } catch (Exception e) {} DaveNotepad frm = new DaveNotepad(); frm.addWindowListener(new Win()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -