I have a panel, and I am trying to have a background image, and two sets of text, a "title" and text area to add text to later how do I get the text area to go on top of the image (label) or if there is a completely better way to achieve this?
` //For Text (right)
JPanel textPanel = new JPanel();
//textPanel.setLayout(); //I don't understand layouts I tried some non really got what I needed
textPanel.setBorder(border);
textPanel.setBounds(190, 0, 1300, 886);
textPanel.setBackground(Color.BLACK);
//Imports For inventory Label
JLabel textLabel = new JLabel(); //Title label
arrayTXTA = new JTextArea(0,1);//The text I will add later label
JLabel textLabelbg = new JLabel(); //Background image label
ImageIcon Teximage =new ImageIcon("src\\emporium\\pkg\\background.jpg");//sets up image for background
//Code for background image
textLabelbg.setIcon(Teximage);//adds an image
textLabelbg.setBounds(70, 5, 1200, 886);
//TEXT AT 253
//Code for title
textLabel.setHorizontalTextPosition(JLabel.CENTER); icon
textLabel.setVerticalTextPosition(JLabel.TOP);
textLabel.setForeground(new Color(0,255,0));
textLabel.setFont(new Font("Blackadder ITC",Font.BOLD,30));
//textLabel.setBounds(300, 0, 100, 36);
//Code for Text I will add later
arrayTXTA.setVisible(true);
arrayTXTA.append("Words");
arrayTXTA.append("Words");
arrayTXTA.append("Words");
arrayTXTA.setForeground(new Color(255,0,0));
arrayTXTA.setFont(new Font("Blackadder ITC",Font.BOLD,30));
arrayTXTA.setBackground(new Color(0, 0, 0, 0));
arrayTXTA.setBounds(25, 0, 0, 0);
`
Here is a picture of what I am trying to achieve
Thank you in advance
Don't use null
layouts, they aren't helping you. Take the time to learn and use the available layout managers, see Laying Out Components Within a Container for more details.
JLabel
is a bad choice to trying to make background image. There's a few reasons, but lets just say they aren't really designed for the job
Start with a custom component and paint the background you want via it. Use it as the primary container for the JTextArea
.
Configure the JScrollPane
and JTextArea
to be transparent and you will see the background through it
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
private BufferedImage background;
public TestPane() throws IOException {
background = ImageIO.read(getClass().getResource("/images/Paper.png"));
setLayout(new BorderLayout());
JTextArea ta = new JTextArea("This is some text");
ta.setOpaque(false);
JScrollPane sp = new JScrollPane(ta);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);
add(sp);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(background, 0, 0, this);
g2d.dispose();
}
}
}
As to the rest of you layout. Simple, make use of a number of containers/panels with their own layout managers and then bind the all together into a "master" panel with a suitable layout manager ( BorderLayout
looks like it would work)
Depending on your needs, I might consider having some more logic in the paintComponent
method to allow it paint more instances of the background if the container was resized beyond the size of the base background image, but that would depend on the image and your needs.
While more difficult, you could look at painting the background directly to the JTextArea
(by overriding it), this would allow the background to scroll with the JTextArea
, but would also require you to consider the point above
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.