JFrame上的Swing图形 [英] Swing Graphics on JFrame

查看:130
本文介绍了JFrame上的Swing图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java Graphics,我试图绘制一个简单的矩形。

Using Java Graphics, I tried to draw a simple rectangle.

As Applet 它运行良好但是当我用它来显示 JFrame ,矩形即将到来但有一些不寻常的背景

As Applet it runs well but when I use it to show on a JFrame, the rectangle is coming but with some unusual background

这是编码:

package graphicsex;

import java.awt.Graphics;

public class Graphics2 extends javax.swing.JFrame {

    public static void main(String[] args) {
        Graphics2 inst = new Graphics2();
        inst.setVisible(true);
    }

    public Graphics2() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        g.drawRect(10, 20, 30, 40);
    }
}

然后我尝试使用 JTextArea 使用这两个类但在这种情况下矩形根本不显示。

Then I tried to use JTextArea using these two classes but in this case the rectangle is not displaying at all.

GraphicsEx1.java:

package graphicsex;

import javax.swing.JTextArea;
import java.awt.Graphics;

public class GraphicsEx1 extends javax.swing.JFrame {

    private JTextArea jTextArea1;

    {
        //Set Look & Feel
        try {
            javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        GraphicsEx1 inst = new GraphicsEx1();
        inst.setVisible(true);
    }

    public GraphicsEx1() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
            {
                jTextArea1 = new JTextArea();
                getContentPane().add(jTextArea1);
                jTextArea1.setBounds(7, 7, 371, 245);
                jTextArea1.setEnabled(false);
            }
            pack();
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
        postInitGUI();
    }

    public void postInitGUI() {
        DisplayItems dp = new DisplayItems();
        jTextArea1 = dp;
        dp.setVisible(true);
        this.add(jTextArea1);
    }
}

和DisplayItems.java:

And DisplayItems.java:

package graphicsex;

import java.awt.Dimension;
import java.awt.Graphics;

public class DisplayItems extends javax.swing.JTextArea {

    public DisplayItems() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setPreferredSize(new Dimension(400, 300));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        g.drawRect(10, 20, 50, 100);
        g.drawString("Kalai", 90, 150);
    }
}

任何人都可以帮助我在任何秋千上显示图形组件容器如 JFrame, JPanel JTextarea`等。

Can any one help me display graphics components on any swing containers like JFrame,JPanelorJTextarea` etc.

推荐答案

不建议覆盖顶级容器的 paint 方法......

It is inadvisable to override the paint method of a top level container...

JFrame 包含许多其他组件所放置的重要图层,请执行此操作...

JFrame contains a number of important layers onto which many other components are placed, buy doing this...

public void paint(Graphics g){
    g.drawRect(10,20,30,40);
}

您已成功阻止任何子组件开始绘制,或者事实上,除了你的矩形以外的任何东西。

You've successfully stopped any of the child components from begin painted, or in fact, anything other then your rectangle.

(顶级摇摆容器的秘密生活 - 图片来自如何使用根窗格

可能建议简单地添加对 super.paint(g)的调用,我不推荐它。

While some might suggest simple adding a call to super.paint(g), I would not recommend it.

更好的使用类似 JFrame 的玻璃窗格...这将允许您绘制框架内的组件。如果您需要在组件下绘画,请改为替换 JFrame 的内容窗格。

Better to use something like the JFrame's glass pane...This will allow you to paint over the components that reside within the frame. If you need to paint under the components, replace the JFrame's content pane instead.

您可能会发现。 ..

You may find...

  • Performing Custom Painting
  • Painting in AWT and Swing
  • How to Decorate Components with the JLayer Class

使用中......

更新

我想我开始看到你试图尝试的问题在文本区域执行自定义绘画。

I think I'm beginning to see you problem with trying to perform custom painting on a text area.

问题是, paintComponent 在一次犯规中描绘背景和文字猛扑,意味着你在调用 super.paintComponent 之前所做的任何绘画都将是r已经过了,但是在它上面完成的任何绘画都会在文本上绘画。

The problem is, paintComponent paints the background AND the text in one foul swoop, meaning that any painting you do before calling super.paintComponent will be rendered over, but any painting done over it will paint over the text.

您可以将文本区域设置为非不透明并自己绘制背景......

You could set the text area to be non-opaque and paint the background yourself...

public class CustomTextArea extends JTextArea {

    public CustomTextArea() {
        setOpaque(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.fillRect(0, 0, 100, 100);
        super.paintComponent(g);
    }

}

问题是,这很容易让人们休息不透明的水平,破坏你的工作。当然你可以覆盖 setOpaque getOpaque ,但是你怎么知道用户何时真正想要将组件设置为透明,你可以停止填补背景吗?

The problem is though, it's easy for people to rest the opaque level and destroy your work. Sure you could override the setOpaque or getOpaque, but how do you known when the user actually wants to set the component transparent, so you can stop fill the back ground?

这篇关于JFrame上的Swing图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆