-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));
}
}
在所有的代码中,你没有向我们展示按钮获取值的位置?就目前而言,它们只是空的按钮。请告诉我们相关的代码。 – christopher 2013-04-30 20:23:31
他们应该是空的,我只需要按钮变成半透明的颜色。我不想要任何文字 – user1836661 2013-04-30 21:13:55
是这样的问题,我是否需要将它们设置为空字符串。在最后他们从showButton()方法获得它们的背景 – user1836661 2013-04-30 21:15:12