資源簡介
一個(gè)java swing 實(shí)現(xiàn)的拖拽組件 交換位置的小程序
代碼片段和文件信息
package?com.teraee.tcat.util;
import?java.awt.BorderLayout;
import?java.awt.Component;
import?java.awt.EventQueue;
import?java.awt.Color;
import?java.awt.event.MouseAdapter;
import?java.awt.event.MouseEvent;
import?java.util.HashMap;
import?java.util.Map;
import?javax.swing.BorderFactory;
import?javax.swing.Jframe;
import?javax.swing.JPanel;
public?class?TestDrag?extends?Jframe?{
private?JPanel?contentPane;
//坐標(biāo)數(shù)組
public?static?int[][]?seat?=?{{1010}{15010}{10150}{150150}};
//記錄坐標(biāo)對(duì)應(yīng)的組件
public?static?Map?seatPanel?=?new?HashMap();
public?static?JPanel[]?panels?=?new?JPanel[4]?;
//查找最近的區(qū)域坐標(biāo),返回編號(hào)
public?static?int?getbest(int?x?int?y){
int?indexbest?=?0;
int?modbest?=?0;
int?modtmp?=?0;
int?indextmp?=?0;
System.out.println(“x:“+x?+?“?y:“?+?y);
for(int?i?=?0;?i?4;?i++){
if(i==0){
modbest?=?Math.abs(x-seat[i][0])?+?Math.abs(y-seat[i][1]);
System.out.println(“modbest:“+modbest?);
indexbest?=?0;
}else{
modtmp?=?Math.abs(x-seat[i][0])?+?Math.abs(y-seat[i][1]);
System.out.println(“modtmp:“+modtmp?);
indextmp?=?i;
if(modtmp? modbest?=?modtmp;
indexbest?=?indextmp;
System.out.println(“modbest:“+modbest?+?“?indexbest:“?+?indextmp);
}
}
}
return?indexbest;
}
public?static?void?main(String[]?args)?{
EventQueue.invokeLater(new?Runnable()?{
public?void?run()?{
try?{
TestDrag?frame?=?new?TestDrag();
frame.setVisible(true);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
});
}
public?TestDrag()?{
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
setSize(310?330);
setLocationRelativeTo(null);
contentPane?=?new?JPanel();
contentPane.setLayout(new?BorderLayout());
add(contentPane);
JPanel?panel?=?new?JPanel();
contentPane.add(panel?BorderLayout.CENTER);
panel.setBorder(BorderFactory.createLineBorder(Color.orange?2));
panel.setLayout(null);
panels[0]?=?new?JPanel();
panels[0].setBackground(Color.BLUE);
panels[0].setBounds(10?10?130?130);
panel.add(panels[0]);
seatPanel.put(“0“?“0“);
panels[1]?=?new?JPanel();
panels[1].setBackground(Color.LIGHT_GRAY);
panels[1].setBounds(150?10?130?130);
panel.add(panels[1]);
seatPanel.put(“1“?“1“);
panels[2]?=?new?JPanel();
- 上一篇:mail.jar和activation.jar
- 下一篇:jstl 1.2 jar
評(píng)論
共有 條評(píng)論