
应该可以了,是你定义了MouseHandler定义了一个MouseAdapter的方法而没有用到,把mouserHandler改成mousePressed或者是mouseReleased就可以了下面是代码:
package comboby;
import javaawt;
import javaawtevent;
import javautil;
import javaawtgeom;
import javaxswing;
public class MouseTest {
public static void main(String[] args) {
MouseFrame frame = new MouseFrame();
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
}
class MouseFrame extends JFrame {
public MouseFrame() {
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
MousePanel panel = new MousePanel();
Container contentPane = getContentPane();
contentPaneadd(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class MousePanel extends JPanel {
public MousePanel() {
squares = new ArrayList();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g) {
superpaintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2setPaint(ColorRED);
for (int i = 0; i < squaressize(); i++)
g2draw((Rectangle2D) squaresget(i));
}
public Rectangle2D find(Point2D p) {
for (int i = 0; i < squaressize(); i++) {
Rectangle2D r = (Rectangle2D) squaresget(i);
if (rcontains(p))
return r;
}
return null;
}
public void add(Point2D p) {
double x = pgetX();
double y = pgetY();
current = new Rectangle2DDouble(x - SIDELENGTH / 2,
y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
squaresadd(current);
repaint();
}
public void remove(Rectangle2D s) {
if (s == null)
return;
if (s == current)
current = null;
squaresremove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList squares;
private Rectangle2D current;
private class MouseHandler extends MouseAdapter {
public void mouseReleased(MouseEvent event) {
current = find(eventgetPoint());
if (current == null)
add(eventgetPoint());
}
public void mouseClicked(MouseEvent event) {
current = find(eventgetPoint());
if (current == null && eventgetClickCount() >= 2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event) {
if (find(eventgetPoint()) == null)
setCursor(CursorgetDefaultCursor());
else
setCursor(CursorgetPredefinedCursor(CursorCROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event) {
if (current != null) {
int x = eventgetX();
int y = eventgetY();
currentsetFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}
//#########计算器#####
import javaawtContainer;
import javaawtFlowLayout;
import javaawtGridLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseMotionListener;
import javatextDecimalFormat;
import javaxswing;
public class 计数器_一般 extends JFrame implements ActionListener {
private JButton an[];
private JButton qingkong;
private JTextField k;
private JPanel rq1, rq2;
private String string[] = { "7", "8", "9", "/", "6", "5", "4", "", "3",
"2", "1", "-", "0", "", "=", "+" };
private String s = "", shu1 = "", shu2 = "", shu3 = "";
private static int fo = 0;
private boolean ff = true;
public 计数器_一般() {
Container c = getContentPane();
csetLayout(new FlowLayout());
// csetLayout(new GridLayout(2,1));
rq1 = new JPanel();
k = new JTextField(10);
qingkong = new JButton("C");
rq1add(k);
rq1add(qingkong);
rq2 = new JPanel();
rq2setLayout(new GridLayout(4, 4, 3, 3));
an = new JButton[stringlength];
for (int i = 0; i < stringlength; i++) {
an[i] = new JButton(string[i]);
an[i]addActionListener(this);
rq2add(an[i]);
}
cadd(rq1);
cadd(rq2);
qingkongaddActionListener(this);
setSize(190, 200);
setVisible(true);
}
public static void main(String[] args) {
计数器_一般 s = new 计数器_一般();
ssetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (!(egetSource() == an[3] || egetSource() == an[7]
|| egetSource() == an[11] || egetSource() == an[13]
|| egetSource() == an[14] || egetSource() == an[15] || e
getSource() == qingkong)) {
if (egetSource() == an[12]) {
s += egetActionCommand();
char a = scharAt(0);
if (a == '0') {
s = "0";
ksetText(s);
}
} else {
s += egetActionCommand();
ksetText(s);
ff = true;
}
} else {
if (egetSource() == qingkong) {
ksetText("");
fo = 0;
shu1 = "";
s = "";
}
if (egetSource() == an[13]) {
String ss = s;
ss += egetActionCommand();
char a = sscharAt(0);
if (a == '') {
s = "";
} else {
if (ssmatches("[0-9]")) {
s += egetActionCommand();
ksetText(s);
}
}
}
if (ff) {
ff = false;
if (egetSource() == an[3]) {
if (kgetText()equals(""))
;
else {
if (shu1equals("")) {
shu1 = kgetText();
ksetText("/");
s = "";
fo = 3;
} else {
shu2 = kgetText();
jishu(fo, shu1, shu2);
fo = 3;
}
}
}
if (egetSource() == an[7]) {
if (kgetText()equals(""))
;
else {
if (shu1equals("")) {
shu1 = kgetText();
ksetText("");
s = "";
fo = 7;
} else {
shu2 = kgetText();
jishu(fo, shu1, shu2);
fo = 7;
}
}
}
if (egetSource() == an[11]) {
if (kgetText()equals(""))
;
else {
if (shu1equals("")) {
shu1 = kgetText();
ksetText("-");
s = "";
fo = 11;
} else {
shu2 = kgetText();
jishu(fo, shu1, shu2);
fo = 11;
}
}
}
if (egetSource() == an[15]) {
if (kgetText()equals(""))
;
else {
if (shu1equals("")) {
shu1 = kgetText();
ksetText("+");
s = "";
fo = 15;
} else {
shu2 = kgetText();
jishu(fo, shu1, shu2);
fo = 15;
}
}
}
if (egetSource() == an[14]) {
if (kgetText()equals("") || shu1equals(""))
;
else {
shu2 = kgetText();
jishu(fo, shu1, shu2);
}
}
}
}
}
public void jishu(int fj, String s1, String s2) {
DecimalFormat twoDigits = new DecimalFormat("000");
double a, b;
a = DoubleparseDouble(s1);
b = DoubleparseDouble(s2);
switch (fj) {
case 3:
shu3 = twoDigitsformat((double) (a / b));
ksetText(shu3);
break;
case 7:
shu3 = twoDigitsformat((double) (a b));
ksetText(shu3);
break;
case 11:
shu3 = twoDigitsformat((double) (a - b));
ksetText(shu3);
break;
case 15:
shu3 = twoDigitsformat((double) (a + b));
ksetText(shu3);
break;
}
s = "";
shu1 = shu3;
shu2 = "";
}
}
//############显示当前时间###############
import javaawtContainer;
import javautilDate;
import javaxswingJFrame;
import javaxswingJLabel;
public class Test extends JFrame implements Runnable {
JLabel jl;
Date now;
public Test() {
Container c = getContentPane();
jl = new JLabel();
cadd(jl);
setSize(280, 80);
setVisible(true);
}
public static void main(String[] args) {
Test s = new Test();
Thread t1=new Thread(s);
t1start();
ssetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public void run() {
while (true) {
now = new Date();
jlsetText("当前时间是:"+now);
try {
Threadsleep(1000); //1秒动一次
} catch (Exception e) {
eprintStackTrace();
}
}
}
}
已测试:
class Stu{
private String name;
private double english;
private double math;
private double chinese;
public Stu(){}
public Stu(String name){
thisname=name;
}
public void setGrades(double english,double math,double chinese){
thisenglish=english;
thismath=math;
thischinese=chinese;
}
public double getTotalGrades(){
return english+math+chinese;
}
public String toString()
{
return thisname + "\t" + thisenglish + "\t" + thismath + "\t" + thischinese+"\t" + thisgetTotalGrades();
}
}
public class Test
{
public static void main(String[] args)
{
double[] grades={
70,80,90,
71,72,73,
72,73,74,
73,74,75,
76,77,78,
61,62,63,
62,63,64,
63,64,65,
64,65,76,
90,90,90
};
Stu[] stu=new Stu[10];
int i=0;
for(i=0;i<10;i++){
stu[i]=new Stu("stu"+i+1);
}
for(i=0;i<30;i+=3)
stu[i/3]setGrades(grades[i],grades[i+1],grades[i+2]);
Test t=new Test();
tsort(stu);
Systemoutprintln("名次\t姓名\t语文\t数学\t英语\t总分");
for(i=0;i<10;i++)
{
Systemoutprint(i+1 + "\t");
Systemoutprintln(stu[i]);
}
}
public void sort(Stu[] stu){
int least=0;
for(int i=0;i<stulength-1;i++){
least=i;
for(int j=i+1;j<stulength;j++){
if(stu[least]getTotalGrades()<stu[j]getTotalGrades())
least=j;
}
if(i!=least) swap(stu,i,least);//交换
}
}
//两个数交换
private void swap(Stu[] stu,int one,int two){
Stu temp=stu[one];
stu[one]=stu[two];
stu[two]=temp;
}
}
以上就是关于java简单程序全部的内容,包括:java简单程序、求助:极其简单的java程序、求写简单JAVA程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)