问题
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?
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);
}
}
回答1:
Do not override the method paint()
of JFrame.
Override the method paintComponent()
of an element.
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);
}
}
回答2:
The paint(Graphics g)
function is the one which paint your component and it's content with invoking fowllowing three functions in the order they are appearing:
protected void paintComponent(Graphics g)
: this one paint your component, e,g.:backgroundprotected void paintBorder(Graphics g)
: this one paints the border of the componentprotected void paintChildren(Graphics g)
: This one paints the children of the component in it
So, anything you are painting by overriding the paint(Graphics g)
function, you should call these function as well with the order they appeared above inside the paint()
function. Calling super.paint(g)
will work, because it is calling the container's Super class(JComponent
class)'s paint()
function which is already invoking these three function.
But: why you are overriding this paint()
function for just custom painting! put your custom painting code by creating a custom component extending JComponent or JPanel
and implement paintComponent()
function by overriding it and don't forget to call super.paintComponent()
. If you need to have this custom component as frame's content pane: just set this pane frame.setContentPane(customPane)
;
Check out A Closer Look at the Paint Mechanism
回答3:
It's all explained in the javadoc:
public void paint(Graphics g)
Paints the container. This forwards the paint to any lightweight components that are children of this container. If this method is reimplemented, super.paint(g) should be called so that lightweight components are properly rendered.
回答4:
You should not use paint()
with JFrame
because it's better to use paintComponent()
on JPanel
then add it to JFrame
.
In the paintComponent()
call super.paintComponent()
to prepare the drawing surface to be painted.
来源:https://stackoverflow.com/questions/19889792/no-j-components-show-on-my-shape