为什么我这样画的图会闪呢?还有就是左边文本域中的只在运行后消失了

帅帅的大哥 2013-08-17 12:43:59
package haha;
/**数据结构的可视化排序
* @author Administrator
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class MainFrame extends JFrame implements ActionListener{

int H=0;
static int Array[]={145,165,190,350,170,220,120,260,190,245,190,280,160,200,120,180,110,180,320,130};
static int setArray[]=new int[12];
static int clearArray[]={0};
String str=null;
JButton JB0=new JButton("清屏");
JButton JB1=new JButton("145");
JButton JB2=new JButton("165");
JButton JB3=new JButton("190");
JButton JB4=new JButton("350");
JButton JB5=new JButton("170");
JButton JB6=new JButton("220");
JButton JB7=new JButton("120");
JButton JB8=new JButton("260");
JButton JB9=new JButton("190");
JButton JB10=new JButton("245");
JButton JB11=new JButton("190");
JButton JB12=new JButton("280");
JButton JB13=new JButton("160");
JButton JB14=new JButton("200");
JButton JB15=new JButton("120");
JButton JB16=new JButton("180");
JButton JB17=new JButton("110");
JButton JB18=new JButton("180");
JButton JB19=new JButton("320");
JButton JB20=new JButton("130");
JButton JB31=new JButton("初始化");
JButton JB32=new JButton("选择排序");
JButton JB33=new JButton("冒泡排序");
JButton JB34=new JButton("堆排序");
JButton JB35=new JButton("希尔排序");
JButton JB36=new JButton("插入排序");
JButton JB37=new JButton("基排序");
JButton JB38=new JButton("100");
JTextArea textArea = new JTextArea(2,6);
public MainFrame(){
//JFrame JF=new JFrame();
setSize(1200, 700);
JPanel JP0=new JPanel();
JP0.setLocation(320, 73);
JP0.setSize(1000,542);
//JP0.setBackground(Color.orange);
add(JP0);
setBackground(Color.orange);
setLayout(null);
JLabel JL=new JLabel();
JL.setText("请点击选择要排序的数:");
JL.setFont(new Font("SanSerif",Font.PLAIN,24));
JL.setLocation(20,70);
JL.setSize(280,30);
JPanel JP1=new JPanel();
JP1.setForeground(Color.BLUE);
JP1.setLocation(20,100);
JP1.setSize(280, 150);
GridLayout grid =new GridLayout(4,5);
JP1.setLayout(grid);

JB0.addActionListener(this);
JB1.addActionListener(this);
JB2.addActionListener(this);
JB3.addActionListener(this);
JB4.addActionListener(this);
JB5.addActionListener(this);
JB6.addActionListener(this);
JB7.addActionListener(this);
JB8.addActionListener(this);
JB9.addActionListener(this);
JB10.addActionListener(this);
JB11.addActionListener(this);
JB12.addActionListener(this);
JB13.addActionListener(this);
JB14.addActionListener(this);
JB15.addActionListener(this);
JB16.addActionListener(this);
JB17.addActionListener(this);
JB18.addActionListener(this);
JB19.addActionListener(this);
JB20.addActionListener(this);
JB31.addActionListener(this);
JB33.addActionListener(this);


JP1.add(JB1);
JP1.add(JB2);
JP1.add(JB3);
JP1.add(JB4);
JP1.add(JB5);
JP1.add(JB6);
JP1.add(JB7);
JP1.add(JB8);
JP1.add(JB9);
JP1.add(JB10);
JP1.add(JB11);
JP1.add(JB12);
JP1.add(JB13);
JP1.add(JB14);
JP1.add(JB15);
JP1.add(JB16);
JP1.add(JB17);
JP1.add(JB18);
JP1.add(JB19);
JP1.add(JB20);
add(JL);
add(JP1);
JLabel JL2=new JLabel();
JL2.setLocation(20, 265);
JL2.setSize(280, 30);
JL2.setText("得到的排序数:");
JL2.setFont(new Font("SanSerif",Font.PLAIN,24));
add(JL2);
JPanel jp4=new JPanel();
jp4.setLocation(230,260);
jp4.setSize(80,35);
jp4.add(JB0);
add(jp4);
//JP2.setBackground(Color.BLUE);

//JTextArea textArea = new JTextArea();
textArea.setBounds(20,300,280,60);
textArea.setFont(new Font("SanSerif",Font.PLAIN,24));
textArea.setEditable(false);
textArea.setLineWrap(true);
add(textArea);
JLabel JL3=new JLabel();
JL3.setLocation(20,370);
JL3.setSize(280,30);
JL3.setText("请点击选择要排序的方式:");
JL3.setFont(new Font("SanSerif",Font.PLAIN,24));
add(JL3);
JPanel JP3=new JPanel();
JP3.setLocation(20,410);
JP3.setSize(280, 200);

JP3.add(JB31);
JP3.add(JB32);
JP3.add(JB33);
JP3.add(JB34);
JP3.add(JB35);
JP3.add(JB36);
JP3.add(JB37);
JP3.add(JB38);
//排序方式的界面设计
GridLayout grid1 =new GridLayout(4,2);
JP3.setLayout(grid1);
add(JP3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);


}


public void actionPerformed(ActionEvent e) {
JButton bt=(JButton)e.getSource();

if(bt==JB0)
{
textArea.setText("");
H=0;
for(int k=0;k<setArray.length;k++){
setArray[k]=0;
}
}
else if (bt==JB1)
{
textArea.setText(textArea.getText()+String.valueOf(Array[0])+" ");
setArray[H]=Array[0];
H++;

}
else if (bt==JB2)
{
textArea.setText(textArea.getText()+String.valueOf(Array[1])+" ");
setArray[H]=Array[1];
H++;

}

else if (bt==JB3)
{
textArea.setText(textArea.getText()+String.valueOf(Array[2])+" ");
setArray[H]=Array[2];
H++;
}
else if (bt==JB4)
{
textArea.setText(textArea.getText()+String.valueOf(Array[3])+" ");
setArray[H]=Array[3];
H++;
}
else if (bt==JB5)
{
textArea.setText(textArea.getText()+String.valueOf(Array[4])+" ");
setArray[H]=Array[4];
H++;
}
else if (bt==JB6)
{
textArea.setText(textArea.getText()+String.valueOf(Array[5])+" ");
setArray[H]=Array[5];
H++;
}
else if (bt==JB7)
{
textArea.setText(textArea.getText()+String.valueOf(Array[6])+" ");
setArray[H]=Array[6];
H++;
}
else if (bt==JB8)
{
textArea.setText(textArea.getText()+String.valueOf(Array[7])+" ");
setArray[H]=Array[7];
H++;
}
else if (bt==JB9)
{
textArea.setText(textArea.getText()+String.valueOf(Array[8])+" ");
setArray[H]=Array[8];
H++;
}
else if (bt==JB10)
{
textArea.setText(textArea.getText()+String.valueOf(Array[9])+" ");
setArray[H]=Array[9];
H++;
}
else if (bt==JB11)
{
textArea.setText(textArea.getText()+String.valueOf(Array[10])+" ");
setArray[H]=Array[10];
H++;
}
else if (bt==JB12)
{
textArea.setText(textArea.getText()+String.valueOf(Array[11])+" ");
setArray[H]=Array[11];
H++;
}
else if (bt==JB13)
{
textArea.setText(textArea.getText()+String.valueOf(Array[12])+" ");
setArray[H]=Array[12];
H++;
}
else if (bt==JB14)
{
textArea.setText(textArea.getText()+String.valueOf(Array[13])+" ");
setArray[H]=Array[13];
H++;
}
else if (bt==JB15)
{
textArea.setText(textArea.getText()+String.valueOf(Array[14])+" ");
setArray[H]=Array[14];
H++;
}
else if (bt==JB16)
{
textArea.setText(textArea.getText()+String.valueOf(Array[15])+" ");
setArray[H]=Array[15];
H++;
}
else if (bt==JB17)
{
textArea.setText(textArea.getText()+String.valueOf(Array[16])+" ");
setArray[H]=Array[16];
H++;
}
else if (bt==JB18)
{
textArea.setText(textArea.getText()+String.valueOf(Array[17])+" ");
setArray[H]=Array[17];
H++;
}
else if (bt==JB19)
{
textArea.setText(textArea.getText()+String.valueOf(Array[18])+" ");
setArray[H]=Array[18];
H++;
}
else if (bt==JB20)
{
textArea.setText(textArea.getText()+String.valueOf(Array[19])+" ");
setArray[H]=Array[19];
H++;
}
else if (bt==JB31){
this.setArray=getCount();
repaint();
}
else if (bt==JB33)
{
textArea.setText(textArea.getText());
BubSorting bub=new BubSorting();
Thread T=new Thread(bub);
T.start();
}
/*
else if (bt==JB31)
n=31;
else if (bt==JB32)
H=32;
else if (bt==JB33)
H=33;
else if (bt==JB34)
H=34;
else if (bt==JB35)
H=35;
else if (bt==JB36)
H=36;
else if (bt==JB37)
H=37;
else if (bt==JB38)
H=38;
*/
Graphics g=this.getGraphics();
repaint();
}

public int []getCount(){
return setArray;
}

public void p1(int count[])
{
this.setArray=count;
System.out.println(Arrays.toString(setArray));
repaint();
}


public void paint(Graphics g){
super.paintComponents(g);
g.setColor(Color.red);
for(int i=0;i<setArray.length;i++){
g.setColor(Color.blue);
g.drawRect(400+30*i, 500-setArray, 20, setArray[i]);
//System.out.println(Arrays.toString(setArray));
}
}

public static void main(String[]args){
new MainFrame();
//System.out.println(Arrays.toString(Array));
}
}


package haha;


/**
* 冒泡排序算法
*/
public class BubSorting implements Runnable{

public void run(){
MainFrame n1=new MainFrame();
int count[]=n1.getCount();
for(int i=1;i<count.length;i++){
for(int j=0;j<count.length-1;j++){
try{
n1.p1(count);
Thread.sleep(300);
if(count[j]>count[j+1]){
int temp=count[j];
count[j]=count[j+1];
count[j+1]=temp;
}
}
catch(InterruptedException e){}
n1.p1(count);
}
}

}
}
[i]

...全文
89 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧