⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfontchooser.java~114~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~114~
字号:
package chat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.JTextPane;
import javax.swing.text.*;

public class JFontChooser extends JDialog {
  public static byte OK_OPTION = 0;
  public static byte CANCEL_OPTION = 1;
  private int OPTION;

  Font font;

  String[] styles;
  String[] sizes;




  static String fontFamily="宋体";
  static int fontStyle=Font.PLAIN;
  static int fontSize=12;
  static boolean underLine=false;
  static boolean strikeThrough=false;
  static boolean superscript=false;
  static boolean subscript=false;

  static Color selectedColor=null;
  Style def;
  StyledDocument doc;


  JPanel fullPanel;
  JPanel topPanel;
  JPanel centerPanel;
  JPanel undersidePanel;
  JPanel fontPanel;
  JPanel colorPanel;
  JPanel decoratePanel;
  JPanel effectPanel;
  JPanel viewPanel;
  JPanel fontFamilyPanel;
  JPanel fontStylePanel;
  JPanel fontSizePanel;
  JList fontList;
  JList sizeList;
  JList styleList;
  JScrollPane fontFamilyScroll;
  JScrollPane fontSizeScroll;
  JScrollPane styleScroll;
  JScrollPane sampleTextScroll;

  JButton okButton;
  JButton cancelButton;
  JButton selectColorButton;

  JCheckBox underLineCB;
  JCheckBox strikeThroughCB;
  JCheckBox superscriptCB;
  JCheckBox subscriptCB;

  JTextPane sampleText;

  JColorChooser colorChooser;


  Container con;


  public JFontChooser(JFrame frame){
    super(frame,true);
    setTitle("字体属性对话框");
    font=new Font("新宋体",Font.PLAIN,12);
    sizes=new String[]{"8","10","12","14","16","18","20","22","24","30","34"};
    styles=new String[]{"常规","粗体","斜体","粗斜体"};

    this.OPTION=this.CANCEL_OPTION;


    okButton=new JButton("确定");
    cancelButton=new JButton("取消");
    selectColorButton=new JButton("选择颜色");

    underLineCB=new JCheckBox("下划线");
    strikeThroughCB=new JCheckBox("删除线");
    superscriptCB=new JCheckBox("上标");
    subscriptCB=new JCheckBox("下标");

    underLineCB.setFont(font);
    strikeThroughCB.setFont(font);
    superscriptCB.setFont(font);
    subscriptCB.setFont(font);

    colorChooser=new JColorChooser();



    fullPanel=new JPanel();
    topPanel=new JPanel();
    centerPanel=new JPanel();
    undersidePanel=new JPanel();
    fontPanel=new JPanel();
    colorPanel=new JPanel();
    decoratePanel=new JPanel();
    effectPanel=new JPanel();
    viewPanel=new JPanel();
    fontFamilyPanel=new JPanel();
    fontStylePanel=new JPanel();
    fontSizePanel=new JPanel();
    fontList = new JList(GraphicsEnvironment.getLocalGraphicsEnvironment().
                 getAvailableFontFamilyNames())
                 {
                        public Dimension getPreferredScrollableViewportSize()
                        {
                          return new Dimension(160, 140);
                        }
                 };
    fontList.setFont(font);
    fontFamilyScroll=new JScrollPane(fontList);
    //fontFamilyPanel.setPreferredSize(new Dimension(170,150));
    fontFamilyPanel.add(fontFamilyScroll);

    sizeList=new JList(sizes){
      public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(60, 140);
      }
    };
    sizeList.setFont(font);

    fontSizeScroll=new JScrollPane(sizeList);

    fontSizePanel.add(fontSizeScroll);


    styleList=new JList(styles){
      public Dimension getPreferredScrollableViewportSize(){
        return new Dimension(60,140);
      }
    };
    styleList.setFont(font);
    styleScroll=new JScrollPane(styleList);
    fontStylePanel.add(styleScroll);

    fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //styleList.setPreferredSize(new Dimension(60,160));


   // fontFamilyPanel.setBorder(BorderFactory.createCompoundBorder(
     //           BorderFactory.createTitledBorder(
       //             "字体"),
         //       BorderFactory.createEmptyBorder(10,10,10,10)));
   sampleText=new JTextPane(){
     public Dimension getPreferredScrollableViewportSize() {
       return new Dimension(300, 100);
     }
   };
   sampleText.setEditable(false);


    sampleText.setFont(font);

    sampleText.setText("选择威尔,选择成功\nchoose WELL,choose to succeed");


    def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.
        DEFAULT_STYLE);

    //StyleConstants.setUnderline(def,true);
    //StyleConstants.setStrikeThrough(def,true);
    doc=sampleText.getStyledDocument();

    //doc.setCharacterAttributes(0,doc.getLength(),def,false);

    sampleTextScroll=new JScrollPane(sampleText);
    //sampleTextScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    //sampleTextScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    fullPanel.setBorder(BorderFactory.createRaisedBevelBorder());
    fontFamilyPanel.setBorder(BorderFactory.createTitledBorder("字体"));
    fontStylePanel.setBorder(BorderFactory.createTitledBorder("字形"));
    fontSizePanel.setBorder(BorderFactory.createTitledBorder("字体大小"));
    //colorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
    effectPanel.setBorder(BorderFactory.createTitledBorder("字体效果"));
    viewPanel.setBorder(BorderFactory.createTitledBorder("预览"));

    colorPanel.setPreferredSize(new Dimension(160,40));
    //effectPanel.setPreferredSize(new Dimension(320,60));

    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
      //System.exit(0);
       JFontChooser.this.setVisible(false);
       JFontChooser.this.OPTION=JFontChooser.this.CANCEL_OPTION;
      }});



    okButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        JFontChooser.this.OPTION=JFontChooser.OK_OPTION;

        JFontChooser.this.setVisible(false);

      }
    });
    cancelButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        JFontChooser.this.OPTION=JFontChooser.CANCEL_OPTION;
        JFontChooser.this.setVisible(false);

      }
    });

    selectColorButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        Color color=colorChooser.showDialog(null,"颜色选择对话框",selectedColor);
        if(color!=null){
          selectedColor=color;
          sampleText.setForeground(selectedColor);
        }
      }
    });

    ActionListener action=new ActionListener(){
      public void actionPerformed(ActionEvent e){
        if(e.getSource()==underLineCB){
          if(underLineCB.isSelected()){
            //StyledConstants.
            StyleConstants.setStrikeThrough(def,false);
            StyleConstants.setUnderline(def,true);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            underLine=true;
            strikeThrough=false;
            strikeThroughCB.setSelected(false);
          }else{
            StyleConstants.setUnderline(def,false);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            underLine=false;
          }
        }else if(e.getSource()==strikeThroughCB){
          if(strikeThroughCB.isSelected()){
            StyleConstants.setUnderline(def,false);
            StyleConstants.setStrikeThrough(def,true);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            strikeThrough=true;
            underLine=false;
            underLineCB.setSelected(false);
          }else{
            StyleConstants.setStrikeThrough(def,false);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            strikeThrough=false;
          }
        }else if(e.getSource()==superscriptCB){
          if(superscriptCB.isSelected()){
            StyleConstants.setSubscript(def,false);
            StyleConstants.setSuperscript(def,true);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            superscript=true;
            subscript=false;
            subscriptCB.setSelected(false);
          }else{
            StyleConstants.setSuperscript(def,false);
            doc.setCharacterAttributes(0,doc.getLength(),def,false);
            superscript=false;
          }

        }else if(e.getSource()==subscriptCB){
          if (subscriptCB.isSelected()) {
            StyleConstants.setSuperscript(def, false);
            StyleConstants.setSubscript(def, true);
            doc.setCharacterAttributes(0, doc.getLength(), def, false);
            subscript = true;
            superscript = false;
            superscriptCB.setSelected(false);
          }else {
            StyleConstants.setSubscript(def, false);
            doc.setCharacterAttributes(0, doc.getLength(), def, false);
            subscript = false;
          }
        }
      }
    };

    underLineCB.addActionListener(action);
    strikeThroughCB.addActionListener(action);
    superscriptCB.addActionListener(action);
    subscriptCB.addActionListener(action);

    fontPanel.add(fontFamilyPanel);
    fontPanel.add(fontStylePanel);
    fontPanel.add(fontSizePanel);

    decoratePanel.setLayout(new GridLayout(2,2));
    decoratePanel.add(underLineCB);
    decoratePanel.add(strikeThroughCB);
    decoratePanel.add(superscriptCB);
    decoratePanel.add(subscriptCB);

    colorPanel.add(selectColorButton);
    effectPanel.add(colorPanel,BorderLayout.NORTH);
    //effectPanel.setLayout(new BoxLayout(colorPanel,BoxLayout.Y_AXIS));
    //effectPanel.setLayout(new BoxLayout(decoratePanel,BoxLayout.Y_AXIS));
    effectPanel.add(decoratePanel,BorderLayout.SOUTH);
    viewPanel.add(sampleTextScroll);

    //centerPanel.setLayout(new GridLayout(3,1));
    centerPanel.add(fontPanel);
   // centerPanel.add(colorPanel);
    centerPanel.add(effectPanel);
    centerPanel.add(viewPanel);

    undersidePanel.add(okButton);
    undersidePanel.add(cancelButton);



    con=this.getContentPane();
    fullPanel.setLayout(new BorderLayout());
    fullPanel.add(topPanel,BorderLayout.NORTH);
    fullPanel.add(centerPanel,BorderLayout.CENTER);
    fullPanel.add(undersidePanel,BorderLayout.SOUTH);
    con.add(fullPanel);

    setSize(400,500);
    setResizable(false);

    setFont(null);


    ListSelectionListener listListener = new ListSelectionListener(){
      public void valueChanged(ListSelectionEvent e) {
          sampleText.setFont(getCurrentFont());

      }
    };
    fontList.addListSelectionListener(listListener);
    styleList.addListSelectionListener(listListener);
    sizeList.addListSelectionListener(listListener);
  }

  public int showDialog(Font font,Color color){
    setFont(font);
    setColor(color);
    //System.out.println("showdialog  you");
    return showDialog();
  }
  public int showDialog(Font font,Color color,boolean underline,
               boolean strikethrough,boolean superscript,boolean subscipt){
    setFont(font);
    setColor(color);
    this.setUnderline(underline);
    this.setStrikeThough(strikethrough);
    this.setSuperscript(superscript);
    this.setSubscript(subscipt);
    return showDialog();
  }

  public int showDialog(){
    setVisible(true);
   // System.out.println("show dialog no");
    return OPTION;
  }

  public void setFont(Font font){
    if(font==null){
      font=sampleText.getFont();
    }
    fontList.setSelectedValue(""+font.getFontName(),true);
    fontList.ensureIndexIsVisible(fontList.getSelectedIndex());
    sizeList.setSelectedValue(""+font.getSize(),true);
    sizeList.ensureIndexIsVisible(fontList.getSelectedIndex());
    styleList.setSelectedIndex(font.getStyle());
    styleList.ensureIndexIsVisible(styleList.getSelectedIndex());
    //System.out.println(font.getStyle());
  }
  public void setColor(Color color){
    if(color==null){
      color=sampleText.getForeground();
    }
    selectedColor=color;
    sampleText.setForeground(color);
  }
  public void setUnderline(boolean b){
    if(b){
      StyleConstants.setUnderline(def,true);
      doc.setCharacterAttributes(0,doc.getLength(),def,false);
    }
  }
  public void setStrikeThough(boolean b){
    if(b){
      StyleConstants.setStrikeThrough(def,true);
      doc.setCharacterAttributes(0,doc.getLength(),def,false);
    }
  }
  public void setSuperscript(boolean b){
    if(b){
      StyleConstants.setSuperscript(def,true);
      doc.setCharacterAttributes(0,doc.getLength(),def,false);
    }
  }
  public void setSubscript(boolean b){
    if(b){
      StyleConstants.setSubscript(def,true);
      doc.setCharacterAttributes(0,doc.getLength(),def,false);
    }
  }

  public Font getFont(){
    if(this.OPTION==this.OK_OPTION){
      return getCurrentFont();
    }else{
      return null;
    }
  }
  public Color getColor(){
    if(this.OPTION==this.OK_OPTION){
      return this.sampleText.getForeground();
    }else{
      return null;
    }
  }
  public boolean getUndeline(){
    if(this.OPTION==this.OK_OPTION){
      return this.underLine;
    }else{
      return false;
    }
  }
  public boolean getStrikeThrough(){
    if(this.OPTION==this.OK_OPTION){
      return this.strikeThrough;
    }else{
      return false;
    }
  }

  public boolean getSuperscipt(){
      if(this.OPTION==this.OK_OPTION){
        return this.superscript;
      }else{
        return false;
      }
    }
   public boolean getSubscripte(){
     if (this.OPTION == this.OK_OPTION) {
       return this.subscript;
     }
     else {
       return false;
     }
   }


  public Font getCurrentFont(){
     fontFamily=(String)fontList.getSelectedValue();
     fontSize=Integer.parseInt((String)sizeList.getSelectedValue());
     fontStyle=styleList.getSelectedIndex();
     //System.out.println(fontStyle);
    return new Font(fontFamily,fontStyle,fontSize);
  }













  public static void main(String[] args) {
    JFontChooser fontChooser=new JFontChooser(new JFrame());
    fontChooser.setSize(500,600);
    fontChooser.setVisible(true);
  }
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -