java中在面板上怎么画直线?
importjavax.swing.*;importjava.awt.*;publicclassDrawLineextendsJPanel{publicstaticvoi...
import javax.swing.*;
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JPanel p = new JPanel();
JFrame frame = new JFrame("DrawLine");
frame.add(p);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
//protected void paintComponent(Graphics g) {
//super.paintComponent(g);
// g.drawLine(50, 50, 200, 250);
}
}
请你帮我在就在这个代码基础上,在面板p上画出一条直线吧。谢谢 展开
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JPanel p = new JPanel();
JFrame frame = new JFrame("DrawLine");
frame.add(p);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
//protected void paintComponent(Graphics g) {
//super.paintComponent(g);
// g.drawLine(50, 50, 200, 250);
}
}
请你帮我在就在这个代码基础上,在面板p上画出一条直线吧。谢谢 展开
展开全部
方法1:
public static void main(String[] args) {
//JPanel p = new JPanel(); 注释掉这句
JFrame frame = new JFrame("DrawLine");
frame.add(new DrawLine());//将p对象换成本类
//因为本类继承了JPanel重写paintComponent进行绘制,是绘制到本类的panel上的,
//而不是绘制到new Panel()对象
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
方法2:
//这种方式可能 让你更理解
public static void main(String[] args) {
DrawLine dl = new DrawLine();//新建对象
dl.init();//执行初始化
}
private void init(){
//JPanel p = new JPanel(); 注释掉这句
JFrame frame = new JFrame("DrawLine");
frame.add(this);//将p对象换成本类
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(50, 50, 200, 250);
}
展开全部
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphcis2D g2d = (Graphics2D)g;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(2f));
g.setPaint(Color.BLUE);
g.drawLine(x1,y1,x2,y2);
g.draw(new Line2D.Double(x1d,y1d,x2d,y2d));
}
在JPanel子类的paintComponent方法里绘制。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
drawLine()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询