Berikut listing program contoh pengunaan javax.swing pada java:
package latihan;
import javax.swing.*;
public class bintangX {
public static void main(String[] args) {
int n = Integer.valueOf(JOptionPane.showInputDialog("Masukkan banyak bintang"));
String[][] p = new String[n][n];
for(int i =0; i<n;i++){
for (int j = 0; j<n;j++){
if((i==j)||(j==n-i-1))p[i][j]="*";
else p[i][j]=" ";
}
}
for(int i =0; i<n;i++){
for (int j = 0; j<n;j++){
System.out.print(p[i][j]);
}
System.out.println();
}
}
}
Apa yang terjadi?
package latihan;
import javax.swing.*;
public class bintangX {
public static void main(String[] args) {
int n = Integer.valueOf(JOptionPane.showInputDialog("Masukkan banyak bintang"));
String[][] p = new String[n][n];
for(int i =0; i<n;i++){
for (int j = 0; j<n;j++){
if((i==j)||(j==n-i-1))p[i][j]="*";
else p[i][j]=" ";
}
}
for(int i =0; i<n;i++){
for (int j = 0; j<n;j++){
System.out.print(p[i][j]);
}
System.out.println();
}
}
}
Apa yang terjadi?