我的形状上没有显示J分量? [英] No J components show on my shape?

查看:41
本文介绍了我的形状上没有显示J分量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常大的问题,即当我创建一个JLabel,Jbutton等时,它就可以在屏幕上显示,但是当我要将它们放置在一个矩形上时,它消失了,矩形仅显示?

I'm having a really big issue in which when ether I create a JLabel, Jbutton and so on....it works in terms of showing on screen however when I want to place them on a rectangle it disappears and the rectangle only shows?

在使用JLabel时,我选择改用束线,但现在我一直想尝试启用JTextField.我不知道我在想什么.

With JLabel I opted to use drawstring instead but now I'm stuck with trying to get JTextField on. I don't know what I am missing.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.event.*;

class main
{   
    public static void main (String Args [])
    {
        GUIwindow guiW = new GUIwindow();
    }
}

class GUIwindow extends JFrame
{
    JPanel grid = new JPanel();
    JTextArea screenArea = new JTextArea("", 10, 20);
    JScrollPane scrollBar = new JScrollPane(screenArea);

    GUIwindow()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,800);
        setTitle("Title here");
        setLocationRelativeTo(null);

        screenArea.setLineWrap(true);
        screenArea.setEditable(false);


        grid.add(scrollBar);
        add(grid);
        setVisible(true);
    }

    public void paint (Graphics g)
    {
        g.setColor(Color.decode("#0232ac"));
        g.fillRoundRect(100, 50, 300, 600, 50, 50);
        g.setColor(Color.white);
        g.drawString("TitleonRect", 220, 80);
    }  

}

推荐答案

请勿覆盖JFrame的方法paint().

Do not override the method paint() of JFrame.

覆盖元素的方法paintComponent().

如果您继承了JPanel,则可以覆盖其paintComponent方法:

If you subclass JPanel, you can override its paintComponent method:

class GridPanel extends JPanel {
    GridPanel() {
        super();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.decode("#0232ac"));
        g.fillRoundRect(100, 50, 300, 600, 50, 50);
        g.setColor(Color.white);
        g.drawString("TitleonRect", 220, 80);
    }
}

这篇关于我的形状上没有显示J分量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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