我写的Java里面的一个窗体程序运行之后显示的用Graphic画出来的图形总是一直在刷新怎么办

publicclassExercise2extendsJFrame{privatefinalintWINDOW_WIDTH=500;//thewidthofawindow... public class Exercise2 extends JFrame{
private final int WINDOW_WIDTH = 500; // the width of a window
private final int WINDOW_HEIGHT = 420; // the height of a window
private JMenuBar menuBar;
private JPanel panel;
private JMenu shapesMenu, colorMenu,drawingModeMenu;
private JMenuItem rectangleItem,ovalItem,polygonItem,colorChooserItem;
private JRadioButtonMenuItem fillItem,unfillItem;
private Color selectedColor;
private int i;
public void paint(Graphics g) {
int[] xCoords = { 200, 300, 395, 300, 200, 100};
int[] yCoords = { 105, 105, 210, 315, 315, 210};
super.paint(g);
g.setColor(selectedColor);
if(i == 1){
if(unfillItem.isSelected()){
g.drawRect(150, 140, 200, 140);
}else{
g.fillRect(150, 140, 200, 140);
}
}
else if(i == 2){
if(unfillItem.isSelected()){
g.drawOval(150, 140, 200, 140);
}else{
g.fillOval(150, 140, 200, 140);
}
}
else if(i == 3){
if(unfillItem.isSelected()){
g.drawPolygon(xCoords, yCoords, 6);
}else{
g.fillPolygon(xCoords, yCoords, 6);
}
}
Exercise2.this.repaint();
}
public class MenuListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(unfillItem.isSelected()){
if(e.getSource() == rectangleItem){
i = 1;
}else if(e.getSource() == ovalItem){
i = 2;
}
else if(e.getSource() == polygonItem){
i = 3;
}
else if(e.getSource() == colorChooserItem){
i = 4;
}
}
else{
if(e.getSource() == rectangleItem){
i = 1;
}
else if(e.getSource() == ovalItem){
i = 2;
}
else if(e.getSource() == polygonItem){
i = 3;
}
else if(e.getSource() == colorChooserItem){
i = 4;
}
}
}
}
}
那些其他部分因为补充太长就没写,事件监听的关联我都加了的,那些变量我也都创建过的
展开
 我来答
育知同创教育
2016-06-12 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
public class Exercise2 extends JFrame{
private final int WINDOW_WIDTH = 500; // the width of a window
private final int WINDOW_HEIGHT = 420; // the height of a window
private JMenuBar menuBar;
private JPanel panel;
private JMenu shapesMenu, colorMenu,drawingModeMenu;
private JMenuItem rectangleItem,ovalItem,polygonItem,colorChooserItem;
private JRadioButtonMenuItem fillItem,unfillItem;
private Color selectedColor;
private int i;
public void paint(Graphics g) {
int[] xCoords = { 200, 300, 395, 300, 200, 100};
int[] yCoords = { 105, 105, 210, 315, 315, 210};
super.paint(g);
g.setColor(selectedColor);
if(i == 1){
if(unfillItem.isSelected()){
g.drawRect(150, 140, 200, 140);
}else{
g.fillRect(150, 140, 200, 140);
}
}
else if(i == 2){
if(unfillItem.isSelected()){
g.drawOval(150, 140, 200, 140);
}else{
g.fillOval(150, 140, 200, 140);
}
}
else if(i == 3){
if(unfillItem.isSelected()){
g.drawPolygon(xCoords, yCoords, 6);
}else{
g.fillPolygon(xCoords, yCoords, 6);
}
}
Exercise2.this.repaint();
}
public class MenuListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(unfillItem.isSelected()){
if(e.getSource() == rectangleItem){
i = 1;
}else if(e.getSource() == ovalItem){
i = 2;
}
else if(e.getSource() == polygonItem){
i = 3;
}
else if(e.getSource() == colorChooserItem){
i = 4;
}
}
else{
if(e.getSource() == rectangleItem){
i = 1;
}
else if(e.getSource() == ovalItem){
i = 2;
}
else if(e.getSource() == polygonItem){
i = 3;
}
else if(e.getSource() == colorChooserItem){
i = 4;
}
}
}
}
}
总是一直刷新是不是哪里死循环了,检查一下代码。
追问
我根本就没有写循环啊,只写了if-else语句啊,还是循环在我不知道的地方
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式