当前位置:编程学习 > JAVA >>

swing的问题求助各位大神!!!

package com.zs.jFrameTest;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

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

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class DrawFrame extends JFrame {

private static final long serialVersionUID = 1L;

public DrawFrame() {
setTitle("Test2");
setSize(400, 400);
DrawComponent component = new DrawComponent();
add(component);
}
}

class DrawComponent extends JComponent{

private static final long serialVersionUID = 1L;

public void printComponents(Graphics g) {
Graphics2D graphics2d = (Graphics2D) g;

double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;

Rectangle2D rectangle2d = new Rectangle2D.Double(leftX, topY, width, height);
graphics2d.draw(rectangle2d);

Ellipse2D ellipse2d = new Ellipse2D.Double();
ellipse2d.setFrame(rectangle2d);
graphics2d.draw(ellipse2d);

graphics2d.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));

double centerX = rectangle2d.getCenterX();
double centerY = rectangle2d.getCenterY();
double radius = 150;

Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
graphics2d.draw(circle);

graphics2d.drawString("asdf", 100, 100);
}
}

为什么没有效果显示出来啊?只有一个框架。。 Swing --------------------编程问答-------------------- 自己顶,坐等答案!!! --------------------编程问答--------------------
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

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

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                DrawFrame frame = new DrawFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class DrawFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public DrawFrame() {
        setTitle("Test2");
        setSize(400, 400);
        DrawComponent component = new DrawComponent();
        add(component);
    }
}

class DrawComponent extends JComponent{

    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g) {
        Graphics2D graphics2d = (Graphics2D) g;

        double leftX = 100;
        double topY = 100;
        double width = 200;
        double height = 150;

        Rectangle2D rectangle2d = new Rectangle2D.Double(leftX, topY, width, height);
        graphics2d.draw(rectangle2d);

        Ellipse2D ellipse2d = new Ellipse2D.Double();
        ellipse2d.setFrame(rectangle2d);
        graphics2d.draw(ellipse2d);

        graphics2d.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height));

        double centerX = rectangle2d.getCenterX();
        double centerY = rectangle2d.getCenterY();
        double radius = 150;

        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
        graphics2d.draw(circle);

        graphics2d.drawString("asdf", 100, 100);
    }
}
--------------------编程问答-------------------- 哪里不一样吗?!!!没看出来哪里的毛病啊!! --------------------编程问答-------------------- 类名不一样,Test2 to Hello. --------------------编程问答-------------------- class DrawComponent extends JComponent{
     
    private static final long serialVersionUID = 1L;
 
    public void printComponents(Graphics g) {

这里的方法应该是paintComponent(Graphics g) --------------------编程问答-------------------- LZ 重写了printComponents
2L大神重新了paintComponent

不晓得有啥区别。。 --------------------编程问答-------------------- protected void paintComponent(Graphics g)
  如果 UI 委托为非 null,则调用该 UI 委托的 paint 方法。向该委托传递 Graphics 对象的副本,以保护其余的 paint 代码免遭不可取消的更改(例如 Graphics.translate)。 
  如果在子类中重写此方法,则不应该对传入的 Graphics 进行永久更改。例如,不应更改剪贴区的 Rectangle 或修改转换。如果需要进行这些操作,您会发现根据传入的 Graphics 创建一个新 Graphics 并进行操作更加方便。另外,如果不调用超类的实现,则必须遵守不透明属性,也就是说,如果此组件是不透明的,则必须以透明的颜色完全填充背景。如果不遵守不透明属性,则很可能看到可视的人为内容。 

传入的 Graphics 对象可能安装了恒等转换以外的转换。在这种情况下,如果多次应用其他转换,则可能得到不可预料的结果。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,