问题描述
好的:我的 Main 代码:
import javax.swing.JFrame;
public class Main extends JFrame {
public static void main(String[] args) {
new MyFrame();
}
}
我的“绘图”代码:
import java.awt.AWTException;
import java.awt.Basicstroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
// import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import static javax.swing.JOptionPane.showMessageDialog;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class MyFrame extends JPanel{
public JFrame frame = new JFrame();
public JPanel myCanvas = new JPanel();
public JButton play = new JButton();
MyFrame(){
frame.setLayout(null);
frame.setLayout(null);
frame.setTitle("Drawing game test");
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setLocationRelativeto(null);
frame.setResizable(false);
frame.getContentPane().setBackground(Color.white);
frame.setCursor(1);
frame.setVisible(true);
Border border = BorderFactory.createLineBorder(Color.black,3,false);
Font font = new Font("Calibri",Font.BOLD,25);
myCanvas.setBounds(150,150,300,300);
myCanvas.setVisible(false);
myCanvas.setBackground(Color.cyan);
frame.add(myCanvas);
play.setFont(font);
play.setBounds(150,195,200,50);
play.setText("'Play'");
play.setFocusable(false);
play.setBackground(Color.GRAY);
play.setBorder(border);
play.setVisible(true);
frame.add(play);
play.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// Todo Auto-generated method stub
}
@Override
public void mousepressed(MouseEvent e) {
// Todo Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// Todo Auto-generated method stub
play.setVisible(false);
paintComponent(getGraphics());
}
@Override
public void mouseEntered(MouseEvent e) {
// Todo Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// Todo Auto-generated method stub
}
});
}
public void paintComponent(Graphics g) {
System.out.println("I display this if I work");
Graphics2D g2D = (Graphics2D) g;
g2D = (Graphics2D) myCanvas.getGraphics();
super.paintComponent(g2D);
g2D.setColor(Color.BLACK);
g2D.setstroke(new Basicstroke(5f));
g2D.drawRect(0,200);
}
}
本质上它所做的就是制作一个可见的 jframe,当你点击一个按钮时,它会在 jpanel 上绘制。 我没有收到任何错误,尽管正方形看起来很奇怪,就像一个正方形穿过它一样。 这以前从未发生过,我很奇怪广场上可能发生了什么。
当我手动调用我的paintComponent并暂时移除按钮时没有问题,但是当按钮在那里时它会干扰绘图过程?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)