2013-04-30 72 views
-1

我正在处理类的项目,其中一段代码显示为图像,并且按钮隐藏在代码中的错误所在的位置。这个想法是,用户可以点击他们认为是错误的代码区域,并且该按钮变得可见 - 我已经将它设置为半透明红色,以便用户仍然可以看到下面的错误。代码中发生的事情是按钮正在工作,但是当我点击另一个按钮时,按钮将显示最后一个按钮的视图。例如,第一个按钮超出错误'j k',单击时它变为红色,仍然可以看到错误。然而,当点击错误为'i + j'的下一个按钮时,第一个按钮将变为显示'i + j',并出现第二个按钮错误。由于错误嵌入在图像中,我不太确定这是如何发生的。任何帮助将是非常受欢迎的。Java中的按钮在按下之前按下的按钮的值

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

在所有的代码中,你没有向我们展示按钮获取值的位置?就目前而言,它们只是空的按钮。请告诉我们相关的代码。 – christopher 2013-04-30 20:23:31

+0

他们应该是空的,我只需要按钮变成半透明的颜色。我不想要任何文字 – user1836661 2013-04-30 21:13:55

+0

是这样的问题,我是否需要将它们设置为空字符串。在最后他们从showButton()方法获得它们的背景 – user1836661 2013-04-30 21:15:12

回答

1

我会做不同的事情:

  • 我想给我的节目矩形或ArrayList<Rectangle>的阵列,并在“活动”矩形列表填充集合图片。
  • 我会给我的程序一个矩形变量,所谓的pressedRetrieb最初设置为空。
  • 我会让我的gui类扩展JPanel并给它一个MouseListener。
  • 在这个监听器的mousePressed(...)方法中,我将遍历数组或集合中的Rectangles以查看是否有任何按钮被按下。
  • 如果按下,我会将pressed的pressed变量设置为在Mouselistener中标识的Rectangle。
  • 我会在JPanel的paintComponent(...)方法中绘制图像。
  • 在同样的paintComponent(...)方法,我会检查如果pressedReRect为空,如果不是,我会填写它使用Graphics2D#fill(...)方法。您可以对此使用半透明颜色,例如new Color(0, 80, 0, 80)