2 summer wxx summer_WXX 于 2016.05.07 10:35 提问

java GUI 问题 求好心人帮忙看看,谢谢

我有一道练习题,题目大致意思是:只用线和画圆输出“HELLO”,我写出的程序不能同时输出所有字母,哪位好心人可以帮我看看改正一下么
源码:
package test;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class DrawHelloTest{
public static void main(String[] args){

    LetterH1 h=new LetterH1();      
    LetterE1 e=new LetterE1();
    LetterL1 l=new LetterL1();
    LetterO1 o=new LetterO1();

    JFrame frame=new JFrame();
    frame.getContentPane().add(h);
    frame.getContentPane().add(e);
    frame.getContentPane().add(l);
    frame.getContentPane().add(o);
    frame.setSize(600, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

class LetterH1 extends JPanel{
public void paintComponent(Graphics g){
//g.setColor(Color.pink);
g.drawLine(50,50,50,20);
g.drawLine(70,50,70,20);
g.drawLine(50, 35, 70, 35);
}
}

class LetterE1 extends JPanel{
public void paintComponent(Graphics g){
//g.setColor(Color.pink);
g.drawLine(75,50,75,20);
g.drawLine(75,50,95,50);
g.drawLine(75, 35, 95, 35);
g.drawLine(75, 20, 95, 20);
}
}

class LetterL1 extends JPanel{
public void paintComponent(Graphics g){
//g.setColor(Color.pink);
g.drawLine(100,50,100,20);
g.drawLine(100,50,120,50);
g.drawLine(125,50,125,20);
g.drawLine(125,50,145,50);
}
}

class LetterO1 extends JPanel{
public void paintComponent(Graphics g){
//g.setColor(Color.pink);
Graphics2D g2 = (Graphics2D) g;
Rectangle2D rect = new Rectangle2D.Double(150, 20, 20, 30);
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
}
}

1个回答

yonghuwangwei
yonghuwangwei   2016.05.08 12:51
已采纳

frame那里设一下layout,就不会被覆盖了:

    frame.setLayout(new GridLayout());
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!