平时别人问得一些小例子,做出来放在这,以后不断添加
需要的也可以留言提出。
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、拖动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();
}
}
}