我想用我的程序做的是当我单击图像时,矩形将与JOptionPane一起出现。但是,JOptionPane是唯一出现的东西。
我尝试更改方法并添加更多类,没有任何工作>。<任何人都能解释我的问题吗?这是我的代码片段。
下面是我调用filechooser的地方,它允许我选择我的照片。此外,还有很多像标签这样的东西。
public Help(){
fc.setDialogTitle("Choose an image file to begin:");
int returnval = fc.showOpenDialog(null);
if (returnval == JFileChooser.APPROVE_OPTION){ //when user selects a file, value returned will be JFileChooser.APPROVE_OPTION
File file = fc.getSelectedFile(); //the File value of the selection is returned from a call on getSelectedFile
try{
image = ImageIO.read(file); //reads and loads File as image
}
catch (IOException e){}
System.out.println("You chose to open this file: " + file.getName());
}else
System.out.println("No file selected.");
icon = new ImageIcon(image);
label = new JLabel(icon);
tagName = new JLabel(input);
label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel());
panel.add(tagName);
}
最后,我的ImagePanel类,包含麻烦的paintComponent。还有一些mouseListeners。
class ImagePanel extends JPanel implements MouseListener, MouseMotionListener{
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
input = JOptionPane.showInputDialog("Enter tag name");
tagName.setText("You have tagged: " + input);
System.out.println(input);
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(image != null && isRectPresent){
g.setColor(Color.DARK_GRAY);
g.drawRect(x-50, y-50, 100, 100);
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
您可以编译代码并亲眼看看。如果你知道该怎么做,请告诉我:)非常感谢你!
答案 0 :(得分:2)
各种奇怪的东西:
label.addMouseListener(new ImagePanel());
label.addMouseMotionListener(new ImagePanel());
您不应该仅仅为了向组件添加侦听器而创建新的JPanel。您已经拥有该面板的实例。
addMouseMotionListener(this);
永远不要在绘画方法中为组件添加侦听器。您无法控制何时调用绘制方法,并且最终会添加多次添加的相同侦听器。
答案 1 :(得分:1)
一个注意事项:早些时候会回答一个较小的例子。
将鼠标事件x和y分配给ImagePanel中的自定义字段,其他名称如下:
int mx;
int my;
要试验的其他事情是遗漏super.paintComponent。 此外,您可能希望在g上使用更多方法:
Graphics2D g2 = (Graphics2D)g;
(分配给基类x和y绝不是一个好主意;最好使用setBounds来改变坐标。)
答案 2 :(得分:0)
try{
image = ImageIO.read(file); //reads and loads File as image
}
catch (IOException e){}
这里代码说:“让我们尝试读取图像。如果失败(抛出异常),则忽略问题并在没有图像的情况下继续。”忽视问题总是很糟糕。我们至少可以打印问题并继续。
try{
image = ImageIO.read(file); //reads and loads File as image
}
catch (IOException e){
e.printStackTrace();//print the exception
}
或打印问题并停止:
try{
image = ImageIO.read(file); //reads and loads File as image
}
catch (IOException e){
e.printStackTrace();//print the exception
System.exit(0);//stop executing
}
实际问题很可能在这里:
if(image != null && isRectPresent){
g.setColor(Color.DARK_GRAY);
g.drawRect(x-50, y-50, 100, 100);
}
我认为问题是if条件是false
(没有图像(也许有读取它的例外......?)和/或isRectPresent是false
)因此它确实没有!在if
包含一个断点,以调试模式启动程序,并在程序到达此点时检查变量image
和isRectPresent
。 (如果它没有到达那里,你知道你有一个不同的问题。)祝你好运!