秋千中的可滚动面板 [英] scrollable panel in swing
问题描述
将滚动窗格添加到我的应用程序时出现问题.我的应用程序是这样的: 1.在启动面板1(黄色)上初始化 2.按"potvrdit" panel2后(红色的一个被初始化) 3.将一组面板(绿色的一组)添加到panel2
I have a problem when adding a scroll pane to my app. My app works like this: 1. At start panel1(yellow one) is initialized 2. After I press "potvrdit" panel2(red one is initialized) 3. Set of panels(green one) are added to panel2
问题是当set(绿色面板)的数量对于屏幕太大时,我无法添加滚动条.如您所见,它只是在右侧添加了细线.
The problem is that I cant add a scrollbar when the number of set(green panels) is too big for screen. It just added thin line on the right side as you can see.
这是我的源代码的一部分:
This is part of my source code:
JFrame jframe = new JFrame("Etiket print.");
JPanel panel0 = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollFrame = new JScrollPane(panel2);
jframe.setMinimumSize(new Dimension(1280, 1000));
GridBagConstraints c = new GridBagConstraints();
panel0.setLayout(new GridBagLayout());
panel1.setLayout(new GridBagLayout());
panel2.setLayout(new GridBagLayout());
panel2.setAutoscrolls(true);
panel3.setLayout(new GridBagLayout());
panel4.setLayout(new GridBagLayout());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JPanel container1 = new JPanel();
JPanel container2 = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));
container2.setLayout(new BoxLayout(container2, BoxLayout.X_AXIS));
container1.add(panel0);
container1.add(panel1);
container2.add(container1);
container2.add(panel3);
container2.add(panel4);
container.add(container2);
container.add(panel2);
jframe.add(scrollFrame);
jframe.add(container, BorderLayout.NORTH);
jframe.pack();
jframe.setVisible(true);
if (e.getActionCommand().equals("Potvrd")||e.getActionCommand().equals("myTextField")){
temporaryBlockedSubharnesses.clear();
panel2.removeAll();
harnessPanelList.clear();
GridBagConstraints c = new GridBagConstraints();
for (int i=0; i<20; i++){
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = i;
JPanel hp= new JPanel();
hp.setMinimumSize(new Dimension(80, 70));
hp.setMaximumSize(new Dimension(80, 70));
hp.setPreferredSize(new Dimension(80, 70));
if(i%2==0) {
hp.setBackground(myBlue1Color);
}
else {
hp.setBackground(myBlue2Color);
}
panel2.add(hp, c);
hp.repaint();
hp.validate();
}
panel2.repaint();
panel2.validate();
jframe.repaint();
}
推荐答案
而不是JPanel
,请尝试使用swingx库中的JXPanel
.它实现了Scrollable
,因此它支持滚动.
Instead of JPanel
try JXPanel
from swingx library. It implements Scrollable
so it support scrolling.
这篇关于秋千中的可滚动面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!