1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class MainFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = -7204686009086977692L;
private JScrollPane pane;
private JLabel label;
private JTextField field;
public MainFrame() {
super();
this.init();
}
@Override
public void actionPerformed(ActionEvent e) {
try {
int value = Integer.parseInt(this.field.getText());
this.pane.getVerticalScrollBar().setValue(value);
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
private void init() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(500, 400);
this.setTitle("Test scrollbar");
ImageIcon icon = new ImageIcon("C:/test/image.jpg");
this.label = new JLabel(icon);
this.label.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
this.pane = new JScrollPane(this.label);
this.add(this.pane, BorderLayout.CENTER);
this.field = new JTextField();
this.field.addActionListener(this);
this.add(this.field, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame frame = new MainFrame();
frame.setVisible(true);
}
} |
Partager