如何在JFrame中添加其他类的输出?

时间:2013-10-04 00:14:46

标签: java swing

我想将其他类的输出放在JFrame中代替代码:

在主要课堂内

 import java.util.Arrays;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.*;

public class Main {
  public static void main(String[] args){

    int choice;
    int g;
    String randomNo;
    Scanner input=new Scanner(System.in);
    randomNo=JOptionPane.showInputDialog(null,"Enter how many random no:");
    g=Integer.parseInt(randomNo);

    RandomAlgo rand=new RandomAlgo();
    int[] data=rand.randomNum(g);

    JOptionPane.showMessageDialog(null,"Your Random numbers Are  : " + Arrays.toString(data));

    String randomChoice;
    randomChoice=JOptionPane.showInputDialog(null, "Choose Sorting algorithm: \n (1)Selection Sort \n (2)Insertion Sort \n (3)Bubble Sort \n (4)Quick Sort" );
    choice=Integer.parseInt(randomChoice);

    switch(choice){
      case 1:{
        SortingAlgo algo=new SortingAlgo();
        data=algo.selectionSort(data);
        break;
      }
      case 2:{
        SortingAlgo algo=new SortingAlgo();
        data=algo.InsertionSort(data);
        break;
      }
      case 3:{
        SortingAlgo algo=new SortingAlgo();
        data=algo.bubbleSort(data);
        break;
      }
      case 4:{
        SortingAlgo algo=new SortingAlgo();
        data=algo.quickSort(data);

      }
    }
  }
  public Main(){
    JFrame frame=new JFrame("Sorted List");
    JLabel jdate=new JLabel("");
    frame.setSize(300,500);
    frame.setVisible(true);
  }


}

然后这是我的SortingAlgo类........................................ ................

  import java.util.Arrays;
public class SortingAlgo{
  public int[] selectionSort(int[] data){
    int lenD = data.length;
    int j = 0;
    int tmp = 0;
    for(int i=0;i<lenD;i++){
      j = i;
      for(int k = i;k<lenD;k++){
        if(data[j]>data[k]){
          j = k;
        }
      }
      tmp = data[i];
      System.out.println("\n"+ Arrays.toString(data));
      data[i] = data[j];
      data[j] = tmp;
    }
      return data;
  }
  public int[] InsertionSort(int[] data){
  int len = data.length;
  int key = 0;
  int i = 0;
  for(int j = 1;j<len;j++){
    key = data[j];

    i = j-1;

    while(i>=0 && data[i]>key){
      data[i+1] = data[i];
      i = i-1;
      data[i+1]=key;
       System.out.println("\n"+ Arrays.toString(data)); 
    }
  }
  return data;
  }

  public int[] bubbleSort(int[] data){
  int lenD = data.length;
  int tmp = 0;
  for(int i = 0;i<lenD;i++){

    for(int j = (lenD-1);j>=(i+1);j--){
      System.out.println("\n"+ Arrays.toString(data));
      if(data[j]<data[j-1]){
        tmp = data[j];

        data[j]=data[j-1];

        data[j-1]=tmp;


      }
    }  System.out.println("\n"+ Arrays.toString(data));

  }
  return data;
  }
  public int[] quickSort(int[] data){
    int lenD = data.length;
    int pivot = 0;
    int ind = lenD/2;
    int i,j = 0,k = 0;
    if(lenD<2){
      return data;
    }
    else{
      int[] L = new int[lenD];
      int[] R = new int[lenD];
      int[] sorted = new int[lenD];
      pivot = data[ind];
      for(i=0;i<lenD;i++){
        if(i!=ind){
          if(data[i]<pivot){
            L[j] = data[i];
            j++;
          }
          else{
            R[k] = data[i];
            k++;
          }
        }

      }
      int[] sortedL = new int[j];
      int[] sortedR = new int[k];
      System.arraycopy(L, 0, sortedL, 0, j);
      System.arraycopy(R, 0, sortedR, 0, k);
      sortedL = quickSort(sortedL);
      sortedR = quickSort(sortedR);
      System.arraycopy(sortedL, 0, sorted, 0, j);
      sorted[j] = pivot;
      System.arraycopy(sortedR, 0, sorted, j+1, k);
      System.out.println("\n"+ Arrays.toString(sorted));
      return sorted;
    }
  }
}

我的随机课.................................

import java.util.HashSet;
import java.util.*;
public class RandomAlgo { 

  public int[] randomNum(int g){ 
        Random rand = new Random();

        int[] randomNumbers = new int[g];

        for (int i = 0; i < g; i++) {
            int e = rand.nextInt(1000);
            randomNumbers[i] = e;

        }

        return randomNumbers;
    }

  }

我的代码在控制台中工作。但我现在想把排序列表的输出放在一个框架内。而不是在控制台上。

3 个答案:

答案 0 :(得分:1)

另一个解决方案是将标准输出(即 System.out )重定向到JFrame中的文本区域

开发Custom PrintStream并调用

就足够了
System.setOut( new MyCustomPrintStream() );

要了解更多详情“How to redirect console content to a textArea in java?

答案 1 :(得分:0)

关于,

System.out.println("\n"+ Arrays.toString(data));
**//is this good? that i placed print here so i can show the ouput to the each sorting steps?**

不,这不会很好。 println打印到控制台而不是GUI。你的selectionSort()方法已经返回一个int数组 - 那么为什么不简单地使用它呢?我将从您的selectionSort方法获取数组结果,并在GUI中使用for循环遍历数组并在JTextArea中打印结果。

switch(choice){
  case 1:{
    SortingAlgo algo=new SortingAlgo();
    data=algo.selectionSort(data);

    // you've got your sorted data here. 
    // put your for loop here to show it in your GUI

    break; 
  }

答案 2 :(得分:0)

JFrame开头。

创建JPanel并向其中添加一堆JRadioButton个。这些将作为您的选择,Selection SortInsertion SortBubble SortQuick Sort。将其中的每一个添加到ButtonGroup,以便只能选择按钮。

在此面板中添加JTextField,这将作为“随机数字长度”值(您也可以使用JSpinner,但不要让事情复杂化。

向面板添加JButton。这将作为“排序”按钮,它将接受输入并实际进行排序。将ActionListener添加到此按钮

将此面板添加到框架的BorderLayout.WEST位置...

使用JPanel创建另一个GridLayout

对此,我会添加两个JList,每个JScrollPane s。这些将作为未排序和排序的列表。

将此面板添加到框架的BorderLayout.CENTER位置。

当用户点击该按钮时,您需要确定选择了哪个JRadioButton,从JTextField获取文字,将其转换为intInteger.parseInt(text) )。

生成随机数列表,将这些列表添加到第一个JList,执行排序,然后将结果添加到第二个JList

请仔细阅读......

了解更多详情......

相关问题