我有一个gui类,当点击一个按钮时,有四个按钮,程序应该运行一个排序算法的动画。问题是,当我点击一个按钮时,它弹出一个Jframe,但没有任何东西出现在jframe上,它显示为黑色。很可能是因为我在Bubble Sort动画类中使用了延迟。屏幕变为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);
}
}
}
}