开发者

Having problem updating JLabel texts; relating to GUI Builder of Netbeans 6.9

开发者 https://www.devze.com 2023-02-28 11:36 出处:网络
I\'m having problem updating the texts of JLabels on my GUI in the program at开发者_StackOverflow中文版tached below.

I'm having problem updating the texts of JLabels on my GUI in the program at开发者_StackOverflow中文版tached below.

The lblArray appears to be null even though I've assigned each JLabel to corresponding index in the array.

I did try experimenting with static modifier by removing the static modifier from the JLabels & maybe some other relevant variables like the lblArray, and instantiating a class of Blackjack inside the main() method and accessing the JLabels from that new object. But the resulting interface is temperamental, with varying results every time, but basically alternating between 3 kinds of GUI glitch, which sometimes could update but some other time could not.

The GUI is generated from the GUI Builder in NetBeans 6.9.

I'm suspecting there's something to do with the static modifier. Please explain to me if it's that problem. Besides, may I know how do we actually access between static and non-static variables or methods, especially accessing non-static variables from the static main() method?

Btw, another problem I've encountered is not having adequate flexibility to modify the coding generated from the GUI Builder. Is there a way to modify the coding flexibly?

I'm attaching excerpts from my coding over here, as the original coding could be lengthy. I've already included whatever I think is relevant to this issue. There's automatic code generation from the GUI Builder in the initComponents() method, which I omit from the coding below.

public class Blackjack extends javax.swing.JFrame {

static Deck deck = new Deck(1);
static int numberOfPlayer = 1;
static Player playerArray[];
static int sequence;
static JLabel lblArray[];

/** Creates new form Blackjack */
public Blackjack() {
    initComponents();
}

// Variables declaration - do not modify
private javax.swing.JButton btnHit;
private javax.swing.JButton btnStand;
private javax.swing.JButton btnSurrender;
private static javax.swing.JLabel lblCard1;
private static javax.swing.JLabel lblCard10;
private static javax.swing.JLabel lblCard2;
private static javax.swing.JLabel lblCard3;
private static javax.swing.JLabel lblCard4;
private static javax.swing.JLabel lblCard5;
private static javax.swing.JLabel lblCard6;
private static javax.swing.JLabel lblCard7;
private static javax.swing.JLabel lblCard8;
private static javax.swing.JLabel lblCard9;
private javax.swing.JLabel lblDealer;
private javax.swing.JLabel lblPlayer;
private javax.swing.JLabel lblTitle;
private javax.swing.JPanel pnlDealer;
private javax.swing.JPanel pnlOptions;
javax.swing.JPanel pnlPlayer;
// End of variables declaration

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Blackjack().setVisible(true);
        }
    });

    lblArray = new JLabel[10];
    lblArray[0] = lblCard1;
    lblArray[1] = lblCard2;
    lblArray[2] = lblCard3;
    lblArray[3] = lblCard4;
    lblArray[4] = lblCard5;
    lblArray[5] = lblCard6;
    lblArray[6] = lblCard7;
    lblArray[7] = lblCard8;
    lblArray[8] = lblCard9;
    lblArray[9] = lblCard10;
    }

    public static void initialPlay() {

    //Drawing of first 2 cards
    for (int i=0; i<2; i++) {
        Card draw = deck.draw();

        if (!playerArray[sequence].isDealer()) {
            lblArray[i].setText(draw.getName());
        }
        else {
            lblArray[i+5].setText(draw.getName());
        }
    }
    }

Really hope that u can help me out, as I'm rushing this for assignment due. Thanks a lot!


The gui (and with it its attributes/members) might not have been initialized when you're putting the labels into the array, as the gui creation is done in another thread.

Remove the static keyword and move your code assigning the labels into the Blackjack-constructor.


You haven't initialized the value of integer called Sequence? That was probably why the null value was assigned to variable Sequence.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号