当我将一个JScrollPane添加到JPanel时,JPanel中的所有按钮都会消失,它会变空。我正在关注此帖here以及其他类似帖子。
以下是没有JScrollPane的代码:
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
}
/* Slide Images */
.slide-img {
width: 100%;
}
.dot-control {
text-align: center;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0.8);
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* The dots/bullets/indicators */
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
给出了:
使用JScrollPane的代码,最后一行替换为:
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.setBounds(5, 5, 286, 573);
contentPane.add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel_1.setBounds(12, 53, 262, 484);
panel.add(panel_1);
panel_1.setLayout(null);
//this panel is the one the scrollPane is being added to and contains the buttons
JPanel panel_2 = new JPanel();
panel_2.setBounds(12, 68, 238, 403);
panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
for(int i = 0; i < 50; i++) {
JButton bttn = new JButton("TEST");
panel_2.add(bttn);
panel_2.revalidate();
}
panel_1.add(panel_2);
但是,所有按钮都会消失:
编辑:以下是完整代码:
JScrollPane scrollPane = new JScrollPane(panel_2);
panel_1.add(scrollPane);
//panel_1.add(panel_2);
这是整个应用程序的图像;结果选项卡下面的面板带有&#34; TEST&#34;按钮是滚动窗格的位置:
非常感谢任何帮助!
答案 0 :(得分:2)
你的约束有点奇怪:
sl_panel_1.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
sl_panel_1.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, panel_1);
sl_panel_1.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
sl_panel_1.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.WEST, panel_1);
改为,例如:
sl_panel_1.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.SOUTH, separator);
sl_panel_1.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, panel_1);
sl_panel_1.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.SOUTH, panel_1);
sl_panel_1.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, panel_1);
如果您添加了带有这些约束的panel_2且没有滚动窗格,则它也不会显示