📄 scrollexample3.java
字号:
package JFCBook.Chapter5.jdk13;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollExample3 extends JFrame {
public ScrollExample3() {
super("Scroll Example 3");
JScrollPane p = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add(p);
labels = new JLabel[imageNames.length];
for (int i = 0; i < imageNames.length; i++) {
labels[i] = new JLabel(new ImageIcon(getClass().getResource(imageNames[i])));
labels[i].setHorizontalAlignment(SwingConstants.LEFT);
labels[i].setVerticalAlignment(SwingConstants.TOP);
}
JPanel pictures = new JPanel();
pictures.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 1;
c.gridheight = 1;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(4, 4, 4, 4);
c.fill = GridBagConstraints.BOTH;
for (int i = 0; i < labels.length; i++) {
pictures.add(labels[i], c);
c.gridx++;
if ((c.gridx % 3) == 0) {
c.gridx = 0;
c.gridy++;
}
}
p.setViewportView(pictures);
p.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
}
public static void main(String[] args) {
JFrame f = new ScrollExample3();
f.setSize(250, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private static final String[] imageNames = {
"images/p1.jpg", "images/p2.jpg", "images/p3.jpg",
"images/p4.jpg", "images/p5.jpg", "images/p6.jpg",
"images/p7.jpg", "images/p8.jpg", "images/p9.jpg" };
private JLabel[] labels;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -