英文:
Why is the String not printing on the JLabel? (Java)
问题
import java.util.*;
import javax.swing.*;
import javax.swing.JLabel;
public class ProjectFrame extends javax.swing.JFrame {
/**
* Creates new form ProjectFrame
*/
public ProjectFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(119, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProjectFrame().setVisible(true);
}
});
arrayGen();
}
public static void arrayGen() {
int[][] ranArr = new int[2][3];
for(int i = 0; i < 4; i++) {
//Get random values b/w 0-2
Random r = new Random();
int posOne = r.nextInt(2);
int posTwo = r.nextInt(3);
while (ranArr[posOne][posTwo] == 1) {
posOne = r.nextInt(2);
posTwo = r.nextInt(3);
}
ranArr[posOne][posTwo] = 1;
}
for (int x = 0; x < 2; x++) {
for (int j = 0; j < 3; j++) {
//System.out.printf("%5d ", ranArr[x][j]);
}
//System.out.println();
}
String twoD = Arrays.deepToString(ranArr);
int mid = twoD.length()/2;
String firstHalf = twoD.substring(0,mid-1);
String secondHalf = twoD.substring(mid);
String firstSecond = firstHalf + secondHalf;
JLabel jLabel1 = new JLabel();
jLabel1.setText(firstHalf+"\n"+secondHalf);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
英文:
I am trying to print a string onto JLabel, the output should look like on the JLabel
[ [1,0,1]
[0,1,1] ]
I have tried but I cant seem to get this to work.. Please look at the end of the code to see the relevant src code..
My source code:
import java.util.*;
import javax.swing.*;
import javax.swing.JLabel;
public class ProjectFrame extends javax.swing.JFrame {
/**
* Creates new form ProjectFrame
*/
public ProjectFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(119, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProjectFrame().setVisible(true);
}
});
arrayGen();
}
public static void arrayGen() {
int[][] ranArr = new int[2][3];
for(int i = 0; i < 4; i++) {
//Get random values b/w 0-2
Random r = new Random();
int posOne = r.nextInt(2);
int posTwo = r.nextInt(3);
while (ranArr[posOne][posTwo] == 1) {
posOne = r.nextInt(2);
posTwo = r.nextInt(3);
}
ranArr[posOne][posTwo] = 1;
}
for (int x = 0; x < 2; x++) {
for (int j = 0; j < 3; j++) {
//System.out.printf("%5d ", ranArr[x][j]);
}
//System.out.println();
}
String twoD = Arrays.deepToString(ranArr);
int mid = twoD.length()/2;
String firstHalf = twoD.substring(0,mid-1);
String secondHalf = twoD.substring(mid);
String firstSecond = firstHalf + secondHalf;
JLabel jLabel1 = new JLabel();
jLabel1.setText(firstHalf+"\n"+secondHalf);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
答案1
得分: 0
在arrayGen()
函数中创建了一个新的Label,这就是问题所在。
我已经修改了代码。
import java.util.*;
import javax.swing.*;
import javax.swing.JLabel;
public class ProjectFrame extends javax.swing.JFrame {
public ProjectFrame() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(119, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
ProjectFrame f = new ProjectFrame();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
f.setVisible(true);
}
});
f.arrayGen();
}
public void arrayGen() {
int[][] ranArr = new int[2][3];
for(int i = 0; i < 4; i++) {
Random r = new Random();
int posOne = r.nextInt(2);
int posTwo = r.nextInt(3);
while (ranArr[posOne][posTwo] == 1) {
posOne = r.nextInt(2);
posTwo = r.nextInt(3);
}
ranArr[posOne][posTwo] = 1;
}
String twoD = Arrays.deepToString(ranArr);
int mid = twoD.length()/2;
String firstHalf = twoD.substring(0,mid-1);
String secondHalf = twoD.substring(mid);
String firstSecond = firstHalf + secondHalf;
jLabel1.setText(firstHalf+"\n"+secondHalf);
}
private javax.swing.JLabel jLabel1;
}
英文:
You created a new Lable in arrayGen() thats the problem.
I have modified the code.
import java.util.*;
import javax.swing.*;
import javax.swing.JLabel;
public class ProjectFrame extends javax.swing.JFrame {
/**
* Creates new form ProjectFrame
*/
public ProjectFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(119, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProjectFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
ProjectFrame f = new ProjectFrame();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//new ProjectFrame().setVisible(true);
f.setVisible(true);
}
});
f.arrayGen();
}
public void arrayGen() {
int[][] ranArr = new int[2][3];
for(int i = 0; i < 4; i++) {
//Get random values b/w 0-2
Random r = new Random();
int posOne = r.nextInt(2);
int posTwo = r.nextInt(3);
while (ranArr[posOne][posTwo] == 1) {
posOne = r.nextInt(2);
posTwo = r.nextInt(3);
}
ranArr[posOne][posTwo] = 1;
}
for (int x = 0; x < 2; x++) {
for (int j = 0; j < 3; j++) {
//System.out.printf("%5d ", ranArr[x][j]);
}
//System.out.println();
}
String twoD = Arrays.deepToString(ranArr);
int mid = twoD.length()/2;
String firstHalf = twoD.substring(0,mid-1);
String secondHalf = twoD.substring(mid);
String firstSecond = firstHalf + secondHalf;
// JLabel jLabel1 = new JLabel();
jLabel1.setText(firstHalf+"\n"+secondHalf);
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论