java paint() 方法的使用问题
我写的程序如下,没有报错,但是为什么不能runasjava程序?packagemain;importjava.awt.Color;importjava.awt.Dimen...
我写的程序如下,没有报错,但是为什么不能 run as java 程序?
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PersonToComputerMode extends JFrame {
private JPanel BoardPane;//用来画棋盘格子的pane
private void pain(Graphics g) {
g.drawLine(30, 30, 60, 60);
}
public static void mian(String args[]){
PersonToComputerMode pm =new PersonToComputerMode();
}
public PersonToComputerMode(){
super("人机对战");
//init compments
this.BoardPane=new JPanel();
BoardPane.setLayout(new FlowLayout());
Graphics g = BoardPane.getGraphics();
repaint();
//add action listenner
//common methods
this.add(this.BoardPane);
BoardPane.setBounds(350, 350, 200, 200);
this.setBounds(300, 300, 300, 300);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
this.show();
}
}
现在可以运行了,mian 写错了,但是为什么不会产生直线? 展开
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PersonToComputerMode extends JFrame {
private JPanel BoardPane;//用来画棋盘格子的pane
private void pain(Graphics g) {
g.drawLine(30, 30, 60, 60);
}
public static void mian(String args[]){
PersonToComputerMode pm =new PersonToComputerMode();
}
public PersonToComputerMode(){
super("人机对战");
//init compments
this.BoardPane=new JPanel();
BoardPane.setLayout(new FlowLayout());
Graphics g = BoardPane.getGraphics();
repaint();
//add action listenner
//common methods
this.add(this.BoardPane);
BoardPane.setBounds(350, 350, 200, 200);
this.setBounds(300, 300, 300, 300);
this.setLayout(new FlowLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
this.show();
}
}
现在可以运行了,mian 写错了,但是为什么不会产生直线? 展开
1个回答
展开全部
一共改了六处错误见注释。
完整的程序如下:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PersonToComputerMode extends JFrame {
private JPanel BoardPane;//用来画棋盘格子的pane
public void paint(Graphics g) { //这里pain函数改成paint函数
g.drawLine(30, 30, 60, 60);
}
public static void main(String args[]){ //这里mian函数改成main函数
PersonToComputerMode pm =new PersonToComputerMode();
}
public PersonToComputerMode(){
super("人机对战");
//init compments
this.BoardPane=new JPanel();
BoardPane.setLayout(new FlowLayout());
Graphics g = BoardPane.getGraphics();
repaint();
//add action listenner
//common methods
this.add(this.BoardPane);
//BoardPane.setBounds(350, 350, 200, 200);//这里注释掉一句
//this.setBounds(300, 300, 300, 300);//这里注释掉一句
this.setLayout(new FlowLayout());
this.setSize(300,300); //这里加一句
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
//this.show(); //这里注释掉一句
}
}
完整的程序如下:
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PersonToComputerMode extends JFrame {
private JPanel BoardPane;//用来画棋盘格子的pane
public void paint(Graphics g) { //这里pain函数改成paint函数
g.drawLine(30, 30, 60, 60);
}
public static void main(String args[]){ //这里mian函数改成main函数
PersonToComputerMode pm =new PersonToComputerMode();
}
public PersonToComputerMode(){
super("人机对战");
//init compments
this.BoardPane=new JPanel();
BoardPane.setLayout(new FlowLayout());
Graphics g = BoardPane.getGraphics();
repaint();
//add action listenner
//common methods
this.add(this.BoardPane);
//BoardPane.setBounds(350, 350, 200, 200);//这里注释掉一句
//this.setBounds(300, 300, 300, 300);//这里注释掉一句
this.setLayout(new FlowLayout());
this.setSize(300,300); //这里加一句
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
//this.show(); //这里注释掉一句
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询