Java 按钮点击和延迟问题

Java 按钮点击和延迟问题,java,swing,sorting,animation,Java,Swing,Sorting,Animation,我有一个gui类,其中有四个按钮,当单击按钮时,程序应该运行排序算法的动画。问题是,当我点击一个按钮时,它会弹出一个Jframe,但Jframe上什么也没有显示,它看起来是黑色的。很可能是因为我在气泡排序动画类中使用的延迟。Jframe的屏幕变黑,动画完成后,最终显示最终结果。那么,有人知道如何解决这个问题吗 //This is the main GUI class import java.awt.*; import java.awt.event.*; imp

我有一个gui类,其中有四个按钮,当单击按钮时,程序应该运行排序算法的动画。问题是,当我点击一个按钮时,它会弹出一个Jframe,但Jframe上什么也没有显示,它看起来是黑色的。很可能是因为我在气泡排序动画类中使用的延迟。Jframe的屏幕变黑,动画完成后,最终显示最终结果。那么,有人知道如何解决这个问题吗

    //This is the main GUI class

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class GUIMain {

        Bubble gui=new Bubble();
        Selection gui2=new Selection();

       private JFrame mainFrame;
       private JLabel headerLabel;
       private JLabel statusLabel;
       private JPanel controlPanel;

       public GUIMain(){
          prepareGUI();
       }

       public static void main(String[] args){
          GUIMain swingControlDemo = new GUIMain();  
          swingControlDemo.showEventDemo();       
       }

       private void prepareGUI(){
          mainFrame = new JFrame("Sorting Animations");
          mainFrame.setSize(400,400);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          //labels are for texts
          headerLabel = new JLabel("",JLabel.CENTER );
          statusLabel = new JLabel("",JLabel.CENTER);        

          statusLabel.setSize(350,100);
          mainFrame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
             }        
          });
          //panel contains both the buttons and the text
          controlPanel = new JPanel();
          controlPanel.setLayout(new FlowLayout());

          mainFrame.add(headerLabel);
          mainFrame.add(controlPanel);
          mainFrame.add(statusLabel);
          mainFrame.setVisible(true);  
       }

       private void showEventDemo(){
          headerLabel.setText("Select any of the sorting algorithm!"); 

          JButton Insertion = new JButton("Insertion");
          JButton Selection = new JButton("Selection");
          JButton Bubble = new JButton("Bubble");
          JButton Shell = new JButton("Shell");
         // JButton Quick= new JButton ("Quick");

          Insertion.setActionCommand("Insertion");
          Selection.setActionCommand("Selection");
          Bubble.setActionCommand("Bubble");
          Shell.setActionCommand("Shell");
         // Quick.setActionCommand("Quick");

          Insertion.addActionListener(new ButtonClickListener()); 
          Selection.addActionListener(new ButtonClickListener()); 
          Bubble.addActionListener(new ButtonClickListener());
          Shell.addActionListener(new ButtonClickListener());
         // Quick.addActionListener(new ButtonClickListener());

          controlPanel.add(Insertion);
          controlPanel.add(Selection);
          controlPanel.add(Bubble);  
          controlPanel.add(Shell);
         // controlPanel.add(Quick);

          mainFrame.setVisible(true);  
       }

       private class ButtonClickListener implements ActionListener{
          public void actionPerformed(ActionEvent e) {
             String command = e.getActionCommand();  
             if( command.equals( "Insertion" ))  {
                statusLabel.setText("Insertion Button clicked.");

             }
             else if( command.equals( "Selection" ) )  {
                 try {
                    gui2.sort();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                statusLabel.setText("Selection Button clicked."); 
             }
             else if(command.equals("Bubble"))
             {
                statusLabel.setText("Bubble Button clicked.");
                try {
                    gui.sort();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
             }      
             else if(command.equals("Shell")){
                 statusLabel.setText("Shell Button clicked");
             }
             else
             {
                 statusLabel.setText("Quick Button clicked");
             }
          }     
       }
    }`






//and this is bubble sort class







import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import java.awt.*;

public class Bubble{

public static void main(String[] args) throws InterruptedException{
        Bubble b = new Bubble();
        b.sort();
    }

//////////////////////////////////
////SORT STARTS HER
/////////////////////////////////




public void sort() throws InterruptedException {
        JFrame frame = new JFrame("Bubble Sort");   //Naming the javaframe
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int k =0;                                       //k=0
        frame.setLayout(null);                          //making layout null
        //  Border thickBorder = new LineBorder(Color.BLACK, 12);
          Border thin = new LineBorder(Color.RED, 12);
        JButton[][] buttons= new JButton [6][6];        //defining array for buttons and labels


        for(int i = 0 ; i<6; i++){
            buttons[i][0] = new JButton("7");
            buttons[i][1] = new JButton("4");
            buttons[i][2] = new JButton("3");
            buttons[i][3] =new JButton ("2");
            buttons[i][4] =new JButton("0");
            buttons[i][5] =new JButton("1");

        }   //for ends  
        for(int i = 0 ; i<6; i++){
            buttons[i][0].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][1].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][2].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][3].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][4].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][5].setFont(new Font("Verdana",Font.BOLD,22));
        }

        int c,d,e,f;
        frame.setVisible(true);


        frame.setVisible(true);
        String temp;

        for (int i=0; i<buttons.length-1; i++)


        {   



            //main For Loop starts
            k++;
            if(k>=6){
                break;
            }
            for (int j=0; j<buttons[k].length-i-1; j++)     //sorting the buttons buttons[k].length=6
            {

                if (Integer.parseInt(buttons[k][j].getText())>Integer.parseInt(buttons[k][j+1].getText()))
                {
                    temp = buttons[k][j].getText();
                    buttons[k][j].setText(buttons[k][j+1].getText());   
                    buttons[k][j+1].setText(temp);
                    buttons[k][j+1].setBorder(thin);
                }// main if ends
                c=d=6;
                e=45;
                f=57;
                for(int l = 0 ; l<6; l++){
                    for(int v = 0; v < buttons[k].length; v++){
                        System.out.print(buttons[l][v].getText() + " ");

                        buttons[l][v].setBounds(c, d, 50, 53);

                        c+=102;
                        e+=110;
                        frame.add(buttons[l][v]);

                        buttons[l][j].setBackground(Color.WHITE);
                        buttons[l][j+1].setBackground(Color.WHITE);
                    }
                    System.out.println();
                    c=6;
                    e=45;
                    d+=76;
                    f+=78;
                }
            }//main for ends here

        }

        frame.setSize(700, 700);
        frame.setVisible(true);
        for(int j=1;j<6;j++){
            for(int i=0;i<buttons[0].length;i++){
                buttons[j][i].setVisible(false);

            }
        }
        for(int j=1;j<buttons[0].length;j++){
            Thread.sleep(200);
            for(int i=0;i<buttons[0].length;i++){   
                buttons[j][i].setVisible(true);

            }
        }
    }
}
//这是主GUI类
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类GUIMain{
气泡gui=新气泡();
选择gui2=新选择();
专用JFrame主机;
私人JLabel headerLabel;
专用JLabel状态标签;
专用JPanel控制面板;
公共服务{
prepareGUI();
}
公共静态void main(字符串[]args){
GUIMain swingcontoldemo=new GUIMain();
swingControlDemo.showEventDemo();
}
私有void prepareGUI(){
大型机=新JFrame(“排序动画”);
大型机。设置大小(400400);
大型机.setLayout(新的GridLayout(3,1));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//标签用于文本
headerLabel=新的JLabel(“,JLabel.CENTER”);
statusLabel=新的JLabel(“,JLabel.CENTER”);
statusLabel.setSize(350100);
mainFrame.addWindowListener(新的WindowAdapter(){
公共作废窗口关闭(WindowEvent WindowEvent){
系统出口(0);
}        
});
//面板包含按钮和文本
控制面板=新的JPanel();
setLayout(新的FlowLayout());
mainFrame.add(headerLabel);
添加(控制面板);
mainFrame.add(状态标签);
mainFrame.setVisible(true);
}
私有void showEventDemo(){
headerLabel.setText(“选择任何排序算法!”);
JButton插入=新JButton(“插入”);
JButton Selection=新JButton(“Selection”);
JButton Bubble=新JButton(“Bubble”);
JButton外壳=新JButton(“外壳”);
//JButton Quick=新JButton(“Quick”);
Insertion.setActionCommand(“插入”);
Selection.setActionCommand(“选择”);
Bubble.setActionCommand(“Bubble”);
Shell.setActionCommand(“Shell”);
//Quick.setActionCommand(“Quick”);
addActionListener(新按钮ClickListener());
Selection.addActionListener(新按钮ClickListener());
Bubble.addActionListener(新按钮ClickListener());
addActionListener(新按钮ClickListener());
//Quick.addActionListener(新按钮ClickListener());
控制面板。添加(插入);
控制面板。添加(选择);
控制面板。添加(气泡);
控制面板。添加(外壳);
//控制面板。添加(快速);
mainFrame.setVisible(true);
}
私有类按钮ClickListener实现ActionListener{
已执行的公共无效操作(操作事件e){
String command=e.getActionCommand();
if(command.equals(“插入”)){
statusLabel.setText(“单击插入按钮”);
}
else if(command.equals(“Selection”)){
试一试{
gui2.sort();
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
statusLabel.setText(“单击选择按钮”);
}
else if(command.equals(“气泡”))
{
statusLabel.setText(“单击气泡按钮”);
试一试{
gui.sort();
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}      
else if(command.equals(“Shell”)){
statusLabel.setText(“单击外壳按钮”);
}
其他的
{
statusLabel.setText(“快速点击按钮”);
}
}     
}
}`
//这是气泡排序类
导入javax.swing.*;
导入javax.swing.border.border;
导入javax.swing.border.LineBorder;
导入java.awt.*;
公共阶级泡沫{
公共静态void main(字符串[]args)引发InterruptedException{
气泡b=新气泡();
b、 排序();
}
//////////////////////////////////
////排序启动了她
/////////////////////////////////
public void sort()引发InterruptedException{
JFrame=newJFrame(“气泡排序”);//命名javaframe
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int k=0;//k=0
frame.setLayout(null);//使布局为null
//Border thickBorder=新的线条边框(颜色:黑色,12);
边框细=新的线条边框(颜色为红色,12);
JButton[][]按钮=新JButton[6][6];//定义按钮和标签的数组

对于(int i=0;iI也不知道在Google上搜索什么相关问题您能提供源代码给我们吗?
actionPerformed
是从事件调度线程运行的,事件调度线程上的某些东西做一些需要很长时间的事情是不好的,例如
thread.sleep
或长时间的计算,因为这会导致使其他Swing操作无法完成。您应该仔细查看。此外,按照惯例,变量名称以小写字母开头。声明变量,例如
JButton Bubble
,尤其是