求简单代码,如何在窗体上绘制一个矩形。 通过drawRect,谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 void drawRect(int x, int y, int width, int height) Component都有getGraphics()方法然后再用得到的Graphics来drawRect(int x, int y, int width, int height) import java.awt.*;public class Test extends Frame{ public static void main(String[] args) { new Test().launch(); } public void launch() { setBounds(50,50,300,300); setVisible(true); } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.RED); g.drawRect(20, 30, 50, 80); g.setColor(c); }} /* * To change this template, choose Tools | Templates * and open the template in the editor. */package experiment16;import java.awt.*;import javax.swing.*;/** * * @author Kevin Zhang */class DrawPanel extends JFrame { DrawPanel(String s) { super(s); setSize(400, 400); setVisible(true); } // 在面板中绘制图形 public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLACK); // 把当前的前景色设置成黑色 g.drawRect(-150, -150, 100, 100); // 绘制一个正方形 }}public class Main { public static void main(String[] args) { DrawPanel drawPanel = new DrawPanel("绘制矩形"); drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} /* * To change this template, choose Tools | Templates * and open the template in the editor. */package experiment16;import java.awt.*;import javax.swing.*;/** * * @author Kevin Zhang */class DrawPanel extends JFrame { DrawPanel(String s) { super(s); setSize(400, 400); setVisible(true); } // 在面板中绘制图形 public void paint(Graphics g) { super.paint(g); g.setColor(Color.BLACK); // 把当前的前景色设置成黑色 g.drawRect(-150, -150, 100, 100); // 绘制一个正方形 }}public class Main { public static void main(String[] args) { DrawPanel drawPanel = new DrawPanel("绘制矩形"); drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} SWING镶嵌HTML地图 java基础问题 成员变量与局部变量有何区别,举例说明一下? 我还是那个连连看的 求大虾帮忙 新手问一下,Java下连Oracle取数据回来,在网格中显示,速度如何? 我是菜鸟!请问文件里面的中文ASCII乱码要如何转换啊!例如:‘指淳涂κ卜⑹?’ 这样算是面向对象么? jvm错误,大家来看看 今天加班的兄弟们辛苦了,散分 请问tomcat3 or tomcat4里的mod_webapp.so文件是怎样形成的呀,(只有100分了) cvs错误,已经发生好多次了,但是不知道怎么解决 java文件和类的安排
然后再用得到的Graphics来
drawRect(int x, int y, int width, int height)
import java.awt.*;
public class Test extends Frame{
public static void main(String[] args) {
new Test().launch();
}
public void launch() {
setBounds(50,50,300,300);
setVisible(true);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.drawRect(20, 30, 50, 80);
g.setColor(c);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package experiment16;
import java.awt.*;
import javax.swing.*;/**
*
* @author Kevin Zhang
*/
class DrawPanel extends JFrame {
DrawPanel(String s) {
super(s);
setSize(400, 400);
setVisible(true);
}
// 在面板中绘制图形
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK); // 把当前的前景色设置成黑色
g.drawRect(-150, -150, 100, 100); // 绘制一个正方形
}
}
public class Main {
public static void main(String[] args) {
DrawPanel drawPanel = new DrawPanel("绘制矩形");
drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package experiment16;
import java.awt.*;
import javax.swing.*;/**
*
* @author Kevin Zhang
*/
class DrawPanel extends JFrame {
DrawPanel(String s) {
super(s);
setSize(400, 400);
setVisible(true);
}
// 在面板中绘制图形
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK); // 把当前的前景色设置成黑色
g.drawRect(-150, -150, 100, 100); // 绘制一个正方形
}
}
public class Main {
public static void main(String[] args) {
DrawPanel drawPanel = new DrawPanel("绘制矩形");
drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}