java画线程序drawline怎么都不管用,什么问题
packageline;importjava.math.*;importjava.awt.*;importjava.util.*;importjava.awt.Graph...
package line;
import java.math.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
import javax.swing.*;
public class draw extends JFrame{
JFrame j;
int x0=34,x1=48,y0=28,y1=69;
draw()
{
j=new JFrame("draw");
j.setVisible(true);
j.setSize(100, 100);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
Graphics g=getGraphics() ;
super.paint(g);
}
public void paint(Graphics g) {
g.drawLine(10,10,50,50);
g.setColor(Color.black);
}
}
住方法
package line;
import java.util.*;
import java.math.*;
import java.awt.*;
import javax.swing.*;
import java.math.*;
public class line extends JFrame{
public static void main(String[] args) {
// TODO Auto-generated method stub
draw d=new draw();
}
} 展开
import java.math.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
import javax.swing.*;
public class draw extends JFrame{
JFrame j;
int x0=34,x1=48,y0=28,y1=69;
draw()
{
j=new JFrame("draw");
j.setVisible(true);
j.setSize(100, 100);
j.setDefaultCloseOperation(EXIT_ON_CLOSE);
Graphics g=getGraphics() ;
super.paint(g);
}
public void paint(Graphics g) {
g.drawLine(10,10,50,50);
g.setColor(Color.black);
}
}
住方法
package line;
import java.util.*;
import java.math.*;
import java.awt.*;
import javax.swing.*;
import java.math.*;
public class line extends JFrame{
public static void main(String[] args) {
// TODO Auto-generated method stub
draw d=new draw();
}
} 展开
2个回答
展开全部
这程序编的乱七八糟的,对Java GUI的编程是根本没理解嘛。
思路如下:
绘图要绘在Panel(或JPanel)组件上,用Panel的paint()方法(或paintComponent()方法)绘制图形,所以这个类一般情况下继承Panel类。
程序修改如下:
import java.math.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
import javax.swing.*;
public class draw extends JPanel{
JFrame j;
// int x0=34,x1=48,y0=28,y1=69;
draw()
{
j=new JFrame("draw");
j.setVisible(true);
j.setSize(100, 100);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.add(this,BorderLayout.CENTER);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(10,10,50,50);
}
public static void main(String[] args)
{
new draw();
}
}
思路如下:
绘图要绘在Panel(或JPanel)组件上,用Panel的paint()方法(或paintComponent()方法)绘制图形,所以这个类一般情况下继承Panel类。
程序修改如下:
import java.math.*;
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
import javax.swing.*;
public class draw extends JPanel{
JFrame j;
// int x0=34,x1=48,y0=28,y1=69;
draw()
{
j=new JFrame("draw");
j.setVisible(true);
j.setSize(100, 100);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.add(this,BorderLayout.CENTER);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(10,10,50,50);
}
public static void main(String[] args)
{
new draw();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要在jframe中加入一个jfpanel的布局
我给你看下我的列子把。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawLineFrame extends JFrame {
public DrawLineFrame() {
DrawLinePanel dlp=new DrawLinePanel();
dlp.setBounds(0, 0, 600, 400);
this.add(dlp);
this.setSize(600,400);
this.setTitle("实验1");
this.setLayout(null);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class DrawLinePanel extends JPanel{
public void paint(Graphics g) {
g.drawLine(70, 50, 180, 50);
g.drawLine(70, 80, 180, 70);
}
}
public static void main(String[] args){
DrawLineFrame d1=new DrawLineFrame();
}
}
我给你看下我的列子把。
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawLineFrame extends JFrame {
public DrawLineFrame() {
DrawLinePanel dlp=new DrawLinePanel();
dlp.setBounds(0, 0, 600, 400);
this.add(dlp);
this.setSize(600,400);
this.setTitle("实验1");
this.setLayout(null);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class DrawLinePanel extends JPanel{
public void paint(Graphics g) {
g.drawLine(70, 50, 180, 50);
g.drawLine(70, 80, 180, 70);
}
}
public static void main(String[] args){
DrawLineFrame d1=new DrawLineFrame();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询