private JLabel lb = new JLabel(\拖动测试对象\); private Thread th = new Thread(new dothread()); private boolean canmove = false; private Component com = null; private Point pt = new Point(); MyClass() { }
class dothread implements Runnable {
public void run() {
// TODO 自动生成方法存根 while (true) {
if (canmove) {
if (fm.getMousePosition() != null) {
fm.setLayout(null);
fm.setDefaultCloseOperation(3); fm.setBounds(300, 200, 450, 300); fm.addMouseListener(new mouselisten()); lb.setBounds(10, 10, 100, 25); // lb.hide(); th.start();
fm.getContentPane().add(lb); fm.show();
}
}
}
}
}
pt.x = fm.getMousePosition().x; pt.y = fm.getMousePosition().y; lb.setLocation(pt);
class mouselisten implements MouseListener {
public void mouseClicked(MouseEvent arg0) {
}
if (arg0.getButton() == 1) { } else { }
canmove = false; canmove = true;
public void mouseEntered(MouseEvent arg0) { }
public void mouseExited(MouseEvent arg0) { }
public void mousePressed(MouseEvent arg0) {
}
}
new MyClass();
public static void main(String[] args) { }
}
canmove = false;
public void mouseReleased(MouseEvent arg0) {
}
canmove = false;
8.计算器
package Frame;
public class Compute { public static void main(String args[]) { ComputeFrame s=new ComputeFrame(); }
}
////////////////////////////////////////////////////////////// package Frame;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.*;
public class ComputeFrame extends JFrame { private JPanel EditTextPanel,ButtonPanel; private JTextField input; private JButton numbutton[]; private JButton btnDot, btnPosMinus, btnBackspace, btnDivide1, btnEqual, btnClear; private JButton btnAdd, btnSubtract, btnMultiply, btnDivide; private JButton btnX2, btnX3, btnXY, btnSin, btnCos, btnTan, btnSqrt, btnN, btnLeft, btnRight; private int i; ComputeControler comControler; public ComputeFrame() { this.setBounds(300, 300, 400, 300); //this.DISPOSE_ON_CLOSE(this); setVisible(true); setLocation(400, 200); EditTextPanel=new JPanel(); ButtonPanel=new JPanel(); input =new JTextField(20); comControler=new ComputeControler(input); input.setHorizontalAlignment(JTextField.HEIGHT); input.setEditable(false); numbutton=new JButton[10]; for( i=0;i<10;i++) { numbutton[i]=new JButton(\ } btnDot=new JButton(\ btnPosMinus=new JButton(\ btnBackspace=new JButton(\ btnDivide1=new JButton(\
btnEqual=new JButton(\ btnClear=new JButton(\ btnAdd=new JButton(\ btnSubtract=new JButton(\ btnMultiply=new JButton(\ btnDivide=new JButton(\ btnLeft = new JButton(\ btnRight = new JButton(\ btnX2 = new JButton(\ btnX3 = new JButton(\ btnXY = new JButton(\ btnSqrt = new JButton(\ btnSin = new JButton(\ btnCos = new JButton(\ btnTan = new JButton(\ btnN = new JButton(\
EditTextPanel.add(input); //布局
ButtonPanel.setLayout(new GridLayout(6, 5));
ButtonPanel.add( btnLeft ).setForeground(Color.red); ButtonPanel.add( btnRight ).setForeground(Color.red); ButtonPanel.add( btnSin ).setForeground(Color.red); ButtonPanel.add( btnCos ).setForeground(Color.red); ButtonPanel.add( btnTan ).setForeground(Color.red);
ButtonPanel.add( btnX2 ).setForeground(Color.red); ButtonPanel.add( btnX3 ).setForeground(Color.red); ButtonPanel.add( btnXY ).setForeground(Color.red); ButtonPanel.add( btnSqrt ).setForeground(Color.red); ButtonPanel.add( btnN ).setForeground(Color.red);
ButtonPanel.add( numbutton[7] ).setForeground(Color.blue); numbutton[7].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[8] ).setForeground(Color.blue); numbutton[8].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[9] ).setForeground(Color.blue); numbutton[9].setBackground(Color.LIGHT_GRAY); ButtonPanel.add( btnAdd ).setForeground(Color.red); ButtonPanel.add( btnClear ).setForeground(Color.red);