在Java中切换为只读 [英] Toggle read-only in Java

查看:72
本文介绍了在Java中切换为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以切换为只读模式,所以当您单击窗口中的任何对象时,它仅会返回您单击的内容,而忽略该对象的常规事件处理? IE,在这种只读"模式下,如果单击按钮",它将仅返回该按钮,而不实际按下该按钮.然后我可以做类似的事情:

Is there a way to toggle a read-only mode so when you click any object in your window it simply returns what you clicked, ignoring the object's usual event handling? IE, while in this "read-only" mode, if you click on a Button, it simply returns the button, not actually pressing the button. Then I could do something like:

if ("thing pressed" == button) "do this";
else if ("thing pressed" == panel) "do that";
else "do nothing";

这是我的代码,它是一个带有3个彩色框的框架.单击第二个框,第三个框或背景将显示一条消息.单击框1不会执行任何操作.我喜欢使用新的鼠标适配器,所以我想用这种方式.

Here's my code, its a frame with 3 colored boxes. Clicking the 2nd box, the 3rd box, or the background will display a message. Clicking box 1 does nothing. I like using new mouse adapters so I want to do it this way.

现在,我要单击框1时,将框1视为已选中(如果这样可以帮助您获得图片).然后,如果您单击任何地方,包括框1,再次,框1被取消选择,并且没有其他内容(意味着框2,框3或背景的消息将显示).那时,只有单击框2或3,它们仍然不会显示其正常消息,而是会显示不同的消息.

Now what I want is when you click box 1, box 1 is treated as selected (if that helps you get the picture). Then if you click anywhere, including box 1 again, box 1 is deselected and nothing else (meaning that box 2, box 3. or the background's message will display). At that time, only if box 2 or 3 were clicked, they will still not display their normal message but a different message would be displayed.

如果我缺一小段时间,我感到非常抱歉.

I'm very sorry if I come off a little short.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Labels {
    public static void main(String[] args) {
        new Labels();
    }

    Square l1, l2, l3;
    public Labels() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        l1 = new Square();
        l2 = new Square();
        l3 = new Square();

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(120, 150);
        frame.setResizable(false);

        panel.setVisible(true);
        panel.setLayout(null);
        l1.setLocation(5, 5);
        l2.setLocation(5, 60);
        l3.setLocation(60, 5);

        l2.setColor("yellow");
        l3.setColor("black");

        l1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //do nothing
            }
        });

        l2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Pushed label 2");
            }
        });
        l3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Pushed label 3");
            }
        });
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("pushed background");
            }
        });

        frame.add(panel);
        panel.add(l1);
        panel.add(l2);
        panel.add(l3);
    }

    class Square extends JLabel{
        Color color = Color.blue;

        public Square() {
            // TODO Auto-generated constructor stub\
            setVisible(true);
            setSize(50,50);
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(color);
            g.fillRect(0, 0, 50, 50);
        }
        public void setColor(String color){
            if (color == "white") this.color = Color.white;
            else if (color == "black") this.color = Color.black;
            else if (color == "yellow") this.color = Color.yellow;
            else {
                System.out.println("Invalid color");
                return;
            }
            repaint();
        }
    }
}

推荐答案

请勿禁用任何内容.只需更改类的状态,也许可以使用一些布尔标志变量/字段,然后根据所按的内容更改这些标志.

Don't disable anything. Simply change the state of your class, perhaps by using a few boolean flag variables/fields and change these flags depending on what is pressed.

因此有一个名为label1PressedLastlabel2PressedLastlabel3PressedLast或类似名称的布尔字段,当按下标签时,请检查所有其他标志的状态,并根据这些状态更改程序的行为标志和刚刚按下的标签.然后将除与刚刚按下的标签相对应的所有标志之外的所有标志都设置为false.

So have boolean fields called label1PressedLast, label2PressedLast, and label3PressedLast or something similar, and when a label is pressed, check the states of all other flags and have your program's behavior change depending on the state of these flags and the label that was just pressed. Then set all flags to false except for the one corresponding to the label that was just pressed.

例如,只有在第一个和第三个JLabel被按下时,此小程序才会做出反应:

For example, this little program reacts only if the first and then the third JLabel have been pressed:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class FlagEg extends JPanel {
   private static final int LABEL_COUNT = 3;
   private JLabel[] labels = new JLabel[LABEL_COUNT];
   private boolean[] flags = new boolean[LABEL_COUNT];

   public FlagEg() {
      setLayout(new GridLayout(1, 0, 20, 0));
      setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

      // panel mouse listener
      addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent arg0) {
            inactivateAll();
         }
      });

      MouseListener labelsMouseListener = new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent mouseEvt) {
            myMousePressed(mouseEvt);
         }
      };

      // create JLabels and add MouseListener
      for (int i = 0; i < labels.length; i++) {
         labels[i] = new JLabel("Label " + (i + 1));
         labels[i].addMouseListener(labelsMouseListener);
         labels[i].setOpaque(true);
         labels[i].setBorder(BorderFactory.createLineBorder(Color.black));
         add(labels[i]);
      }
   }

   private void inactivateAll() {
      for (int i = 0; i < labels.length; i++) {
         labels[i].setBackground(null);
         flags[i] = false;
      }
   }

   private void myMousePressed(MouseEvent mouseEvt) {
      JLabel label = (JLabel) mouseEvt.getSource();

      // which label was pressed?
      int index = -1;
      for (int i = 0; i < labels.length; i++) {
         if (label == labels[i]) {
            index = i;
         }
      }

      // check if first label and then third pressed:
      if (flags[0] && index == 2) {
         System.out.println("first and then third label pressed!");
      }

      // reset all labels and flags to initial state
      inactivateAll();

      // set pressed label background color and set flag of label just pressed
      labels[index].setBackground(Color.pink);
      flags[index] = true;

   }

   private static void createAndShowGui() {
      FlagEg mainPanel = new FlagEg();

      JFrame frame = new JFrame("Flag Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

逻辑迭代二:只有标签1是引物" JLabel.实际上,这更容易实现,因为现在只需要一个布尔标志,该布尔标志表示被按下的标签1:

Logic iteration two: only label 1 is the "primer" JLabel. This is actually easier to implement, because now you only need one boolean flag, that representing label 1 being pressed:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class FlagEg2 extends JPanel {
   private static final int LABEL_COUNT = 3;
   private JLabel[] labels = new JLabel[LABEL_COUNT];

   private boolean label1Flag = false;

   public FlagEg2() {
      setLayout(new GridLayout(1, 0, 20, 0));
      setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

      // panel mouse listener
      addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent arg0) {
            inactivateAll();
         }
      });

      MouseListener labelsMouseListener = new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent mouseEvt) {
            myMousePressed(mouseEvt);
         }
      };

      // create JLabels and add MouseListener
      for (int i = 0; i < labels.length; i++) {
         labels[i] = new JLabel("Label " + (i + 1));
         labels[i].addMouseListener(labelsMouseListener);
         labels[i].setOpaque(true);
         labels[i].setBorder(BorderFactory.createLineBorder(Color.black));
         add(labels[i]);
      }
   }

   private void inactivateAll() {
      for (int i = 0; i < labels.length; i++) {
         labels[i].setBackground(null);
         label1Flag = false;
      }
   }

   private void myMousePressed(MouseEvent mouseEvt) {
      JLabel label = (JLabel) mouseEvt.getSource();

      // which label was pressed?
      int index = -1;
      for (int i = 0; i < labels.length; i++) {
         if (label == labels[i]) {
            index = i;
         }
      }

      if (label1Flag) {
         if (index == 1) {
            System.out.println("Label 1 and label 2 pressed");
         } else if (index == 2) {
            System.out.println("Label 1 and label 3 pressed");
         }

      }

      // reset all labels and flags to initial state
      inactivateAll();

      // if label1, then activate it
      if (index == 0) {
         labels[0].setBackground(Color.pink);
         label1Flag = true;
      }

   }

   private static void createAndShowGui() {
      FlagEg2 mainPanel = new FlagEg2();

      JFrame frame = new JFrame("Flag Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

这篇关于在Java中切换为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆