-1

我正在开发一个类项目,其中一段代码显示为图像,按钮隐藏在代码中的错误位置。这个想法是用户可以单击他们认为错误所在的代码区域并且按钮变为可见 - 我已将其设置为半透明红色,以便用户仍然可以在下面出现错误。代码中发生的事情是按钮正在工作,但是当我点击另一个按钮时,按钮通过最后一个按钮进入视图。例如,第一个按钮在错误“j k”上,单击时它变为红色,仍然可以看到错误。但是,当点击错误为“i+j”的下一个按钮时,第一个按钮会更改为显示“i+j”,出现第二个按钮错误。由于错误嵌入在图像中,我不太确定这是如何发生的。

package gui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Tutorial1Test extends JFrame {
private JPanel contentPane; 

/**
 * 
 */
private static final long serialVersionUID = 1L;
JButton one = new JButton();
JButton two = new JButton();
JButton three = new JButton();
JButton four = new JButton();
JButton five = new JButton();
JButton six = new JButton();
JButton seven = new JButton();
JButton eight = new JButton();
JButton nine = new JButton();

int clickCount = 0;

/**
 * Launch the application.
 * 
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Tutorial1Test frame = new Tutorial1Test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Tutorial1Test() throws IOException {


    //create, format and locate jframe
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit program when framed closed
    setBounds(300, 75, 800, 600);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JLabel background = new JLabel(new ImageIcon("src/gui/Tutorial1TestImage.png"));
    background.setBounds(0, 0, 800, 600);
    contentPane.add(background);

    JLabel count1 = new JLabel("count");
    count1.setBounds(100,110,45,20);
    //count1.setText(Integer.toString(clickCount));
    contentPane.add(count1);

    one = new JButton();
    one.setBounds(100,110, 45, 20);
    hideButton(one);
    contentPane.add(one);

    one.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(one);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


    two = new JButton();
    two.setBounds(100, 215, 45, 20);
    hideButton(two);
    contentPane.add(two);

    two.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {                               
                showButton(two);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    three = new JButton();
    three.setBounds(95, 240, 150, 20);
    hideButton(three);
    contentPane.add(three);

    three.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {                               
                showButton(three);


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    four = new JButton();
    four.setBounds(275, 265, 45, 20);
    hideButton(four);
    contentPane.add(four);

    four.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(four);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    five = new JButton();
    five.setBounds(320, 365, 45, 20);
    hideButton(five);
    contentPane.add(five);

    five.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(five);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    six = new JButton();
    six.setBounds(35, 395, 45, 20);
    hideButton(six);
    contentPane.add(six);

    six.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(six);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    seven = new JButton();
    seven.setBounds(100, 440, 45, 20);
    hideButton(seven);
    contentPane.add(seven);

    seven.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(seven);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    eight = new JButton("");
    eight.setBounds(100, 520, 45, 20);
    hideButton(eight);
    contentPane.add(eight);

    eight.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(eight);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    nine = new JButton("");
    nine.setBounds(550, 545, 45, 20);
    hideButton(nine);
    contentPane.add(nine);

    nine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try {   

                showButton(nine);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

//after you create your panel
contentPane.addMouseListener(new MouseAdapter() {  
     public void mouseClicked(MouseEvent evt) {  
         if (evt.getClickCount() >=12) {

         //close window

         }  
         else {  
            //display number of clicks
            clickCount =  evt.getClickCount();

             }  
         }  
 });
}
public static void hideButton(JButton button){

    //change button settings so not visible on opening
    button.setFocusPainted(false);
    button.setContentAreaFilled(false);
    button.setBorderPainted(false);
    button.setOpaque(false);

}
public static void showButton(JButton button){

    //change button back to visible but transparent with colour to highlight error
    button.setOpaque(true);
    button.setContentAreaFilled(true);
    button.setBackground(new Color(255,0,0,25)); 


}


}

在此处输入图像描述

4

1 回答 1

1

I would do things differently:

  • I would give my program an array of Rectangles or ArrayList<Rectangle> and fill the collection with a list of the "active" rectangles on the image.
  • I would give my program a Rectangle variable, say called pressedRect that is initially set to null.
  • I would have my gui class extend JPanel and give it a MouseListener.
  • In this listener's mousePressed(...) method, I would iterate through the Rectangles in the array or collection to see if any of them have been pressed.
  • If pressed, I would set the pressedRect variable to the Rectangle identified in the Mouselistener.
  • I would draw the image in the paintComponent(...) method of a JPanel.
  • In the same paintComponent(...) method, I'd check if pressedRect is null and if not, I'd fill it in using Graphics2D#fill(...) method. You could use a translucent color for this such as new Color(0, 80, 0, 80).
于 2013-04-30T21:34:19.840 回答