我试图从一个单独的类中将JScrollPane添加到JPanel。并且感谢迄今为止提出的一些问题,我可以帮助自己创建它们。但是我的问题仍然有点特别。 我想在JPanel上显示图像,如果图像对于面板来说很大,我想添加滚动条。但滚动条不会出现。 (当我将ScrollPaneConstants
设置为****_SCROLLBAR_ALWAYS
时,显示条的框架,但没有滚动条)。将JScrollPane添加到JPanel
我想我必须连接图像与酒吧,让他们出现?
我的一些代码片段:
主窗口
public class Deconvolutioner extends JFrame
{
Draw z;
Picturearea picturearea;
class Draw extends JPanel
{
public void paint(Graphics g)
{
}
}
public Deconvolutioner()
{
setTitle("Deconvolutioner");
setLocation(30,1);
setSize(1300,730);
super.setFont(new Font("Arial",Font.BOLD,11));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
this.setLayout(flow);
picturearea = new Picturearea();
picturearea.setLayout(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane(picturearea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(1000, 664));
getContentPane().add(scrollPane, flow); // add scrollpane to frame
add(z = new Draw());
setVisible(true);
}
}
JPanel类
public class Picturearea extends JPanel
{
BufferedImage image;
int panelWidth, panelHeight, imageWidth, imageHeight;
public Picturearea()
{
setBackground(new Color(210,210,210));
setBorder(LineBorder.createBlackLineBorder());
setVisible(true);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
public void setPicture(BufferedImage picture)
{
try
{
image = picture;
}
catch (Exception e)
{
System.err.println("Some IOException accured (did you set the right path?): ");
System.err.println(e.getMessage());
}
repaint();
}
}
感谢您的时间。
感谢您的信息。 但我必须对这些图片进行数学计算。 我想包括一些过滤器,必须像这样的像素和东西。所以我想一个标签不会足够。 – Kyrill
JPanel只包含图形返回零维,覆盖getPreferredSize(在这里搜索,也许每天都会提到) – mKorbel
setBackground(new Color(210,210,210));和setVisible(true);和repaint();是无用的代码行, – mKorbel