2014-01-24 3 views
0

내가 출력 JTextArea에의 행렬을 노력하고 있어요,하지만 난 출력하기 위해 문자열로 그것을 행렬을 변환에 문제가있는 매트릭스는 ... 내 전체 클래스입니다 :출력 JTextArea에

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

import java.awt.*; 

class ConvertMatrix extends JFrame implements ActionListener 
{ 

    JLabel rows = new JLabel ("Numri i rreshtave"+'\n'); 
    JTextField inrows = new JTextField (5); 
    JLabel columns = new JLabel ("Numri i kolonave eshte"); 
    JTextField incolumns = new JTextField (5); 
    JLabel matrix = new JLabel("Matrica ka formen"); 
    JTextField inmatrix = new JTextField(30); 
    JButton mat = new JButton("Afisho matricen"); 
    JTextArea matric = new JTextArea(10,21); 




    int x; 
    int y; 
    int[][] matrica = new int [x][y]; 




    public ConvertMatrix() 
    { 
     super ("Matrica e konvertuar"); 
     setSize(300, 250); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     Container content = getContentPane(); 
     content.setLayout(new FlowLayout()); 
     content.setBackground(Color.pink); 
     content.add(rows); 
     rows.setForeground(Color.blue); 
     content.add(inrows); 
     content.add(columns); 
     columns.setForeground(Color.red); 
     content.add(incolumns); 
     content.add(matrix); 
     content.add(inmatrix); 
     matrix.setForeground(Color.gray); 
     content.add(mat); 

     content.add(matric); 


     mat.addActionListener(this); 


     setContentPane(content); 

    } 
public void mbushMatricen(int x, int y){ 

    for (int i =0; i<x; i++) 

    for (int j=0; j<y; j++) 

    matrica[i][j]=(int) ((double) Math.random()*10);  

     } 

    public void actionPerformed(ActionEvent event) 
    { 

     String rresht = inrows.getText(); 
     int rreshtii = Integer.parseInt(rresht);//kthimi i stringut ne integer 
     String shtyll = incolumns.getText(); 
     int shtylle = Integer.parseInt(shtyll); 
     mbushMatricen(rreshtii,shtylle); 
     String matricaString = ""; 
      for(int i=0; i<rreshtii; i++){ 
       for(int j=0; j<shtylle; j++){ 
        matricaString += matrica[i][j] + " "; 
       } 
       matricaString += "\n"; 
      } 
      matric.setText(matricaString);  


    } 



     public static void main(String []args) 
     { ConvertMatrix m = new ConvertMatrix(); 



} 




     } 


the problem is that it gives me these error: 
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 
    at ConvertMatrix.mbushMatricen(ConvertMatrix.java:87) 
    at ConvertMatrix.actionPerformed(ConvertMatrix.java:98) 

곳 라인 (87)은 : matrica[i][j]=(int) ((double) Math.random()*10); 라인 98은
여기서

공개 무효의 actionPerformed (ActionEvent의 이벤트),617,451 : 또한 이러한 방법을 시도했다

mbushMatricen(rreshtii,shtylle);{

 String rresht = inrows.getText(); 
     int rreshtii = Integer.parseInt(rresht);//kthimi i stringut ne integer 
     String shtyll = incolumns.getText(); 
     int shtylle = Integer.parseInt(shtyll); 
     mbushMatricen(rreshtii,shtylle); 
      StringBuilder matricaString = new StringBuilder(); 

      for(int i=0; i<rreshtii; i++) 
       for(int j=0; j<shtylle; j++) 


       matricaString.append(Character.toString(matrica[i][j])); 


matric.setText(matricaString.toString()); 

    } 

하지만 나에게 말한다 : The method toString(char) in the type Character is not applicable for the arguments (int)

당신이 나를 도울 수 ... 나는 자바

+0

도와 주실 수 있습니까? 당신은 나에게 무엇을 할 수 있는지 말해 줄 수 있니? 두 가지 경우 모두 나는 문제가있다 ... please – user3233650

+0

이 링크보기 [http://stackoverflow.com/questions/21343026/how-to-convert-a-matrix-in -string-in-order-to-it-in-jtextarea/21343557? noredirect = 1 # comment32179499_21343557] –

+0

대신 'JTable'을 사용 해본 적이 있습니까? – MadProgrammer

답변

0

에서 초보자 이것은 잘 작동 사항 :

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import java.awt.*; 

public class ConvertMatrix extends JFrame implements ActionListener{ 
    JLabel rows = new JLabel ("Numri i rreshtave"+'\n'); 
    JTextField inrows = new JTextField (5); 
    JLabel columns = new JLabel ("Numri i kolonave eshte"); 
    JTextField incolumns = new JTextField (5); 
    JLabel matrix = new JLabel("Matrica ka formen"); 
    JTextField inmatrix = new JTextField(30); 
    JButton mat = new JButton("Afisho matricen"); 
    JTextArea matric = new JTextArea(10,21); 

    int x; 
    int y; 
    double[][] matrica; 

    public ConvertMatrix(){ 
     super ("Matrica e konvertuar"); 
     setSize(300, 250); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     Container content = getContentPane(); 
     content.setLayout(new FlowLayout()); 
     content.setBackground(Color.pink); 
     content.add(rows); 
     rows.setForeground(Color.blue); 
     content.add(inrows); 
     content.add(columns); 
     columns.setForeground(Color.red); 
     content.add(incolumns); 
     content.add(matrix); 
     content.add(inmatrix); 
     matrix.setForeground(Color.gray); 
     content.add(mat); 

     content.add(matric); 

     mat.addActionListener(this); 

     setContentPane(content); 
    } 

    public void mbushMatricen(int x, int y){ 
     matrica = new double[x][y]; 
     for (int i =0; i<x; i++){ 
     for (int j=0; j<y; j++){ 
      matrica[i][j]=((double) Math.random()*10);  
     } 
    } 
} 

public void actionPerformed(ActionEvent event){ 
    String rresht = inrows.getText(); 
    int rreshtii = Integer.parseInt(rresht);//kthimi i stringut ne integer 
    String shtyll = incolumns.getText(); 
    int shtylle = Integer.parseInt(shtyll); 
    mbushMatricen(rreshtii,shtylle); 
    String matricaString = ""; 
     for(int i=0; i<rreshtii; i++){ 
      for(int j=0; j<shtylle; j++){ 
       matricaString += matrica[i][j] + " "; 
      } 
     matricaString += "\n"; 
     } 
    matric.setText(matricaString); 
} 

    public static void main(String []args){ 
     ConvertMatrix m = new ConvertMatrix(); 
    } 
} 
+0

고마워 ... 많이 도와 줘 ... 행렬 정수에 숫자를 넣을 방법이있어 ... 두배로하고 싶지 않아. – user3233650

+0

고마워. 찾았 어. – user3233650

0

을 당신 DecimalFormat 클래스를 사용해야합니다.

import java.text.DecimalFormat; 

formatter = new DecimalFormat("#0"); 

matricaString += formatter.format(matrica[i][j]) + " "; 

DecimalFormat