用JLabel修复JScrollPane [英] fixing JScrollPane with JLabel
问题描述
为了添加背景图像,我使用JLabels而不是JPanel.
In order to add a background Image , I work on JLabels instead of JPanel .
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label=new JLabel(new ImageIcon("bg.png"));
label.setLayout(new BoxLayout(label, BoxLayout.Y_AXIS));
for(int i=0;i<20;i++)
label.add(new JLabel(i.toString()));
frame.add(label);
这里的问题是仅显示适合背景图像的组件(在我的示例中,仅显示前10个JLabel)
the problem here is that only components that fit the background image will be shown (in my example only the 10 first JLabels)
我尝试使用JScrollPane
I tried using a JScrollPane
JScrollPane scroll=new JScrollPane(l);
for(int i=0;i<20;i++)
label.add(new JLabel(i.toString()));
frame.add(scroll);
但是它并没有太大变化,所以我尝试使用JPanel
but it didn't change much , so I tried using a JPanel
JPanel panel=new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JScrollPane scroll=new JScrollPane(panel);
for(int i=0;i<20;i++)
panel.add(new JLabel(i.toString()));
label.add(scroll);
frame.add(label);
所以我让JScrollPane正常工作,但是JPanel覆盖了背景图像
so I got the JScrollPane working properly but the JPanel has covered the background image
我该怎么办?
这是将JLabel添加到标签的结果:
Here's the result to adding JLabels to label:
将它们添加到滚动条后的结果:
The result after adding them to the scroll:
在将setOpaque(false)
添加到JPanel
之后:
推荐答案
您可以将图像添加为JPanel
的paintComponent()
方法的一部分,然后在其上方添加标签,然后将该窗格放在JScrollPane
,最后将其添加到JFrame
,例如:
You can add the image as part of the JPanel
's paintComponent()
method, then add the labels above it, then put that pane inside a JScrollPane
and finally add it to the JFrame
, for example:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class PanelWithBackgroundImage {
private JFrame frame;
private JPanel pane;
private Image img;
private JScrollPane scroll;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PanelWithBackgroundImage().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame("JFrame with bg example");
try {
img = ImageIO.read(new FileInputStream("/home/jesus/Pictures/tokyo.jpg"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
};
pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
for (int i = 0; i < 20; i++) {
pane.add(new JLabel("label " + i));
}
scroll = new JScrollPane(pane);
frame.add(scroll);
frame.setSize(frame.getPreferredSize());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
哪个给出以下输出:
如果要使JLabels
居中,则可以使用以下方式更改for
循环:
If you want the JLabels
centered, you can change the for
loop with:
for (int i = 0; i < 20; i++) {
JLabel label = new JLabel("label " + i);
label.setAlignmentX(Component.CENTER_ALIGNMENT);
pane.add(label);
}
应该看起来像这样:
您可以复制粘贴上面的代码,它应该可以工作,只需更改图像路径,该代码称为最小,完整和可验证示例(MCVE),下一次,请发布一个示例,该示例演示您尝试过的内容,不仅是部分内容,而且您会获得更多,更好和更快的答案.帮助我们来帮助您! :)
You can copy-paste the code above, it should work, just change the image path, and that code, is called a Minimal, Complete and Verifiable Example (MCVE), next time, please post one which demonstrates what you have tried, not only parts of it, this way you'll get more, better and faster answers. Help us to help you! :)
这篇关于用JLabel修复JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!