Posted on 2009-03-22 17:30
saobaolu 閱讀(292)
評論(0) 編輯 收藏 所屬分類:
java基礎與算法
/**/
/*
?*?mainf.java
?*
?*?Created?on?2009-3-22,?10:16:38
?
*/
/**?*/
/**
?*
?*?
@author
?ChengLu
?
*/
public
?
class
?mainf?
extends
?javax.swing.JFrame?
{


????
/**?*/
/**
?Creates?new?form?mainf?
*/
????
public
?mainf()?
{
????????initComponents();
????}
????
/**?*/
/**
?This?method?is?called?from?within?the?constructor?to
?????*?initialize?the?form.
?????*?WARNING:?Do?NOT?modify?this?code.?The?content?of?this?method?is
?????*?always?regenerated?by?the?Form?Editor.
?????
*/
????@SuppressWarnings(
"
unchecked
"
)
????
//
?<editor-fold?defaultstate="collapsed"?desc="Generated?Code">??????????????????????????
????
private
?
void
?initComponents()?
{
????????java.awt.GridBagConstraints?gridBagConstraints;

????????jLabel1?
=
?
new
?javax.swing.JLabel();
????????jLabel2?
=
?
new
?javax.swing.JLabel();
????????jButton2?
=
?
new
?javax.swing.JButton();
????????jButton1?
=
?
new
?javax.swing.JButton();

????????setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
????????getContentPane().setLayout(
new
?java.awt.GridBagLayout());

????????jLabel1.setText(
"
我做的好不好?
"
);
????????gridBagConstraints?
=
?
new
?java.awt.GridBagConstraints();
????????gridBagConstraints.anchor?
=
?java.awt.GridBagConstraints.NORTH;
????????getContentPane().add(jLabel1,?gridBagConstraints);

????????jLabel2.setText(
"
Email:?chenglu@yeah.net????Web:http://www.mobai.org
"
);
????????gridBagConstraints?
=
?
new
?java.awt.GridBagConstraints();
????????gridBagConstraints.gridx?
=
?
0
;
????????gridBagConstraints.gridy?
=
?
3
;
????????gridBagConstraints.gridheight?
=
?
8
;
????????getContentPane().add(jLabel2,?gridBagConstraints);

????????jButton2.setText(
"
很好
"
);

????????jButton2.addMouseListener(
new
?java.awt.event.MouseAdapter()?
{

????????????
public
?
void
?mouseClicked(java.awt.event.MouseEvent?evt)?
{
????????????????jButton2MouseClicked(evt);
????????????}
????????}
);

????????jButton2.addActionListener(
new
?java.awt.event.ActionListener()?
{

????????????
public
?
void
?actionPerformed(java.awt.event.ActionEvent?evt)?
{
????????????????jButton2ActionPerformed(evt);
????????????}
????????}
);
????????gridBagConstraints?
=
?
new
?java.awt.GridBagConstraints();
????????gridBagConstraints.gridx?
=
?
0
;
????????gridBagConstraints.gridy?
=
?
1
;
????????getContentPane().add(jButton2,?gridBagConstraints);

????????jButton1.setText(
"
不好
"
);

????????jButton1.addMouseListener(
new
?java.awt.event.MouseAdapter()?
{

????????????
public
?
void
?mouseEntered(java.awt.event.MouseEvent?evt)?
{
????????????????jButton1MouseEntered(evt);
????????????}
????????}
);

????????jButton1.addActionListener(
new
?java.awt.event.ActionListener()?
{

????????????
public
?
void
?actionPerformed(java.awt.event.ActionEvent?evt)?
{
????????????????jButton1ActionPerformed(evt);
????????????}
????????}
);
????????gridBagConstraints?
=
?
new
?java.awt.GridBagConstraints();
????????gridBagConstraints.gridx?
=
?
0
;
????????gridBagConstraints.gridy?
=
?
2
;
????????getContentPane().add(jButton1,?gridBagConstraints);

????????pack();
????}
//
?</editor-fold>????????????????????????
????
private
?
void
?jButton1ActionPerformed(java.awt.event.ActionEvent?evt)?
{?????????????????????????????????????????
????????
//
?TODO?add?your?handling?code?here:
????}
????????????????????????????????????????


????
private
?
void
?jButton1MouseEntered(java.awt.event.MouseEvent?evt)?
{??????????????????????????????????????
????????
//
?TODO?add?your?handling?code?here:
????????jButton1.setLocation(locatx(),locaty());
????}
?????????????????????????????????????


????
private
?
void
?jButton2ActionPerformed(java.awt.event.ActionEvent?evt)?
{?????????????????????????????????????????
????????
//
?TODO?add?your?handling?code?here:
????}
????????????????????????????????????????


????
private
?
void
?jButton2MouseClicked(java.awt.event.MouseEvent?evt)?
{??????????????????????????????????????
????????
//
?TODO?add?your?handling?code?here:
????????jButton2.setText(
"
我就知道你最終會點這個的哈
"
);

????}
?????????????????????????????????????

????
public
?
static
?
int
?locatx()
{
????????
return
?(
int
)(Math.random()
*
666
);
????}
????
public
?
static
?
int
?locaty()
{
????????
return
?(
int
)(Math.random()
*
666
);
????}
????
/**?*/
/**
????*?
@param
?args?the?command?line?arguments
????
*/
????
public
?
static
?
void
?main(String?args[])?
{

????????java.awt.EventQueue.invokeLater(
new
?Runnable()?
{

????????????
public
?
void
?run()?
{
????????????????
new
?mainf().setVisible(
true
);
????????????}
????????}
);
????}
????
//
?Variables?declaration?-?do?not?modify?????????????????????
????
private
?javax.swing.JButton?jButton1;
????
private
?javax.swing.JButton?jButton2;
????
private
?javax.swing.JLabel?jLabel1;
????
private
?javax.swing.JLabel?jLabel2;
????
//
?End?of?variables?declaration???????????????????
}
沒技術含量,由NetBeans直接生成,嘿嘿
思路:
創建一個Jbutton,設置鼠標Entered事件,如果事件發生,那么Jbutton.setLocation(x,y);即更改位置
x,y值是2個隨機數
那么就是Math.random();生成一個隨機數(0-1之間的double數),乘以666強制轉換(int)之后傳入Jbutton.setLocation(x,y);即可。
(Done)?
沒有所謂的命運,只有不同的選擇!