如何在点击netbeans上设置多个jlabel的颜色?

时间:2016-03-08 12:30:42

标签: java swing netbeans jlabel

我在JPanel上有多个JLabel。当我点击JLabel时,背景变为绿色。

我想一次只制作一个绿色标签。例如,如果我点击JLabel1,它必须变为绿色。如果我点击另一个JLabel2,JLabel1必须变为红色,JLabel2必须变为绿色。

我有很多JLabel,并且无法为每个人制作活动。我搜索了很多,但我找不到任何东西。我使用的是Netbeans 8.1。

声明并初始化JLabel。这代表酒店的房间地图。

JLabels声明:

public void click(JLabel l)
{
    l.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            l.setOpaque(true);
            l.setBackground(Color.GREEN);
            System.out.println(l.getText());
            getCamId(l.getText()); 
            cam.setText(String.valueOf(idCam));
        }
    }); 
}

变绿功能:

var checkedValue = this.element.find("#"+taskCheckBoxId).prop("checked");

2 个答案:

答案 0 :(得分:4)

  1. 首先,将所有JLabel放入数组或ArrayList<JLabel>
  2. 我更喜欢使用更灵活的ArrayList,并且通常在循环内创建我的JLabel集合,例如for循环,如果我在循环之前知道要创建多少标签,或者如果我不创建while循环。
  3. 在同一个创建循环中,将创建的JLabel添加到ArrayList<JLabel>以及将容纳所有标签的JPanel容器中。
  4. JPanel容器应该有一个不错的布局,例如GridLayout,以便它能很好地显示JLabel。
  5. 必须通过myLabel.setOpaque(true)将所有JLabel设置为不透明,以便显示其背景颜色。
  6. 创建一个可以共享的MouseListener,并将其添加到ArrayList中的每个标签。
  7. 在MouseListener的mousePressed方法内部,使用for循环遍历ArrayList,在列表中保存的每个JLabel上调用setBackground(Color.RED),将所有标签的红色变为红色。
  8. 在同一方法内部通过调用MouseEvent参数上的JLabel selectedLabel = (JLabel) e.getSource()获取压缩的JLabel。
  9. 在for循环之后,通过调用它上面的setBackground(...)将JLabel的背景颜色选择为Color.GREEN。
  10. 如果您需要更具体的代码帮助,则必须显示相关代码。

    例如

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class LabelGrid extends JPanel {
        // number of columns in the "hallways"
        private static final int COLUMNS = 10;
    
        // colors of the current selection and the previous selection
        public static final Color PREVIOUS_SELECTION_COLOR = Color.RED;
        public static final Color CURRENT_SELECTION_COLOR = Color.GREEN;
    
        // Strings for the rooms.
        private String[] pRooms = { "p401", "p402", "p403", "phol" };
        private String[] aRooms = { "a301", "a302", "a303", "a304", "a305", "a306", "ahol" };
        private String[] dRooms = { "d201", "d202", "d203", "d204", "d205", "d206", "d207", "d208", "d209", "d210", "dhol" };
        private String[] rRooms = { 
                "r10", "r11", "r12", "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", "r21",
                "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", "r32", "r33", "r34", "r35", "r36",
                "r37", "r38", "r39", "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47", "r48", "r49", "r50", "r51",
                "r52", "r53", "r54", "r55", "r56", "r57", "r58", "r59" };
    
        // array of array, just to simplify
        private String[][] allRooms = {
                pRooms, aRooms, dRooms, rRooms
        };
    
        // previously selected JLabel. This will be colored RED
        private JLabel previousSelectedLabel = null;
    
        // ArrayList to hold **all** JLabels
        private List<JLabel> labelList = new ArrayList<>();
    
        public LabelGrid() {
    
            // the MouseListener:
            MyMouse myMouse = new MyMouse();
    
            // layout for the overall GUI
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    
            // go through the array of arrays
            for (String[] strings : allRooms) {
                // create a JPanel to hold each "hallway" of JLabels
                JPanel container = new JPanel(new GridLayout(0, COLUMNS, 5, 5));
                container.setBorder(BorderFactory.createLineBorder(Color.blue));
    
                // iterate through the Strings in the 2nd dimension of our 2D String array
                for (String text : strings) {
                    // create a JLabel with the String
                    JLabel label = new JLabel(text, SwingConstants.CENTER);
                    // make it bigger
                    label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                    // make it opaque
                    label.setOpaque(true);
                    // add the mouse listenr
                    label.addMouseListener(myMouse);
                    // add it to the arraylist
                    labelList.add(label);
    
                    // add it to the container JPanel
                    container.add(label);
                }
    
                // add the container JPanel to the GUI
                add(container);
            }
        }
    
        private class MyMouse extends MouseAdapter {
            @Override
            public void mousePressed(MouseEvent e) {
                // go through all the JLabels clearing all colors
                for (JLabel lbl : labelList) {
                    lbl.setBackground(null); // clear labels
                }
    
                // color the previously selected JLabel to RED 
                if (previousSelectedLabel != null) {
                    previousSelectedLabel.setBackground(PREVIOUS_SELECTION_COLOR);
                }
    
                // get currently selected JLabel
                JLabel label = (JLabel) e.getSource();
                // set it to green
                label.setBackground(CURRENT_SELECTION_COLOR);
    
                // make it now the previously selected label.
                previousSelectedLabel = label;
            }
        }
    
        private static void createAndShowGui() {
            JFrame frame = new JFrame("LabelGrid");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new LabelGrid());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                createAndShowGui();
            });
        }
    }
    

答案 1 :(得分:3)

你可能只需要:

  1. 创建1个方法,用于处理点击时JLabel发生的事情。
  2. 使用JPanel的{​​{3}}获取所有组件。
  3. 使用instance of运算符确保该组件为JLabel
  4. 如果该组件是JLabel,请将1中的方法指定为click事件处理程序。
  5. 在事件处理程序中,只需执行与上面相同的操作。事件本身应该允许您访问source,即触发事件的组件。您只需按以下步骤操作:

    1. 迭代标签并将其设置为绿色背景。*
    2. 获取事件的来源并将其设置为红色。
    3. *这可以通过保留表示当前所选标签的标签来改进。单击标签时,您将重置该标签的背景并更新它以显示用户单击的新标签。