Myeclipse中用Matisse拖一个swing类的JPanel放在JFrame里,怎么让drawline画的线在JPanel里显示?
1个回答
展开全部
要让drawline画的线在JPanel里显示,需要在JPanel的paint方法里画线,然后把JPanel加到JFrame里。我给你个例子,你看看吧。
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();
}
}
追答
你只需要新建一个panel对象,并且把这个panel对象加到JFrame中,就行了。
根据你的代码编写的例子程序,你看看吧。(注意注释)
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class signal extends JFrame{
class panel extends JPanel{
public void paint(Graphics g) {
super.paint(g);
int d=1;
for(int i=0;i<361;i++){
int x=i;
int y=(int)(Math.sin(x*3.14/180)*100);
int y1=(int)(Math.sin((x+1)*3.14/180)*100);
g.drawLine(x,y*d+300,x+1,y1*d+300);
}
g.drawLine(0, 300, 500, 300);
}
}
public signal(){
panel p=new panel(); //创建一个panel类
this.add(p); //把panel加到JFrame中
this.setSize(600,600);
this.setTitle("实验1");
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new signal();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询