高手们!!帮我完善这个程序 我有一个现成的,传给你好了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我已经给你修改好了,你自己对比一下吧。import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class Test extends JFrame{ private JButton b1,b2; private Draw dr; public Test () { dr =new Draw(); b1 = new JButton("line"); b1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { dr.setDraw(e.getActionCommand()); dr.repaint(); } } ); b2 = new JButton("oval"); b2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { dr.setDraw(e.getActionCommand()); dr.repaint(); } } ); Container c = getContentPane(); c.add(b1,BorderLayout.SOUTH); c.add(b2,BorderLayout.NORTH); c.add(dr,BorderLayout.CENTER); validate(); show(); setSize(200,200); repaint(); } public static void main(String args[]) { Test t =new Test(); t.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } } ); }}class Line { public int x0,y0,x1,y1;}class Draw extends JPanel{ Vector lines; Vector ovals ; private String ss; Line cur,curo; public Draw() { lines = new Vector(); ovals = new Vector(); setPreferredSize(new Dimension(150,100)); setBackground(Color.white); addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent e) { if((cur == null)||(curo==null)) { cur = new Line(); curo = new Line(); } if(ss=="line") { cur.x0 = e.getX(); cur.y0 = e.getY(); } else if(ss=="oval") { curo.x0 = e.getX(); curo.y0 = e.getY(); } } public void mouseReleased(MouseEvent e) { if(ss=="line") { cur.x1 = e.getX(); cur.y1 = e.getY(); lines.add(cur); cur =null; } else if (ss=="oval") { curo.x1 = e.getX(); curo.y1 = e.getY(); ovals.add(curo); curo = null; } } }); addMouseMotionListener( new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ if(ss =="line") { cur.x1 = e.getX(); cur.y1 = e.getY(); } else if(ss=="oval") { curo.x1 = e.getX(); curo.y1 = e.getY(); } repaint(); } }); } public void setDraw(String s) { ss=s; } public void paintComponent(Graphics g) { super.paintComponent(g); for(int i = 0;i<lines.size();i++){ Line tmp = (Line)lines.elementAt(i); g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1); } if(cur != null ) g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1); for(int i = 0;i<ovals.size();i++){ Line tmpo = (Line)ovals.elementAt(i); g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1), Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1)); } if(curo != null ) g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1), Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1)); }} MyEclipse 项目错误 {1} 关于静态方法与单例模式 树也有DFS,BFS么? 有没Java查询IP的工具? Document是什么类?为什么在Sun帮助文件里找不到? 连接sql2008失败。求助 如何用Java取得汉字内码? [help]Java如何获取本地系统的缺省编码方式? 打开/保存对话框问题? 在JBuilder中 VM parameters栏该怎么填 初学者问题?简单问题 java应用程序的中文乱码问题。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame{
private JButton b1,b2;
private Draw dr;
public Test ()
{
dr =new Draw();
b1 = new JButton("line");
b1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
b2 = new JButton("oval");
b2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dr.setDraw(e.getActionCommand());
dr.repaint();
}
}
);
Container c = getContentPane();
c.add(b1,BorderLayout.SOUTH);
c.add(b2,BorderLayout.NORTH);
c.add(dr,BorderLayout.CENTER);
validate();
show();
setSize(200,200);
repaint();
}
public static void main(String args[])
{
Test t =new Test();
t.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}class Line {
public int x0,y0,x1,y1;
}class Draw extends JPanel{
Vector lines;
Vector ovals ;
private String ss;
Line cur,curo; public Draw()
{ lines = new Vector();
ovals = new Vector();
setPreferredSize(new Dimension(150,100));
setBackground(Color.white);
addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if((cur == null)||(curo==null))
{
cur = new Line();
curo = new Line();
}
if(ss=="line")
{
cur.x0 = e.getX();
cur.y0 = e.getY();
}
else if(ss=="oval")
{
curo.x0 = e.getX();
curo.y0 = e.getY();
}
}
public void mouseReleased(MouseEvent e) { if(ss=="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
lines.add(cur);
cur =null;
}
else if (ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
ovals.add(curo);
curo = null;
} }
}); addMouseMotionListener( new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(ss =="line")
{
cur.x1 = e.getX();
cur.y1 = e.getY();
}
else if(ss=="oval")
{
curo.x1 = e.getX();
curo.y1 = e.getY();
}
repaint();
}
});
}
public void setDraw(String s)
{
ss=s;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); for(int i = 0;i<lines.size();i++){
Line tmp = (Line)lines.elementAt(i);
g.drawLine(tmp.x0, tmp.y0, tmp.x1, tmp.y1);
}
if(cur != null )
g.drawLine(cur.x0,cur.y0,cur.x1,cur.y1);
for(int i = 0;i<ovals.size();i++){
Line tmpo = (Line)ovals.elementAt(i);
g.drawOval(Math.min(tmpo.x0,tmpo.x1),Math.min(tmpo.y0,tmpo.y1),
Math.abs(tmpo.x0-tmpo.x1),Math.abs(tmpo.y0-tmpo.y1));
}
if(curo != null )
g.drawOval(Math.min(curo.x0,curo.x1),Math.min(curo.y0,curo.y1),
Math.abs(curo.x0-curo.x1),Math.abs(curo.y0-curo.y1));
}
}