简体   繁体   中英

JLabel`s property doesn`t change

I have a class , called boardGUI , it has a list of 64 labels (like a chess board). Every label coresponds to a specific tile on the board.

List<JLabel> labelList = new ArrayList<>();

In another class, I'm trying to set some of this labels opaque, with setOpaque(true) method , whenever I click on one of the labels (inside mouseClicked method).

JLabel l1 = boardGUI.labelList.get(1);
l1.setOpaque(true);

The problem is that although l1 refers the right label in labelList (I checked with the debugger) , it doesn`t make any visual change (on the GUI ).

But, if I'm trying to set opacity of the labels in the boardGUI class , it's working.

for (int i=0;i<64;i++)
   labelList.get(i).setOpaque(true);

Where can the problem be?

here is the class where I'm trying to apply the changes :

public class Controller {

    private Board board = new Board();
    private BoardGUI boardGUI = new BoardGUI();


    public Controller () {
        boardGUI.setVisible(true);
        boardGUI.addLabelListener(new LabelListener());

    }

    class LabelListener implements MouseListener{


        @Override
        public void mouseClicked(MouseEvent arg0) {
            JLabel l1 = boardGUI.labelList.get(1);
            l1.setOpaque(true);
        }

BoardGUI class (there's more code , but it's not relevant) :

public class BoardGUI extends JFrame{

    List<JLabel> labelList = new ArrayList<>();

    public BoardGUI() {
        createView();
    }

    public void createView() {
            createLabels(mainPanel);                            
        }
    public void createLabels(JPanel mainPanel) {
        int startX = 100;
        int startY = 87;
        int x = 100;
        int y = 87;
        int j = 0;
        for (int i=0;i<64;i++) {
             JLabel label = new JLabel(); 
             label.setBounds(x , y , 62, 62);
             labelList.add(label);
             mainPanel.add(label);
             if ( (i == 7*(j+1) +j )) {
                 x = startX;
                 y = startY + 62 *( i / 7);
                 j=j+1;
             }
             else {
                 x = x+62;
             }   
        }

    }

You need to set both background color and opaqueness; here's an example to show how these play together:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    frame.getContentPane().setBackground(Color.GREEN);

    JLabel label1 = new JLabel("label1");
    label1.setBackground(Color.RED);
    label1.setOpaque(false);
    frame.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            label1.setOpaque(!label1.isOpaque());
            label1.setBackground(label1.getBackground() == Color.RED ? Color.BLUE : Color.RED);
        }
        public void mouseReleased(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
    });

    frame.add(label1);
    frame.pack();
    frame.setVisible(true);
}

The label is initially transparanet, then changes to BLUE and opaque and back with every MouseClick. So basically, you would need to set the background color together with opaque (the RED color is just to demonstrate that it is never shown as the label is never both opaque and RED).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM