新人求助~·一个关于JAVA图形界面小程序中的BUG~·~·本人在线等着~·
新人求助~本人第一次发帖·一个关于JAVA图形界面小程序中的BUG~·~·调了好久都未解决~·求好心人帮助提醒~·本人在线等着~·
问题描述:这个程序中为什么在面板上会画两次点和线呢~·paint()方法被调用了两次?前几天调了好久都没答案~·求各位能帮我解决~~·
以下是我的小程序代码:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.event.MouseListener;
public class JieMian5 extends JFrame implements ActionListener {
JPanel jp1, jp3, jp4, jp5;
public static JLabel jlb;
public static JTextArea jta;
JScrollPane jsp = null;
JButton jb1, jb2, jb3;
JSplitPane jsp1;
public static void main (String[] args){
JieMian5 jiemian5 = new JieMian5();
}
public JieMian5() {
int w = Toolkit.getDefaultToolkit().getScreenSize().width;
int h = Toolkit.getDefaultToolkit().getScreenSize().height;
jta = new JTextArea();
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setBackground(Color.YELLOW);
jsp = new JScrollPane(jta);
jp1 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jp5 = new JPanel(new BorderLayout());
//jp5.setb
jb1 = new JButton("原理概述");
jb1.addActionListener(this);
jb2 = new JButton("清空");
jb2.addActionListener(this);
jb3 = new JButton("图文演示");
jb3.addActionListener(this);
// 做一个拆分窗口,分别存取p2,p3.
jsp1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jp1, jsp);
// 指定左边的面板占多大
jsp1.setDividerLocation(w - 780);
jsp1.setDividerSize(0);
jlb = new JLabel(new ImageIcon("image/081020091722_0.jpg"));
jp5.add(jlb);
jp3.add(jb1);
jp3.add(jb2);
jp3.add(jb3);
jp1.setLayout(new BorderLayout());
jp1.add(jp5, BorderLayout.CENTER);
jp1.add(jp3, BorderLayout.SOUTH);
// 从JFrame中取得Container
//Container ct = this.getContentPane();
// 把Panel加入JFrame
this.add(jsp1);
// ct.add(jp4,BorderLayout.SOUTH);
System.out.print("11111");
this.setSize(w, 486);
this.setTitle("山大老校校园导航系统");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void paint(Graphics g) {
Graphic G = new Graphic();
super.paint(g);
g.setColor(Color.BLUE);
for (int ii = 0; ii < 13; ii++) {
g.fillOval(G.vexs[ii].getX(), G.vexs[ii].getY(), 10, 10);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
g.drawLine(G.vexs[0].getX(), G.vexs[0].getY(), G.vexs[12].getX(),
G.vexs[12].getY());try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawLine(G.vexs[0].getX(), G.vexs[0].getY(), G.vexs[11].getX(),
G.vexs[11].getY());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawLine(G.vexs[1].getX(), G.vexs[1].getY(), G.vexs[12].getX(),
G.vexs[12].getY());
g.drawLine(G.vexs[1].getX(), G.vexs[1].getY(), G.vexs[10].getX(),
G.vexs[10].getY());
g.drawLine(G.vexs[2].getX(), G.vexs[2].getY(), G.vexs[3].getX(),
G.vexs[3].getY());
g.drawLine(G.vexs[2].getX(), G.vexs[2].getY(), G.vexs[8].getX(),
G.vexs[8].getY());
g.drawLine(G.vexs[2].getX(), G.vexs[2].getY(), G.vexs[9].getX(),
G.vexs[9].getY());
g.drawLine(G.vexs[2].getX(), G.vexs[2].getY(), G.vexs[10].getX(),
G.vexs[10].getY());
g.drawLine(G.vexs[3].getX(), G.vexs[3].getY(), G.vexs[4].getX(),
G.vexs[4].getY());
g.drawLine(G.vexs[3].getX(), G.vexs[3].getY(), G.vexs[5].getX(),
G.vexs[5].getY());
g.drawLine(G.vexs[3].getX(), G.vexs[3].getY(), G.vexs[11].getX(),
G.vexs[11].getY());
g.drawLine(G.vexs[3].getX(), G.vexs[3].getY(), G.vexs[12].getX(),
G.vexs[12].getY());
g.drawLine(G.vexs[4].getX(), G.vexs[4].getY(), G.vexs[5].getX(),
G.vexs[5].getY());
g.drawLine(G.vexs[4].getX(), G.vexs[4].getY(), G.vexs[11].getX(),
G.vexs[11].getY());
g.drawLine(G.vexs[5].getX(), G.vexs[5].getY(), G.vexs[6].getX(),
G.vexs[6].getY());
g.drawLine(G.vexs[5].getX(), G.vexs[5].getY(), G.vexs[7].getX(),
G.vexs[7].getY());
g.drawLine(G.vexs[6].getX(), G.vexs[6].getY(), G.vexs[7].getX(),
G.vexs[7].getY());
g.drawLine(G.vexs[6].getX(), G.vexs[6].getY(), G.vexs[9].getX(),
G.vexs[9].getY());
g.drawLine(G.vexs[7].getX(), G.vexs[7].getY(), G.vexs[9].getX(),
G.vexs[9].getY());
g.drawLine(G.vexs[8].getX(), G.vexs[8].getY(), G.vexs[9].getX(),
G.vexs[9].getY());
g.drawLine(G.vexs[8].getX(), G.vexs[8].getY(), G.vexs[10].getX(),
G.vexs[10].getY());
g.drawLine(G.vexs[10].getX(), G.vexs[10].getY(), G.vexs[12].getX(),
G.vexs[12].getY());
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getSource() == jb1) {
jta.setText(" 首先,我们需要一个二维数组cost[][]用于存储图形,然后建立一维数组:data[];path[];sata[]分别用于存放从起点到"
+ "点i的最短路径值,最短路径上点i的上一接点顶点号和表示第i个接点是否已经遍历过,sata[i]=0表示为求出点i到起点的最短路径,若sata[i]=1则表示已经求出了最短路径值。另外还需要一个max用于表示距离无穷大,并将cost[][]和data[]设置为max。"
+ "\n"
+ " 1.选择起点,并将其对应的sata[]设置为1,列出其到相邻各点的路径值,将其与对应的data[]中的值经行比较,"
+ "\n"
+ " "
+ "若小于里面的值,则写入data[]中。"
+ "\n"
+ " 3.并从中选出最短一条的顶点作为下一次遍历的起点。"
+ "\n"
+ " 4.对新找出的点经行遍历,并重复上面的步骤,知道所有的点的sata[]标记为一。"
+ "\n"
+ " 5.在完成上述步骤后,data中的值就是当前起点到其他各点的最短路径值。编写完毕。" + "\n");
} else if (arg0.getSource() == jb2) {
jta.setText(null);
// count=0;
} else if (arg0.getSource() == jb3) {
}
}
}
下面是附带的两个类:(校园网络图类和顶点类)
class Graphic {
int numv;
public static int [][] cost1 ;
public static Vertex[] vexs;
int i,j;
int max = 25000;
public static void main(String args[]){
Graphic graphic = new Graphic ();
}
public Graphic(){
numv=13;
cost1 = new int [numv][numv];
vexs = new Vertex[numv];
for(i=0;i<numv;i++)
vexs[i]=new Vertex ();
vexs[0].setName("洪 楼 教 堂");
vexs[1].setName("体 育 场");
vexs[2].setName("公 教 楼");
vexs[3].setName("哲 学 学 院");
vexs[4].setName("法 学 楼");
vexs[5].setName("图 书 馆");
vexs[6].setName("艺 术 学 院");
vexs[7].setName("物 理 楼");
vexs[8].setName("宿 舍 楼");
vexs[9].setName("公 共理 学 院");
vexs[10].setName("外 国 语 学院");
vexs[11].setName("南 门");
vexs[12].setName("西 门");
vexs[0].setX(168);
vexs[1].setX(150);
vexs[2].setX(330);
vexs[3].setX(287);
vexs[4].setX(294);
vexs[5].setX(345);
vexs[6].setX(437);
vexs[7].setX(503);
vexs[8].setX(400);
vexs[9].setX(432);
vexs[10].setX(280);
vexs[11].setX(258);
vexs[12].setX(135);
vexs[0].setY(325);
vexs[1].setY(155);
vexs[2].setY(216);
vexs[3].setY(257);
vexs[4].setY(353);
vexs[5].setY(295);
vexs[6].setY(346);
vexs[7].setY(298);
vexs[8].setY(133);
vexs[9].setY(242);
vexs[10].setY(171);
vexs[11].setY(375);
vexs[12].setY(236);
for(i=0;i<numv;i++)
for(j=0;j<numv;j++)
{
cost1[i][j]=max;
}
cost1[0][12]=100;
cost1[12][0]=100;
cost1[0][11]=300;
cost1[11][0]=300;
cost1[1][10]=250;
cost1[10][1]=250;
cost1[1][12]=100;
cost1[12][1]=100;
cost1[2][3]=160;
cost1[3][2]=160;
cost1[2][8]=160;
cost1[8][2]=160;
cost1[2][9]=150;
cost1[9][2]=150;
cost1[2][10]=100;
cost1[10][2]=100;
cost1[3][4]=150;
cost1[4][3]=150;
cost1[3][5]=120;
cost1[5][3]=120;
cost1[3][11]=200;
cost1[11][3]=200;
cost1[3][12]=250;
cost1[12][3]=250;
cost1[4][5]=130;
cost1[5][4]=130;
cost1[4][11]=100;
cost1[11][4]=100;
cost1[5][6]=200;
cost1[6][5]=200;
cost1[5][7]=300;
cost1[7][5]=300;
cost1[6][7]=150;
cost1[7][6]=150;
cost1[6][9]=200;
cost1[9][6]=200;
cost1[7][9]=100;
cost1[9][7]=100;
cost1[8][9]=200;
cost1[9][8]=200;
cost1[8][10]=180;
cost1[10][8]=180;
cost1[10][12]=300;
cost1[12][10]=300;
cost1[0][0]=0;
cost1[1][1]=0;
cost1[2][2]=0;
cost1[3][3]=0;
cost1[4][4]=0;
cost1[5][5]=0;
cost1[6][6]=0;
cost1[7][7]=0;
cost1[8][8]=0;
cost1[9][9]=0;
cost1[10][10]=0;
cost1[11][11]=0;
cost1[12][12]=0;
}
}
class Vertex {
String name ;
int num;
int x=0;
int y=0;
public Vertex(){
}
public Vertex(int x,int y){
this.x=x;
this.y=y;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num =num;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
[解决办法]
看到是小师弟,大致看了一下,原因是你重写了JFrame的paint方法
你应该重写的是JPanel的paintComponent方法,把你画点的paint方法的里面代码移过去
如果你把你的setSize的方法去掉,你就会看到你的只是画一遍,但是更本不出来,
原因是你更本没有在JPanel里面画,你在JFrame,所以你不得不通过setSize改变Jframe大小
导致二次刷新。