JLabel 从我的鼠标指针坐标吸收背景 JAVA
JLabel absorbs background from my mouse pointer coordinates JAVA
代码如上。这是一个 class,在 window 的左边有分数等。每次我移动光标时,它都会在计时器下显示不同的 "background" 我不知道为什么。这个Timer Label在JPanel no左边,有一部分是透明的。如何去掉timer下的这个垃圾。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TablePointPanel extends JPanel implements ActionListener{
private int score;
private static final long serialVersionUID = 1L;
final public JLabel Score;
final public JLabel PointLabel;
final public JLabel LevelLabel;
private JLabel TimeLabel;
int second=0;
int sets=0;
int minutes=0;
int time=0;
String BufferedTime="";
String Bufferedminutes="";
String Bufferedseconds="";
Timer timeMeasure;
public TablePointPanel() {
ImageIcon iconLogo = new ImageIcon("res\score.png");
Score = new JLabel();
Score.setForeground(Color.BLACK);
Score.setHorizontalAlignment(JLabel.CENTER);
Score.setVerticalAlignment(JLabel.CENTER);
Score.setFont(Score.getFont().deriveFont(10.0f));
//Score.setText("PUNKTY");
Image img = iconLogo.getImage();
Image newimg = img.getScaledInstance(80,80, java.awt.Image.SCALE_SMOOTH);
iconLogo = new ImageIcon(newimg);
Score.setIcon(iconLogo);
PointLabel = new JLabel();
PointLabel.setForeground(Color.BLACK);
PointLabel.setHorizontalAlignment(JLabel.CENTER);
PointLabel.setVerticalAlignment(JLabel.CENTER);
PointLabel.setFont(PointLabel.getFont().deriveFont(16.0f));
PointLabel.setText(score+"");
LevelLabel = new JLabel();
LevelLabel.setForeground(Color.BLACK);
LevelLabel.setFont(new Font("Tahoma", Font.BOLD, 10));
LevelLabel.setText(" Runda I");
SpringLayout springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, LevelLabel, 30, SpringLayout.SOUTH, PointLabel);
springLayout.putConstraint(SpringLayout.SOUTH, LevelLabel, -200, SpringLayout.SOUTH, this);
springLayout.putConstraint(SpringLayout.EAST, LevelLabel, 8, SpringLayout.EAST, Score);
springLayout.putConstraint(SpringLayout.NORTH, PointLabel, 6, SpringLayout.SOUTH, Score);
springLayout.putConstraint(SpringLayout.WEST, PointLabel, -7, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.SOUTH, PointLabel, 45, SpringLayout.SOUTH, Score);
springLayout.putConstraint(SpringLayout.EAST, PointLabel, 5, SpringLayout.EAST, Score);
springLayout.putConstraint(SpringLayout.WEST, LevelLabel, 5, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.NORTH, Score, -3, SpringLayout.NORTH, this);
springLayout.putConstraint(SpringLayout.EAST, Score, 70, SpringLayout.WEST, this);
springLayout.putConstraint(SpringLayout.WEST, Score, 6, SpringLayout.WEST, this);
springLayout.putConstraint(SpringLayout.SOUTH, Score, 80, SpringLayout.NORTH, this);
setLayout(springLayout);
this.add(Score);
this.add(PointLabel);
this.add(LevelLabel);
timeMeasure = new Timer(1000,this);
TimeLabel = new JLabel("00:00");
TimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
springLayout.putConstraint(SpringLayout.NORTH, TimeLabel, 6, SpringLayout.SOUTH, LevelLabel);
springLayout.putConstraint(SpringLayout.WEST, TimeLabel, 0, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.SOUTH, TimeLabel, 26, SpringLayout.SOUTH, LevelLabel);
springLayout.putConstraint(SpringLayout.EAST, TimeLabel, -3, SpringLayout.EAST, Score);
TimeLabel.setOpaque(false);
add(TimeLabel);
}
public void setPoint(int x)
{
score=x;
}
public void resetPoint()
{
score=0;
}
public void resetTime()
{
minutes=0;
second=-1;
time=0;
}
public int GetTime(){return time;}
public void updateDisplay(int millis)
{
// repaint();
try
{
Thread.sleep(millis);
}
catch(InterruptedException e)
{
}
}
public void paintComponent(Graphics g)
{
Image left = new ImageIcon("res\transparent\ROCK.png").getImage();
g.drawImage(left, 0, 0, this.getWidth(), this.getHeight(), null);
}
@Override
public void actionPerformed(ActionEvent e) {
time+=1;
second+=1;
if(second==60)
{
second=0;
minutes=1;
}
if(second<10)
Bufferedseconds="0"+second;
else
Bufferedseconds=second+"";
if(minutes<10)
Bufferedminutes="0"+minutes;
else
Bufferedminutes=minutes+"";
BufferedTime=Bufferedminutes+":"+Bufferedseconds;
TimeLabel.setText(BufferedTime);
}
}
变量名不应以大写字符开头。您的一些变量是正确的,而另一些则不是。保持一致!!!
绘画方法仅供绘画使用。不要在画法中做I/O。构建class时应该读取图像。
不要仅仅为了读取图像而创建 ImageIcon。相反,您可以使用 ImageIO.read(...)
做自定义绘画时不要忘记调用super.paintComponent()
。这将确保在绘制图像之前清除背景,以免出现绘制伪影。
代码如上。这是一个 class,在 window 的左边有分数等。每次我移动光标时,它都会在计时器下显示不同的 "background" 我不知道为什么。这个Timer Label在JPanel no左边,有一部分是透明的。如何去掉timer下的这个垃圾。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TablePointPanel extends JPanel implements ActionListener{
private int score;
private static final long serialVersionUID = 1L;
final public JLabel Score;
final public JLabel PointLabel;
final public JLabel LevelLabel;
private JLabel TimeLabel;
int second=0;
int sets=0;
int minutes=0;
int time=0;
String BufferedTime="";
String Bufferedminutes="";
String Bufferedseconds="";
Timer timeMeasure;
public TablePointPanel() {
ImageIcon iconLogo = new ImageIcon("res\score.png");
Score = new JLabel();
Score.setForeground(Color.BLACK);
Score.setHorizontalAlignment(JLabel.CENTER);
Score.setVerticalAlignment(JLabel.CENTER);
Score.setFont(Score.getFont().deriveFont(10.0f));
//Score.setText("PUNKTY");
Image img = iconLogo.getImage();
Image newimg = img.getScaledInstance(80,80, java.awt.Image.SCALE_SMOOTH);
iconLogo = new ImageIcon(newimg);
Score.setIcon(iconLogo);
PointLabel = new JLabel();
PointLabel.setForeground(Color.BLACK);
PointLabel.setHorizontalAlignment(JLabel.CENTER);
PointLabel.setVerticalAlignment(JLabel.CENTER);
PointLabel.setFont(PointLabel.getFont().deriveFont(16.0f));
PointLabel.setText(score+"");
LevelLabel = new JLabel();
LevelLabel.setForeground(Color.BLACK);
LevelLabel.setFont(new Font("Tahoma", Font.BOLD, 10));
LevelLabel.setText(" Runda I");
SpringLayout springLayout = new SpringLayout();
springLayout.putConstraint(SpringLayout.NORTH, LevelLabel, 30, SpringLayout.SOUTH, PointLabel);
springLayout.putConstraint(SpringLayout.SOUTH, LevelLabel, -200, SpringLayout.SOUTH, this);
springLayout.putConstraint(SpringLayout.EAST, LevelLabel, 8, SpringLayout.EAST, Score);
springLayout.putConstraint(SpringLayout.NORTH, PointLabel, 6, SpringLayout.SOUTH, Score);
springLayout.putConstraint(SpringLayout.WEST, PointLabel, -7, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.SOUTH, PointLabel, 45, SpringLayout.SOUTH, Score);
springLayout.putConstraint(SpringLayout.EAST, PointLabel, 5, SpringLayout.EAST, Score);
springLayout.putConstraint(SpringLayout.WEST, LevelLabel, 5, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.NORTH, Score, -3, SpringLayout.NORTH, this);
springLayout.putConstraint(SpringLayout.EAST, Score, 70, SpringLayout.WEST, this);
springLayout.putConstraint(SpringLayout.WEST, Score, 6, SpringLayout.WEST, this);
springLayout.putConstraint(SpringLayout.SOUTH, Score, 80, SpringLayout.NORTH, this);
setLayout(springLayout);
this.add(Score);
this.add(PointLabel);
this.add(LevelLabel);
timeMeasure = new Timer(1000,this);
TimeLabel = new JLabel("00:00");
TimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
springLayout.putConstraint(SpringLayout.NORTH, TimeLabel, 6, SpringLayout.SOUTH, LevelLabel);
springLayout.putConstraint(SpringLayout.WEST, TimeLabel, 0, SpringLayout.WEST, Score);
springLayout.putConstraint(SpringLayout.SOUTH, TimeLabel, 26, SpringLayout.SOUTH, LevelLabel);
springLayout.putConstraint(SpringLayout.EAST, TimeLabel, -3, SpringLayout.EAST, Score);
TimeLabel.setOpaque(false);
add(TimeLabel);
}
public void setPoint(int x)
{
score=x;
}
public void resetPoint()
{
score=0;
}
public void resetTime()
{
minutes=0;
second=-1;
time=0;
}
public int GetTime(){return time;}
public void updateDisplay(int millis)
{
// repaint();
try
{
Thread.sleep(millis);
}
catch(InterruptedException e)
{
}
}
public void paintComponent(Graphics g)
{
Image left = new ImageIcon("res\transparent\ROCK.png").getImage();
g.drawImage(left, 0, 0, this.getWidth(), this.getHeight(), null);
}
@Override
public void actionPerformed(ActionEvent e) {
time+=1;
second+=1;
if(second==60)
{
second=0;
minutes=1;
}
if(second<10)
Bufferedseconds="0"+second;
else
Bufferedseconds=second+"";
if(minutes<10)
Bufferedminutes="0"+minutes;
else
Bufferedminutes=minutes+"";
BufferedTime=Bufferedminutes+":"+Bufferedseconds;
TimeLabel.setText(BufferedTime);
}
}
变量名不应以大写字符开头。您的一些变量是正确的,而另一些则不是。保持一致!!!
绘画方法仅供绘画使用。不要在画法中做I/O。构建class时应该读取图像。
不要仅仅为了读取图像而创建 ImageIcon。相反,您可以使用
ImageIO.read(...)
做自定义绘画时不要忘记调用
super.paintComponent()
。这将确保在绘制图像之前清除背景,以免出现绘制伪影。