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、浮點數(shù)運算
?import java.math.BigDecimal;
/**
* 由于Java的簡單類型不能夠精確的對浮點數(shù)進行運算,這個工具類提供精
* 確的浮點數(shù)運算,包括加減乘除和四舍五入。
*/
public class Arith {
?//默認除法運算精度
?private static final int DEF_DIV_SCALE = 10;
?//這個類不能實例化
?private Arith() {
?}
?/**
? * 提供精確的加法運算。
? * @param v1 被加數(shù)
? * @param v2 加數(shù)
? * @return 兩個參數(shù)的和
? */
?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 被減數(shù)
? * @param v2 減數(shù)
? * @return 兩個參數(shù)的差
? */
?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 被乘數(shù)
? * @param v2 乘數(shù)
? * @return 兩個參數(shù)的積
? */
?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();
?}
?/**
? * 提供(相對)精確的除法運算,當發(fā)生除不盡的情況時,精確到
? * 小數(shù)點以后10位,以后的數(shù)字四舍五入。
? * @param v1 被除數(shù)
? * @param v2 除數(shù)
? * @return 兩個參數(shù)的商
? */
?public static float div(float v1, float v2) {
? return div(v1, v2, DEF_DIV_SCALE);
?}
?/**
? * 提供(相對)精確的除法運算。當發(fā)生除不盡的情況時,由scale參數(shù)指
? * 定精度,以后的數(shù)字四舍五入。
? * @param v1 被除數(shù)
? * @param v2 除數(shù)
? * @param scale 表示表示需要精確到小數(shù)點以后幾位。
? * @return 兩個參數(shù)的商
? */
?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();
?}
?/**
? * 提供精確的小數(shù)位四舍五入處理。
? * @param v 需要四舍五入的數(shù)字
? * @param scale 小數(shù)點后保留幾位
? * @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)
???將系統(tǒng)坐標邏輯移到(800,600)以后再畫圖像就以這個邏輯原點為參照點,用完以后要translate(-800,-600)回去
posted on 2007-01-10 11:39
aaabbb 閱讀(340)
評論(0) 編輯 收藏 所屬分類:
Swing