我正在尝试创建一个包含JPanels的JScrollPane,这些JPanels包含来自JFreeChart API的图表。 GenerateChartPanel是一个返回JPanel中的图表的方法,因此我可以快速制作一堆。到目前为止,我主要有这个:
public static void main (String[] args){
JFrame main = new JFrame();
main.setLayout(new BorderLayout());
main.setPreferredSize(new Dimension(500,500));
//Creates specFrame
JScrollPane specScrollPane = new JScrollPane();
JPanel container = new JPanel();
container.setLayout(new GridLayout());
container.add(generateSpecPanelFromSmiles("C"));
container.add(generateSpecPanelFromSmiles("CC"));
container.add(generateSpecPanelFromSmiles("CCC"));
container.setVisible(true);
specScrollPane.setVisible(true);
specScrollPane.add(container);
main.add(specScrollPane);
RefineryUtilities.centerFrameOnScreen(main);
main.setVisible(true);
当我运行时,没有任何显示。显然你不能打包,setCloseOp,setLayout或居中这些窗格所以我评论了它们。我错过了什么?
编辑:获取维度
specPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
@Override
public void componentResized(ComponentEvent event) {
specPanel.setSize(Math.min(specPanel.getPreferredSize().width, specPanel.getWidth()),
Math.min(specPanel.getPreferredSize().height, specPanel.getHeight()));
}
});
感谢
答案 0 :(得分:4)
JScrollPane被指定只嵌套一个JComponent
您必须将另一个JPanel添加到JScrollPane
,此JPanel
您可以添加三个组件
必须更改父JPanel
的默认Layout Manager (FlowLayout)(包含三个组件),哪一个依赖于这些组件,它们如何返回其PreferredSize,可能以{{3}开头}
修改
无论generateSpecPanelFromSmiles
是什么,必须将PreferredSize
返回parentPanel
,例如(使用PreferredSize
GridLayout
和BoxLayout
进行游戏 - 接受最小,最大和首选尺寸的框
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JPanelsInsideJScrollPane {
private JFrame frame = new JFrame("JPanels Inside JScrollPane");
private JScrollPane scrollPane = new JScrollPane();
private JPanel parentPanel, childOne, childTwo, childThree;
private JButton button = new JButton("Change, Switch Layout Manager to BoxLayout");
public JPanelsInsideJScrollPane() {
parentPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(new Dimension(800, 600));
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
};
parentPanel.setLayout(new GridLayout(0, 1));
childOne = new JPanel() {
@Override
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(500, 300);
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
};
childTwo = new JPanel() {
@Override
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(600, 400);
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
};
childThree = new JPanel() {
@Override
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(800, 600);
}
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.ORANGE);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
};
parentPanel.add(childOne);
parentPanel.add(childTwo);
parentPanel.add(childThree);
scrollPane.setViewportView(parentPanel);
scrollPane.getVerticalScrollBar().setUnitIncrement(30);
scrollPane.getHorizontalScrollBar().setUnitIncrement(30);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LayoutManager manager = parentPanel.getLayout();
if ((manager != null) && (manager instanceof BoxLayout)) {
parentPanel.setLayout(new GridLayout(0, 1));
button.setText("Change, Switch Layout Manager to BoxLayout");
} else if ((manager != null) && (manager instanceof GridLayout)) {
parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.PAGE_AXIS));
button.setText("Change, Switch Layout Manager to GridLayout");
}
parentPanel.revalidate();
parentPanel.repaint();
}
});
frame.add(scrollPane);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JPanelsInsideJScrollPane();
}
});
}
}