posts - 38,  comments - 22,  trackbacks - 0

1、界面乱码的问题解决,
在主窗体new 之前
Font font = new Font("宋体", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}

文件读写乱码,使用下面两个类进行读写
InputStreamReader(InputStream in, String charsetName)
OutputStreamWriter(OutputStream out, String charsetName)
注意charsetName就是gb2312之类的了
2、方法的调用者
 public static void getCaller(){  
     int i;  
     StackTraceElement stack[] = (new Throwable()).getStackTrace();  
     for (i=0; i < stack.length; i++) {  
       StackTraceElement ste=stack[i];  
         System.out.println(ste.getClassName()+"."+ste.getMethodName()+"(...)");  
       System.out.println(i+"--"+ste.getMethodName());  
       System.out.println(i+"--"+ste.getFileName());  
       System.out.println(i+"--"+ste.getLineNumber());  
     }  
   }

3、键盘事件
 import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class T {
 public static void main(String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
  }

  final JTextField tf = new JTextField();
  tf.setEnabled(false);

  JPanel p = new JPanel(new GridLayout(0, 3, 5, 5));
  for (int i = 0; i <= 9; i++) {
   final JButton btn = new JButton(String.valueOf(i));
   p.add(btn);

   btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
     KeyStroke.getKeyStroke((char) (i + '0')), "PressKeyAction");
   btn.getActionMap().put("PressKeyAction", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
     btn.doClick();
    }
   });

   btn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     tf.setText(tf.getText() + btn.getText());
    }
   });
  }

  JFrame f = new JFrame();
  JComponent contentPane = (JComponent) f.getContentPane();
  contentPane.setLayout(new BorderLayout(0, 5));
  contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

  f.getContentPane().add(p, BorderLayout.CENTER);
  f.getContentPane().add(tf, BorderLayout.NORTH);
  f.pack();
  f.setLocationRelativeTo(null);
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setVisible(true);
 }
}

4、浮点数运算
 import java.math.BigDecimal;


/**
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
* 确的浮点数运算,包括加减乘除和四舍五入。
*/


public class Arith {
 //默认除法运算精度
 private static final int DEF_DIV_SCALE = 10;
 //这个类不能实例化
 private Arith() {
 }
 /**
  * 提供精确的加法运算。
  * @param v1 被加数
  * @param v2 加数
  * @return 两个参数的和
  */
 public static float add(float v1, float v2) {
  BigDecimal b1 = new BigDecimal(Float.toString(v1));
  BigDecimal b2 = new BigDecimal(Float.toString(v2));
  return b1.add(b2).floatValue();
 }


 /**
  * 提供精确的减法运算。
  * @param v1 被减数
  * @param v2 减数
  * @return 两个参数的差
  */
 public static float sub(float v1, float v2) {
  BigDecimal b1 = new BigDecimal(Float.toString(v1));
  BigDecimal b2 = new BigDecimal(Float.toString(v2));
  return b1.subtract(b2).floatValue();
 }
 /**
  * 提供精确的乘法运算。
  * @param v1 被乘数
  * @param v2 乘数
  * @return 两个参数的积
  */
 public static float mul(float v1, float v2) {
  BigDecimal b1 = new BigDecimal(Float.toString(v1));
  BigDecimal b2 = new BigDecimal(Float.toString(v2));
  return b1.multiply(b2).floatValue();
 }
 /**
  * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
  * 小数点以后10位,以后的数字四舍五入。
  * @param v1 被除数
  * @param v2 除数
  * @return 两个参数的商
  */
 public static float div(float v1, float v2) {
  return div(v1, v2, DEF_DIV_SCALE);
 }
 /**
  * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
  * 定精度,以后的数字四舍五入。
  * @param v1 被除数
  * @param v2 除数
  * @param scale 表示表示需要精确到小数点以后几位。
  * @return 两个参数的商
  */
 public static float div(float v1, float v2, int scale) {
  if (scale < 0) {
   throw new IllegalArgumentException("The scale must be a positive integer or zero");
  }
  BigDecimal b1 = new BigDecimal(Float.toString(v1));
  BigDecimal b2 = new BigDecimal(Float.toString(v2));
  return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).floatValue();
 }
 /**
  * 提供精确的小数位四舍五入处理。
  * @param v 需要四舍五入的数字
  * @param scale 小数点后保留几位
  * @return 四舍五入后的结果
  */
 public static float round(float v, int scale) {
  if (scale < 0) {
   throw new IllegalArgumentException("The scale must be a positive integer or zero");
  }
  BigDecimal b = new BigDecimal(Float.toString(v));
  BigDecimal one = new BigDecimal("1");
  return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).floatValue();
 }
};

5、applet获得焦点
  this.addComponentListener(new ComponentListener(){

   public void componentHidden(ComponentEvent e) {
    // TODO 自动生成方法存根
    
   }

   public void componentMoved(ComponentEvent e) {
    // TODO 自动生成方法存根
    
   }

   public void componentResized(ComponentEvent e) {
    // TODO 自动生成方法存根
    
   }

   public void componentShown(ComponentEvent e) {
    // TODO 自动生成方法存根
    startButton.requestFocusInWindow();
   }
   
  });
 JFrame 获得焦点
 this.addWindowListener(new WindowAdapter()
{
        public void windowActivated(WindowEvent e)
        {
                jinput.requestFocus();
        }
});

6、设定JSpinner格式
((JSpinner.DefaultEditor) j.getEditor()).getTextField()
    .setFormatterFactory(
      new DefaultFormatterFactory(new NumberFormatter(
        new DecimalFormat("#,#"))));

7、字体
FontMetrics fm = label.getFontMetrics(font); // font为你所使用的字体
int strWidth = fm.stringWidth("字符串"); // 这样可以得到字符串的长度(像素),字符串的高度一般是字体的大小
int width = 200; // 矩形的长度
int high = 50; // 矩形的高
drawString("字符串", (width - strWidth) / 2, (high - font.getSize()) / 2 + font.getSize());

8、translate(800,600)
   将系统坐标逻辑移到(800,600)以后再画图像就以这个逻辑原点为参照点,用完以后要translate(-800,-600)回去

posted on 2007-01-10 11:39 aaabbb 阅读(327) 评论(0)  编辑  收藏 所属分类: Swing

只有注册用户登录后才能发表评论。


网站导航: