我在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");
答案 0 :(得分:4)
ArrayList<JLabel>
。 ArrayList<JLabel>
以及将容纳所有标签的JPanel容器中。myLabel.setOpaque(true)
将所有JLabel设置为不透明,以便显示其背景颜色。setBackground(Color.RED)
,将所有标签的红色变为红色。JLabel selectedLabel = (JLabel) e.getSource()
获取压缩的JLabel。setBackground(...)
将JLabel的背景颜色选择为Color.GREEN。如果您需要更具体的代码帮助,则必须显示相关代码。
例如
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)
你可能只需要:
JLabel
发生的事情。JPanel
的{{3}}获取所有组件。instance of
运算符确保该组件为JLabel
。JLabel
,请将1
中的方法指定为click
事件处理程序。在事件处理程序中,只需执行与上面相同的操作。事件本身应该允许您访问source
,即触发事件的组件。您只需按以下步骤操作:
*这可以通过保留表示当前所选标签的标签来改进。单击标签时,您将重置该标签的背景并更新它以显示用户单击的新标签。