我想輸出的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是:mbushMatricen(rreshtii,shtylle);
我也曾嘗試這些方法:
公共無效的actionPerformed(ActionEvent的事件) {
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)
可以請你幫我...我是在Java
但你能幫助我嗎?你能說我該怎麼辦......在這兩種情況下,我有問題...請 – user3233650
看到這個鏈接[http://stackoverflow.com/questions/21343026/how-to-convert-a-matrix-in -string-in-order-to-output-it-in-jtextarea/21343557?noredirect = 1#comment32179499_21343557] –
您是否考慮過使用JTable? – MadProgrammer