[英]In Java, why doesn't frame.repaint work in this code?
我不确定为什么fr.repaint();
方法在这里不起作用。 如果我使用 set visible false
,然后在进行更改后将其设置为true
,它会使屏幕 go 变黑,这不是我想要做的。
有什么帮助吗?
此代码用于学校项目,因此如果可能,请提供一些详细的解释以供理解。
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
class Main {
static int startingposx;
static int startingposy;
static int endingposx;
static int endingposy;
public static void main(String[] args) {
JFrame fr = new JFrame();
fr.setBounds(10,10,512,512);
fr.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
});
fr.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
startingposx = e.getX();
startingposy = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
endingposx = e.getX();
endingposy = e.getY();
//fr.setVisible(false);// i tried to use this to refresh it but it makes the frame black for a second
JPanel x = new JPanel(){
@Override
public void paint(Graphics g){
g.setColor(Color.RED);
g.fillRect(startingposx-(32-15),startingposy-32,30,30);
g.setColor(Color.BLUE);
g.fillRect(endingposx-(32-15),endingposy-32,30,30);
}
};
fr.add(x);
fr.repaint();// why does this not work??
//fr.setVisible(true);
System.out.print("\033[H\033[2J");
System.out.println("starting pos: " + startingposx+ ","+ startingposy+"\n" + "ending pos: " + endingposx+ "," + endingposy);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
请参阅代码中的更多注释。 请注意,蓝色矩形完全覆盖了红色矩形。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestCustomPaint {
// Don't use static! Make them instances of the x class and give it a constructor
static int startingposx;
static int startingposy;
static int endingposx;
static int endingposy;
public static void main(String[] args) {
JFrame fr = new JFrame();
// this is the wrong size if the content should be square
fr.setBounds(10,10,512,512);
// unused MouseMotionListener removed
fr.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) {
startingposx = e.getX();
startingposy = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
endingposx = e.getX();
endingposy = e.getY();
fr.repaint();
System.out.print("\033[H\033[2J");
System.out.println("starting pos: " + startingposx+ ","+ startingposy+"\n" +
"ending pos: " + endingposx+ "," + endingposy);
}
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
});
// This is when the panel should be created and added.
JPanel x = new JPanel(){
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(startingposx-(32-15),startingposy-32,30,30);
g.setColor(Color.BLUE);
g.fillRect(endingposx-(32-15),endingposy-32,30,30);
}
};
fr.add(x);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.