Java中的按钮采用之前按下的按钮的值

时间:2013-04-30 20:13:29

标签: java swing button

我正在开发一个类的项目,其中一段代码显示为图像,按钮隐藏在代码中的错误处。想法是用户可以点击他们认为错误的代码区域并且按钮变得可见 - 我已将其设置为半透明红色,因此用户仍然可以在下面显示错误。代码中发生的事情是按钮正在工作,但当我按下另一个按钮时,按钮会通过最后一个按钮获取视图。例如,第一个按钮超过错误'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)); 


}


}

enter image description here

1 个答案:

答案 0 :(得分:1)

我会采取不同的做法:

  • 我会给我的程序一个矩形数组或ArrayList<Rectangle>,并在图像上填充一个“活动”矩形列表。
  • 我会给我的程序一个Rectangle变量,比如名为 pressedRect ,最初设置为null。
  • 我希望我的gui类扩展JPanel并给它一个MouseListener。
  • 在这个监听器的mousePressed(...)方法中,我将迭代数组或集合中的矩形,看看是否有任何一个被按下。
  • 如果按下,我会将pressedRect变量设置为Mouselistener中标识的Rectangle。
  • 我会在JPanel的paintComponent(...)方法中绘制图像。
  • 在同一个paintComponent(...)方法中,我会检查pressedRect是否为空,如果没有,我会使用Graphics2D#fill(...)方法填写它。您可以使用半透明颜色,例如new Color(0, 80, 0, 80)