0

他のクラスからの出力を 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;
    }

  }

私のコードはコンソールで動作しています。しかし、私は今、並べ替えられたリストの出力をフレーム内に配置したいと考えています。コンソールではありません。

4

3 に答える 3

1

別の解決策として、標準出力 (つまりSystem.out ) を JFrame 内のテキスト領域にリダイレクトすることができます。

Custom PrintStream を開発して呼び出すだけで十分です

System.setOut( new MyCustomPrintStream() );

詳細については、「Java でコンソールのコンテンツを textArea にリダイレクトする方法は?

于 2013-10-04T07:26:39.857 に答える
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; 
  }
于 2013-10-04T00:16:09.860 に答える
0

から始めJFrameます。

を作成しJPanel、それに一連の を追加しJRadioButtonます。これらは、Selection SortInsertion SortBubble Sort、 の選択肢として機能しますQuick Sort。これらのそれぞれを に追加して、ButtonGroupボタンのみを選択できるようにします。

このパネルに a を追加JTextFieldします。これは「乱数の長さ」の値として機能します ( を使用することもできますがJSpinner、複雑にしないでください。

JButtonをパネルに追加します。これは、入力を受け取り、実際に並べ替えを行う「並べ替え」ボタンとして機能します。ActionListenerこのボタンに を追加

このパネルをフレームの位置に追加BorderLayout.WEST...

JPanelを使用して、別の を作成しますGridLayout

これにJList、それぞれ独自JScrollPaneの 内に 2 つの を追加します。これらは、ソートされていないリストとソートされたリストとして機能します。

このパネルをBorderLayout.CENTERフレームの位置に追加します。

ユーザーがボタンをクリックすると、どちらJRadioButtonが選択されているかを判断し、 からテキストを取得し、これを( )JTextFieldに変換する必要があります。intInteger.parseInt(text)

乱数のリストを生成し、これらを最初のJListに追加し、並べ替えを実行してから、結果を 2 番目の に追加しますJList

一読して...

詳細については...

于 2013-10-04T01:20:02.320 に答える