问题描述
- 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);
}
}
解决方案
frame那里设一下layout,就不会被覆盖了:
frame.setLayout(new GridLayout());