gpt4 book ai didi

java - 为什么字符串没有打印在 JLabel 上? ( java )

转载 作者:行者123 更新时间:2023-12-01 17:31:24 24 4
gpt4 key购买 nike

我正在尝试将字符串打印到 JLabel 上,输出应该与 JLabel 上的类似[ [1,0,1] [0,1,1]]

我已经尝试过,但似乎无法让它工作。请查看代码末尾以查看相关的 src 代码。

我的源代码:

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
}

最佳答案

您在 arrayGen() 中创建了一个新标签,这就是问题所在。

我修改了代码。

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

}

关于java - 为什么字符串没有打印在 JLabel 上? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61115663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com