简体   繁体   English

单击小程序中的单词时更改其颜色

[英]Change the color of a word in a applet when it is clicked

What I need to have happen is that when the user clicks the word "Java" in the middle of the pink circle, it will change the words color from black to red. 我需要做的是,当用户单击粉红色圆圈中间的“ Java”一词时,它将把颜色从黑色更改为红色。 My issue is that I don't know how to do it, and I lost my Java book and am waiting for one to come in the mail so I'm trying to work through online forums but just wasn't finding a good example to use. 我的问题是我不知道该怎么做,我丢了我的Java书籍,正等着一本寄来的邮件,所以我试图通过在线论坛进行研究,但没有找到一个好的例子来采用。 Any help or links to other examples would be much appreciated! 任何帮助或其他示例的链接将不胜感激!

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Circle extends JApplet{


public void inti()
{
    getContentPane().setBackground(Color.white);
}

public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(Color.black);
    g.drawOval(20, 20, 140, 140);
    g.setColor(Color.pink);
    g.fillOval(20,20,140,140);
    g.setColor(Color.BLACK);
    g.setFont(new Font("SansSerif",Font.BOLD, 25));
    g.drawString("Java", 60, 95);
}
}
  1. (You need to first make sure that your write all method names correctly and your code doesn't have any typos. For example you init method has a typo: inti() ). (您首先需要确保正确编写所有方法名称,并且代码中没有任何错字。例如,您的init方法有错字: inti() )。

  2. Then you need to ensure that your applet class implements the Runnable "Functional Interface", as well as the MouseListener Interface. 然后,您需要确保您的applet类实现了Runnable “功能接口”以及MouseListener接口。

  3. Then you would need to override or implement the abstract methods for each or those Interfaces 然后,您将需要重写或实现每个接口的抽象方法

  4. Since you need to change the color of your text when a mouse click event occurs, then you should override mouseClicked to perform your desired action 由于您需要在发生鼠标单击事件时更改文本的颜色,因此您应该覆盖mouseClicked以执行所需的操作

  5. Make sure to repaint() so that your changes takes effect appropriately. 确保repaint()以便您的更改适当地生效。 Also, ensure that the run() method repaints the shape with the desired frequency 另外,请确保run()方法以所需的频率重新绘制形状

Here's the final solution: 这是最终的解决方案:

public class Main extends JApplet implements Runnable, MouseListener {

//this member field will specify what color should be the text
//in every painting cycle
private Color textColor = Color.BLACK;


@Override
public void paint(Graphics g) {

    g.setColor(Color.black);
    g.drawOval(20, 20, 140, 140);
    g.setColor(Color.pink);
    g.fillOval(20, 20, 140, 140);
    g.setColor(Color.BLACK);

    g.setColor(textColor);
    g.setFont(new Font("SansSerif", Font.BOLD, 25));
    g.drawString("Java", 60, 95);
}

@Override
public void init() {
    //screen size --modify it as desired
    this.setSize(200, 200);
    getContentPane().setBackground(Color.white);
    addMouseListener(this);
}

public void run() {
    while ( true ) {
        repaint();

        try {
            Thread.sleep(17);  //specifies repaint frequency
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX(), y = e.getY();

    if ( x >= 60 && x <= 120 && y >= 80 && y <= 95 )
        textColor = Color.RED;
    else
        textColor = Color.black;

    repaint();


    //you can use this to change the condition of the if statement as you desire
    System.out.println("mouse clicked: x="+x+ " --- y="+y);
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}
}

(Thanks to Alex's solution for clarifying what the question was actually asking for!) (感谢Alex的解决方案来澄清问题的实质所在!)

This takes several steps. 这需要几个步骤。

  1. make the textcolor a field of the class 使textcolor成为该类的一个字段

     private Color textcolor = Color.BLACK; 
  2. let the paint method paint take the value, then its been called 让paint方法paint取值,然后调用它

     g.setColor(textcolor); g.setFont(new Font("SansSerif",Font.BOLD, 25)); g.drawString("Java", 60, 95); 
  3. implement the Mouselistener Interface 实现Mouselistener接口

     public class Circle extends JApplet implements MouseListener{ 
  4. Initilize the class with a Mouselistener 使用Mouselistener初始化类

     public void inti() { addMouseListener(this); getContentPane().setBackground(Color.white); } 
  5. Add the Method mouseClicked 添加方法mouseClicked

     mouseClicked(MouseEvent e){ e.GetX(); e.GetY(); // Get the Clickcoordinates and check if its inside the circle if(//Inside the cirle){ textcolor = Color.RED; } else{ textcolor = Color.BLACK; } // Make the class refrech its content repaint(); } 

That would be my first raw sketch of the solution. 那将是我第一个原始的解决方案草图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM