请问一下在java的一个panel中如何去绘制一条直线,谢谢
我编了以下的程序可以编译但是不能运行importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicc...
我编了以下的程序
可以编译但是不能运行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo {
public static JPanel p1;
public static JPanel p2;
public static void addComponentsToPane(Container pane)
{
pane.setLayout(new GridLayout(0,2));
setP1();
pane.add(p1);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(500,500);
frame.pack();
frame.setVisible(true);
}
public static void setP1()
{
p1=new JPanel();
p1.setBackground(Color.black);
Canvas c=new Canvas();
DebugGraphics g=new DebugGraphics();
g.drawLine(0,0,10,20);
g.setColor(Color.red);
c.paint(g);
p1.add(c);
}
public static void setP2()
{
p2=new JPanel();
}
public static void main(String[] args)throws Exception {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
要在两个面中都绘制直线。
谢谢!!! 展开
可以编译但是不能运行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo {
public static JPanel p1;
public static JPanel p2;
public static void addComponentsToPane(Container pane)
{
pane.setLayout(new GridLayout(0,2));
setP1();
pane.add(p1);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.setSize(500,500);
frame.pack();
frame.setVisible(true);
}
public static void setP1()
{
p1=new JPanel();
p1.setBackground(Color.black);
Canvas c=new Canvas();
DebugGraphics g=new DebugGraphics();
g.drawLine(0,0,10,20);
g.setColor(Color.red);
c.paint(g);
p1.add(c);
}
public static void setP2()
{
p2=new JPanel();
}
public static void main(String[] args)throws Exception {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
要在两个面中都绘制直线。
谢谢!!! 展开
展开全部
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameDemo extends JFrame {
private JPanel jp;
public FrameDemo(){
jp=new JPanel();
linePanel lp = new linePanel();
this.add(jp);
this.add(lp);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public class linePanel extends JPanel{
public void paint(Graphics g){
g.drawLine(0,0,10,20);
}
}
public static void main(String[] args) {
FrameDemo fd=new FrameDemo();
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameDemo extends JFrame {
private JPanel jp;
public FrameDemo(){
jp=new JPanel();
linePanel lp = new linePanel();
this.add(jp);
this.add(lp);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}
public class linePanel extends JPanel{
public void paint(Graphics g){
g.drawLine(0,0,10,20);
}
}
public static void main(String[] args) {
FrameDemo fd=new FrameDemo();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询