Swing


          天行健 君子以自強(qiáng)不息

          posts - 69, comments - 215, trackbacks - 0, articles - 16
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          一些小例子(Ing)

          Posted on 2007-09-14 10:49 zht 閱讀(1809) 評(píng)論(2)  編輯  收藏 所屬分類: Swing
          平時(shí)別人問得一些小例子,做出來放在這,以后不斷添加
          需要的也可以留言提出。

          1、使用Insert來判斷JTextField是插入模式還是修改模式

          public class InsertTextField extends JPanel {

           /**
            * @param args
            */
           public static void main(String[] args) {
            JFrame jf = new JFrame();
            jf.setContentPane(new InsertTextField());
            jf.pack();
            jf.setVisible(true);
           }
           public InsertTextField() {
            this.setLayout(new FlowLayout());
            final JTextField jtf = new JTextField(10);
            final IntegerDocument id = new IntegerDocument(10);
            jtf.addKeyListener(new KeyListener() {
             @Override
             public void keyPressed(KeyEvent e) {
              if (e.getKeyCode() == KeyEvent.VK_INSERT) {
               id.setFlag();
              }
             }
             @Override
             public void keyReleased(KeyEvent e) {
              //    jtf.setCaretPosition(jtf.getText().length());
             }

             @Override
             public void keyTyped(KeyEvent e) {

             }

            });
            jtf.setDocument(id);
            this.add(jtf);
           }

           class IntegerDocument extends PlainDocument {

            private boolean flag = false;

            public IntegerDocument(int n) {
             super();
            }

            public void setFlag() {
             boolean f = flag ? flag = false : (flag = true);
            }

            public void insertString(int offset, String s, AttributeSet ats)
              throws BadLocationException {

             try {
              super.insertString(offset, s, ats);
              if (flag) {
               remove(offset - 1, 1);
              }
             } catch (Exception ex) {
              Toolkit.getDefaultToolkit().beep();
             }
            }
           }
          }
          2、拖動(dòng)JPanel,還有一些小bug,以后有空再改

          /**
           * DragPanel Class
           *
           * author:zhangtao
           * mail:zht_dream@hotmail.com
           * 2007-9-26
           */
          public class DragPanel extends JPanel implements MouseListener,
            MouseMotionListener {

           JPanel north_JP;

           JScrollPane cen_JP;

           /**
            * @param args
            */
           public static void main(String[] args) {
            JFrame jf = new JFrame();
            jf.setContentPane(new DragPanel());
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.pack();
            jf.setVisible(true);
           }

           public DragPanel() {
            this.setLayout(new BorderLayout());
            north_JP = new JPanel();
            north_JP.add(new JButton("1"));
            north_JP.add(new JButton("2"));
            north_JP.add(new JButton("3"));
            north_JP.add(new JButton("4"));
            this.add("North", north_JP);
            cen_JP = new JScrollPane();
            cen_JP.setViewportView(new JTree());
            this.add(cen_JP);
            north_JP.addMouseListener(this);
            north_JP.addMouseMotionListener(this);

           }

           public void mouseClicked(MouseEvent e) {

           }

           public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

           }

           public void mouseExited(MouseEvent e) {
            System.out.println("exit");
            this.setCursor(Cursor.getDefaultCursor());
           }

           public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

           }

           public void mouseReleased(MouseEvent e) {
            this.setCursor(Cursor.getDefaultCursor());
           }

           boolean dragFlag = false;

           public void mouseDragged(MouseEvent e) {
            dragFlag = true;
            if (flag) {
             this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
             Point po = e.getPoint();
             Dimension di = north_JP.getPreferredSize();
             north_JP.setPreferredSize(new Dimension(di.width,
               (north_JP.getY() + po.y)));
             north_JP.revalidate();
             north_JP.repaint();
            }
           }

           boolean flag = false;

           public void mouseMoved(MouseEvent e) {
            Point point = e.getPoint();
            this.setCursor(Cursor.getDefaultCursor());
            flag = false;
            dragFlag = false;
            if (((north_JP.getY() + north_JP.getHeight() - point.y) < 5)
              && ((north_JP.getY() + north_JP.getHeight() - point.y) > -5)) {
             this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
             flag = true;
            } else {
             this.setCursor(Cursor.getDefaultCursor());
            }
           }

          }

          3、載入修改properties的例子

          package proper;

          import java.awt.BorderLayout;
          import java.awt.FlowLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.Iterator;
          import java.util.Map;
          import java.util.Properties;
          import java.util.Vector;
          import java.util.Map.Entry;

          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JPanel;
          import javax.swing.JScrollPane;
          import javax.swing.JTable;
          import javax.swing.table.DefaultTableModel;

          /**
           *
           * Test Class
           *
           * author:zht
           * mail:zht_dream@hotmail.com
           * 2007-10-31
           */
          public class Test extends JPanel implements ActionListener {

           /**
            * @param args
            */
           public static void main(String[] args) {
            // TODO Auto-generated method stub
            Test test = new Test();
            JFrame jf = new JFrame();
            jf.setContentPane(test);
            jf.pack();
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
           }

           JTable jt;

           JButton modify_JBn;

           Properties props = null;

           public Test() {
            try {
             props = loadPro();
            } catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
            this.setLayout(new BorderLayout());
            String[] tn = { "名稱", " 值", };
            String[][] data = {};
            DefaultTableModel defaultModel = new DefaultTableModel(data, tn) {
             @Override
             public boolean isCellEditable(int row, int column) {
              if (column == 0) {
               return false;
              } else
               return true;
             }
            };
            jt = new JTable(defaultModel);
            this.add(new JScrollPane(jt));
            {
             Iterator itor = props.entrySet().iterator();
             while (itor.hasNext()) {
              Map.Entry vEntry = (Entry) itor.next();
              Vector<Object> vv = new Vector<Object>();
              vv.add(vEntry.getKey());
              vv.add(vEntry.getValue());
              //做一些其他的事……
              System.out.println(vEntry.getKey());
              defaultModel.addRow(vv);
             }
            }
            modify_JBn = new JButton("modify");
            modify_JBn.addActionListener(this);
            JPanel jp = new JPanel();
            jp.setLayout(new FlowLayout());
            jp.add(modify_JBn);
            this.add("South", jp);
           }

           public Properties loadPro() throws FileNotFoundException, IOException {
            Properties props = new Properties();
            props.load(new FileInputStream(new File("config/config.properties")));
            return props;
           }

           @Override
           public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < jt.getRowCount(); i++) {
             String name = jt.getValueAt(i, 0).toString();
             String value = jt.getValueAt(i, 1).toString();
             props.setProperty(name, value);
             System.out.println("==");
            }
            FileOutputStream fos;
            try {
             fos = new FileOutputStream(new File("config/config.properties"));
             props.store(fos, "");
            } catch (Exception e1) {
             // TODO Auto-generated catch block
             e1.printStackTrace();
            }
           }
          }

          主站蜘蛛池模板: 济源市| 漾濞| 张北县| 永善县| 布拖县| 十堰市| 永德县| 隆化县| 嘉定区| 安岳县| 石嘴山市| 金溪县| 龙口市| 辰溪县| 华容县| 二连浩特市| 迭部县| 聂拉木县| 秦皇岛市| 日喀则市| 徐闻县| 通州市| 峨山| 都昌县| 乐东| 驻马店市| 曲麻莱县| 社旗县| 巴楚县| 蒙阴县| 合作市| 黄浦区| 名山县| 建水县| 武乡县| 清河县| 漯河市| 潍坊市| 岱山县| 青冈县| 临清市|