문제

I have used below code but did not work properly. I have list of JButton objects on the panel but could not click on each button individually.

for(int i=0; i<udataArr.length(); i++) {
    userBtn = new JButton();
    userLb = new JLabel();

    cur1 = userBtn.getCursor();
    userBtn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    Image imgUO =  ImageIO.read(getClass().getResource("/resources/img-std.png"));
    userBtn.setIcon(new ImageIcon(imgUO));
    userBtn.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
    userLb.setText((String) udataArr.getJSONObject(i).get("user_name"));
    //button[i].setText((String) udataArr.getJSONObject(i).get("user_name"));
    panelLeft.add(userBtn);
    panelLeft.add(userLb);
    panelLeft.add(Box.createVerticalStrut(15));
}

enter image description here

도움이 되었습니까?

해결책

This is the code to create JButton Array

JButton buttons[];
buttons = new JButton[10];
for(int i = 0; i < 10; i++) {
    buttons[i] = new JButton(String.valueOf(i));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top