以下是代码,可以参考一下,希望对您有帮助。
============================================
public class PrintStars {
private static final int LENGTH=10;
private static final int WIDTH=20;
public static void main(String[] args) {
for (int i = 0; i < LENGTH; i++) {
if(i==0||i==LENGTH-1){
for (int j = 0; j < WIDTH; j++) {
System.out.print("* ");
}
}else {
for (int j = 0; j < WIDTH; j++) {
if(j==0||j==WIDTH-1){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
//图形界面的,献上
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RectangleFrame extends JFrame {
RectangleFrame() {
//增加面板
JPanel jp = new JPanel();
jp.setBounds(0, 0, 800, 600);
this.add(jp);
JButton bt = new JButton("画出矩形");
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g = getGraphics();
g.setColor(Color.BLACK);
g.drawRect(50, 50, 100, 200);
}
});
jp.add(bt);
// 设置窗体的位置和大小
this.setBounds(200, 50, 800, 600);
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new RectangleFrame().setVisible(true);
}
}
你是想纯画图还是想楼上这个用某个字符打出一个矩形形状