我正在尝试在JLabel上检测MouseEvent。
我有一个JLabel数组(candidateLabel = new JLabel [9]
)。他的一些JLabel包含一些数字,有些则没有。如果我离开点击并且它不为空,我已经实现了添加文本的选项。我现在希望能够在我的空标签上检测到正确的点击,但这似乎不会发生。
以下是代码:
package sudoku.view;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CaseComponent extends JPanel {
private static final long serialVersionUID = 1L;
private static final String CANDIDATES = "candidate";
private static final String DIGIT = "digit";
private static final int CASE_DIM = 60;
private static final int DIGIT_SIZE = 38;
private MouseAdapter candidatesMouseAdapter;
private MouseAdapter digitMouseAdapter;
private GridBagConstraints gridBagConstraint;
private Color caseColor;
private Color initialCaseColor;
private JLabel[] candidatesLabel;
private JLabel digitLabel;
private String currentState;
private boolean modifiable;
public CaseComponent(Color caseColor, String currentState) {
super();
this.initialCaseColor = caseColor;
this.caseColor = caseColor;
this.currentState = currentState;
this.modifiable = true;
setConfiguration();
createView();
placeComponents();
createController();
}
// REQUETES
public JLabel getDigitLabel() {
return digitLabel;
}
public JLabel[] getCandidatesLabel() {
return candidatesLabel;
}
public Color getInitialCaseColor() {
return initialCaseColor;
}
public static String getCandidateState() {
return CANDIDATES;
}
public static String getDigitState() {
return DIGIT;
}
public String getCurrentState() {
return currentState;
}
public String getDigitText() {
return digitLabel.getText();
}
public boolean isModifiable() {
return modifiable;
}
// COMMANDES
public void setModifiable(boolean modifiable) {
this.modifiable = modifiable;
}
public void setCandidatesLabel(Set<Integer> candidates) {
for (int i = 0; i < 9; ++i) {
candidatesLabel[i].setText("");
}
for (Integer i : candidates) {
candidatesLabel[i - 1].setText(i + "");
}
}
public void changeLabelForeground(Color color) {
digitLabel.setForeground(color);
for (int i = 0; i < candidatesLabel.length; ++i) {
candidatesLabel[i].setForeground(color);
}
}
public void changeDisplay() {
if (currentState.equals(CANDIDATES)) {
candidateDisplay();
} else {
digitDisplay();
}
}
public void digitDisplay() {
CardLayout cl = (CardLayout) getLayout();
cl.show(this, DIGIT);
currentState = DIGIT;
}
public void candidateDisplay() {
CardLayout cl = (CardLayout) getLayout();
cl.show(this, CANDIDATES);
currentState = CANDIDATES;
}
public void addMouseListener() {
digitLabel.addMouseListener(digitMouseAdapter);
for (int i = 0; i < 9; i++) {
candidatesLabel[i].addMouseListener(candidatesMouseAdapter);
}
}
public void removeMouseListener() {
digitLabel.removeMouseListener(digitMouseAdapter);
for (int i = 0; i < 9; i++) {
candidatesLabel[i].removeMouseListener(candidatesMouseAdapter);
}
}
// OUTILS
private void setConfiguration() {
setLayout(new CardLayout());
setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
setPreferredSize(new Dimension(CASE_DIM, CASE_DIM));
setBackground(caseColor);
}
private void createView() {
gridBagConstraint = new GridBagConstraints();
candidatesLabel = new JLabel[9];
for (int i = 0; i < 9; i++) {
candidatesLabel[i] = new JLabel("");
candidatesLabel[i].setPreferredSize(new Dimension(50, 50));
}
digitLabel = new JLabel();
digitLabel.setFont(new Font("TimesRoman", Font.PLAIN, DIGIT_SIZE));
}
private void placeComponents() {
JPanel p = new JPanel(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
JPanel q = new JPanel(new GridBagLayout()); {
q.add(candidatesLabel[i], gridBagConstraint);
}
q.setOpaque(false);
p.add(q);
}
p.setOpaque(false);
add(p, CANDIDATES);
p = new JPanel(new GridBagLayout()); {
p.add(digitLabel, gridBagConstraint);
}
p.setOpaque(false);
add(p, DIGIT);
}
private void createController() {
digitMouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)
&& isModifiable()) {
candidateDisplay();
}
}
};
digitLabel.addMouseListener(digitMouseAdapter);
candidatesMouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel source = (JLabel) e.getSource();
if (SwingUtilities.isLeftMouseButton(e)) {
System.out.print(source.getText().equals(""));
if (source.getText().equals("")) {
for (int i = 0; i < candidatesLabel.length; ++i) {
if (candidatesLabel[i].equals(source)) {
candidatesLabel[i].setText("" + (i - 1));
}
}
} else {
digitDisplay();
digitLabel.setText(source.getText());
}
} else if (SwingUtilities.isRightMouseButton(e)) {
for (int i = 0; i < candidatesLabel.length; ++i) {
if (candidatesLabel[i].equals(source)) {
candidatesLabel[i].setText("");
}
}
}
}
};
for (int i = 0; i < 9; i++) {
candidatesLabel[i].addMouseListener(candidatesMouseAdapter);
}
}
}
这里,当我在空的JLabel上单击左键时,没有任何反应(System.out.println()没有显示)。
有什么想法吗?
答案 0 :(得分:0)
如果我点击了鼠标并且它不是空的话,我已经实现了添加文字的选项
看起来你倒退了;如果您左键单击 IS 为空的正方形
,它将添加文本 if (SwingUtilities.isLeftMouseButton(e)) {
...
if (source.getText().equals("")) {
for (int i = 0; i < candidatesLabel.length; ++i) {
if (candidatesLabel[i].equals(source)) {
candidatesLabel[i].setText("" + (i - 1));
}
}
}
我现在希望能够在空标签上检测到正确的点击次数
对我来说这很有效。右键单击空标签并且文本设置为为空
时,您看不到任何更改 else if (SwingUtilities.isRightMouseButton(e)) {
for (int i = 0; i < candidatesLabel.length; ++i) {
if (candidatesLabel[i].equals(source)) {
candidatesLabel[i].setText("");
}
}
当我在空JLabel上单击左键时,没有任何反应(System.out.println()没有显示)。
如上所示,单击空标签将在其中设置一些文本。 左键单击其中包含文本的标签将导致UI从候选视图切换到数字视图
if (SwingUtilities.isLeftMouseButton(e)) {
...
if (source.getText().equals("")) {
...
} else {
digitDisplay();
digitLabel.setText(source.getText());
}
这有它自己的问题,因为digitLabel
没有文本,它的大小没有设置所以它是width = 0和height = 0所以它的mousePressed监听器永远不会被执行,你永远不能回到候选人观点