[英]Change the color of a word in a applet when it is clicked
我需要做的是,當用戶單擊粉紅色圓圈中間的“ Java”一詞時,它將把顏色從黑色更改為紅色。 我的問題是我不知道該怎么做,我丟了我的Java書籍,正等着一本寄來的郵件,所以我試圖通過在線論壇進行研究,但沒有找到一個好的例子來采用。 任何幫助或其他示例的鏈接將不勝感激!
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);
}
}
(您首先需要確保正確編寫所有方法名稱,並且代碼中沒有任何錯字。例如,您的init方法有錯字: inti()
)。
然后,您需要確保您的applet類實現了Runnable
“功能接口”以及MouseListener
接口。
然后,您將需要重寫或實現每個接口的抽象方法
由於您需要在發生鼠標單擊事件時更改文本的顏色,因此您應該覆蓋mouseClicked
以執行所需的操作
確保repaint()
以便您的更改適當地生效。 另外,請確保run()
方法以所需的頻率重新繪制形狀
這是最終的解決方案:
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) {}
}
(感謝Alex的解決方案來澄清問題的實質所在!)
這需要幾個步驟。
使textcolor成為該類的一個字段
private Color textcolor = Color.BLACK;
讓paint方法paint取值,然后調用它
g.setColor(textcolor); g.setFont(new Font("SansSerif",Font.BOLD, 25)); g.drawString("Java", 60, 95);
實現Mouselistener接口
public class Circle extends JApplet implements MouseListener{
使用Mouselistener初始化類
public void inti() { addMouseListener(this); getContentPane().setBackground(Color.white); }
添加方法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(); }
那將是我第一個原始的解決方案草圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.