我要 在一个swing程序中,在一个窗口中通过下拉框实现字体形状(宋体,仿宋,楷体 等等),大小的改变,请问怎么做哦?给些建议哦

解决方案 »

  1.   

    class Dialog extends JDialog{
    private String s1="宋体";
    private int s2=Font.PLAIN;
    private int s3=16;
    private JLabel jl1=new JLabel("字体");
    private JLabel jl2=new JLabel("字形");
    private JLabel jl3=new JLabel("字体大小");
    private JButton jb1=new JButton("确定");
    private JButton jb2=new JButton("取消");
    private JTextArea jta=new JTextArea("菜鸟作品,此程序为本人编写的第二个程序,编写之初根本没有想要添加" +
    "如此多的功能,只是抱着玩玩的心态,谁知越写越不满意。目前为止,感觉程序的冗余实在太大。此段文字仅为" +
    "文字示例,文段过长,请勿介意");
    private String[] str1={"宋体","仿宋","微软雅黑","黑体","楷体","新宋体","Arial"};
    private String[] str2={"常规","粗体","斜体","粗体斜体"};
    private int[] str3={8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
    private JComboBox jc1=new JComboBox();
    private JComboBox jc2=new JComboBox();
    private JComboBox jc3=new JComboBox();
    public Dialog(JFrame parent){
    super(parent,"字体",true);
    Container cp=getContentPane();
    JPanel jp1=new JPanel(new GridLayout(2,3));
    Box bh=Box.createHorizontalBox();
    bh.add(Box.createHorizontalStrut(10));
    bh.add(jl1);
    bh.add(Box.createHorizontalStrut(80));
    bh.add(jl2);
    bh.add(Box.createHorizontalStrut(80));
    bh.add(jl3);
    for(int i=0;i<str1.length;i++)
    jc1.addItem(str1[i]);
    for(int i=0;i<str2.length;i++)
    jc2.addItem(str2[i]);
    for(int i=0;i<str3.length;i++)
    jc3.addItem(str3[i]);
    Box bx=Box.createHorizontalBox();
    bx.add(Box.createHorizontalStrut(10));
    bx.add(jc1);
    bx.add(Box.createHorizontalStrut(10));
    bx.add(jc2);
    bx.add(Box.createHorizontalStrut(10));
    bx.add(jc3);
    cp.add(bh,BorderLayout.CENTER);
    Box bv=Box.createHorizontalBox();
    bv.add(Box.createHorizontalStrut(100));
    bv.add(jb1);
    bv.add(Box.createHorizontalStrut(30));
    bv.add(jb2);
    jp1.add(bh);
    jp1.add(bx);
    jta.setFont(new Font("宋体",Font.PLAIN,16));
    jta.setBorder(new TitledBorder("示例"));
    jta.setSize(80, 30);
    jta.setLineWrap(true);
    jta.setEditable(false);
    cp.add(jp1,BorderLayout.NORTH);
    cp.add(jta,BorderLayout.CENTER);
    cp.add(bv,BorderLayout.SOUTH);
    jc1.addActionListener(jcListener1);
    jc2.addActionListener(jcListener1);
    jc3.addActionListener(jcListener1);
    jb1.addActionListener(jcListener2);
    jb2.addActionListener(jcListener2);
    setSize(300,400);
    }
    ActionListener jcListener1=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    string=((JComboBox)e.getSource()).getSelectedItem().toString();
    if(string2!="确定"&&string2!="取消"){
    if(string=="斜体")
    s2=Font.ITALIC;
      else if(string=="粗体")
        s2=Font.BOLD;
      else if(string=="常规")
        s2=Font.PLAIN;
      else if(string=="粗体斜体")
        s2=Font.BOLD+Font.ITALIC;
      else if(string=="宋体"||string=="仿宋"||string=="微软雅黑"
        ||string=="黑体"||string=="楷体"||string=="新宋体"||string=="Arial")
        s1=string;
      else 
        s3=Integer.parseInt(string);
    jta.setFont(new Font(s1,s2,s3));
    }
    }
    };
    ActionListener jcListener2=new ActionListener(){
    public void actionPerformed(ActionEvent e){
    string2=((JButton)e.getSource()).getText();
    if(string2=="确定"){
    jtf.setFont(new Font(s1,s2,s3));
    dispose();
    }
    else if(string2=="取消")
    dispose();
    }
    };
    }
    很久前写的了。。你看看吧,记事本上字体管理上的
      

  2.   

    import java.awt.Color;import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    /**
     * 
     * 字体的方法,存放消息内容,字体设置的属性
     *
     */
    public  class FontAttrib implements Serializable{
      /**
     * 
     */
    private static final long serialVersionUID = 1L; public static final int GENERAL = 0;    public static final int BOLD = 1;    public static final int ITALIC = 2;    public static final int BOLD_ITALIC = 3; 
         SimpleAttributeSet attrSet = null;    String text = null, name = null;    int style = 0, size = 0;    Color color = null, backColor = null; 
          //存放表情
     // List list=new ArrayList(); 
      String strface=null;
      
        public FontAttrib() {
      }
    public String getStrface() {
    return strface;
    } public void setStrface(String strface) {
    this.strface = strface;
    } /**
       * 
       *  使用StyleConstants设置字体属性
       */
      public SimpleAttributeSet getAttrSet() {
      
       attrSet = new SimpleAttributeSet();
       if (name != null)
        StyleConstants.setFontFamily(attrSet, name);
       if (style == FontAttrib.GENERAL) {
        StyleConstants.setBold(attrSet, false);
        StyleConstants.setItalic(attrSet, false);
       } else if (style == FontAttrib.BOLD) {
        StyleConstants.setBold(attrSet, true);
        StyleConstants.setItalic(attrSet, false);
       } else if (style == FontAttrib.ITALIC) {
        StyleConstants.setBold(attrSet, false);
        StyleConstants.setItalic(attrSet, true);
       } else if (style == FontAttrib.BOLD_ITALIC) {
        StyleConstants.setBold(attrSet, true);
        StyleConstants.setItalic(attrSet, true);
       }
       StyleConstants.setFontSize(attrSet, size);
       if (color != null)
        StyleConstants.setForeground(attrSet, color);
       if (backColor != null)
        StyleConstants.setBackground(attrSet, backColor);
       return attrSet;
      }
      
      public void setAttrSet(SimpleAttributeSet attrSet) {
       this.attrSet = attrSet;
      }   public String getText() {
       return text;
      }   public void setText(String text) {
       this.text = text;
      }   public Color getColor() {
       return color;
      }   public void setColor(Color color) {
       this.color = color;
      }   public Color getBackColor() {
       return backColor;
      }   public void setBackColor(Color backColor) {
       this.backColor = backColor;
      }   public String getName() {
       return name;
      }   public void setName(String name) {
       this.name = name;
      }   public int getSize() {
       return size;
      }   public void setSize(int size) {
       this.size = size;
      }   public int getStyle() {
       return style;
      }   public void setStyle(int style) {
       this.style = style;
      }
     }//根据需求获取不同的字体属性
     private FontAttrib getFontAttrib() {
     /**
      * 获取组件设置的字体,作为一个对象返回
      */
      FontAttrib att = new FontAttrib();
      att.setText(users.getLoginNameIn()+" : "+jtain.getText());
      att.setName((String) fontName.getSelectedItem());
      att.setSize(Integer.parseInt((String) fontSize.getSelectedItem()));
      System.out.println("------------你设置的字的大小为"+Integer.parseInt((String) fontSize.getSelectedItem()));
      String ts = (String) fontStyle.getSelectedItem();
      if (ts.equals("常规")) {
       att.setStyle(FontAttrib.GENERAL);
      } else if (ts.equals("粗体")) {
       att.setStyle(FontAttrib.BOLD);
      } else if (ts.equals("斜体")) {
       att.setStyle(FontAttrib.ITALIC);
      } else if (ts.equals("粗斜体")) {
       att.setStyle(FontAttrib.BOLD_ITALIC);
      }
      String tc = (String) fontColor.getSelectedItem();
      if (tc.equals("黑色")) {
       att.setColor(new Color(0, 0, 0));
      } else if (tc.equals("红色")) {
       att.setColor(new Color(255, 0, 0));
      } else if (tc.equals("蓝色")) {
       att.setColor(new Color(0, 0, 255));
      } else if (tc.equals("黄色")) {
       att.setColor(new Color(255, 255, 0));
      } else if (tc.equals("绿色")) {
       att.setColor(new Color(0, 255, 0));
      }
      
      String bc = (String) fontBackColor.getSelectedItem();
      if (!bc.equals("无色")) {
       if (bc.equals("灰色")) {
        att.setBackColor(new Color(200, 200, 200));
       } else if (bc.equals("淡红")) {
        att.setBackColor(new Color(255, 200, 200));
       } else if (bc.equals("淡蓝")) {
        att.setBackColor(new Color(200, 200, 255));
       } else if (bc.equals("淡黄")) {
        att.setBackColor(new Color(255, 255, 200));
       } else if (bc.equals("淡绿")) {
        att.setBackColor(new Color(200, 255, 200));
       }
      }
       //保存表情
        List li=NewFrame.list;
        String result="#";
        for(int k=0;k<li.size();k++){
         result=result+li.get(k)+"#";
        }
        if(result!="#"){
        result=result.substring(1,result.length()-1);
        }
        att.setStrface(result);
        System.out.println("~~~~~~~~~~~~~~~~~~~从你的输入框中获得的内容是==="+att.getText());
      return att;
     }
      

  3.   

    顶             
            .-. __ _ .-.
            |  `  / \  |
            /     '.()--\
           |         '._/
          _| O   _   O |_
          =\    '-'    /=
            '-._____.-'
            /`/\___/\`\
           /\/o     o\/\
          (_|         |_)
            |____,____|
            (____|____)
      

  4.   

    监听你的下拉动作,改变Font类属性就可以了!