|
■ swingコンポーネント->JViewport |
|
JViewportはビューの特定の領域を表示する「窓」を提供します。 ビューポートに表示されるビューの位置を操作して別の領域を表示させることもできます。 JViewportクラスは直接インスタンスを生成して使用することはあまりありませんが、Swingのスクロール機能の基本的なコンポーネントになります。 |
||||||||||||||
|
||||||||||||||
|
|
||||||||||||||
| コンストラクタ | ||||||||||||||
|
||||||||||||||
| メソッド(抜粋) | ||||||||||||||
|
||||||||||||||
|
|
||||||||||||||
| SampleView.java | ||||||||||||||
| import java.awt.*; | ||||||||||||||
| import java.awt.event.*; | ||||||||||||||
| import javax.swing.*; | ||||||||||||||
| public class SampleView extends JFrame implements ActionListener{ | ||||||||||||||
| Container contentPane; | ||||||||||||||
| String str[] = { "上","下","左","右"}; | ||||||||||||||
| JButton btn[] = new JButton[str.length]; | ||||||||||||||
| JPanel btnPnl = new JPanel(); | ||||||||||||||
| JLabel lbl = new JLabel(); | ||||||||||||||
| JViewport view = new JViewport(); | ← @インスタンス生成 | |||||||||||||
| public SampleView() { | ||||||||||||||
| super("SampleView"); | ||||||||||||||
| addWindowAdapter(new WindowListener() { | ||||||||||||||
| public void windowClosing(WindowEvent e) { | ||||||||||||||
| System.exit(0); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| contentPane = getContentPane(); | ||||||||||||||
| contentPane.setLayout(new BorderLayout()); | ||||||||||||||
| lbl = new JLabel(new ImageIcon("kozi.gif")); | ||||||||||||||
| view.setView(lbl); | ← Aビューの設定 | |||||||||||||
| btnPnl.setLayout(new FlowLayout()); | ||||||||||||||
| for(int i=0; i<str.length; i++){ | ||||||||||||||
| btn[i] = new JButton(str[i]); | ||||||||||||||
| btn[i].addActionListener(this); | ||||||||||||||
| btnPnl.add(btn[i]); | ||||||||||||||
| } | ||||||||||||||
| contentPane.add("Center",view); | ||||||||||||||
| contentPane.add("North",btnPnl); | ||||||||||||||
| setSize(250,250); | ||||||||||||||
| setVisible(true); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| public void actionPerformed(ActionEvent e){ | ||||||||||||||
| Object ojt = e.getSource(); | ||||||||||||||
| Point point = view.getViewPosition(); | ← B座標の取得 | |||||||||||||
| for(int i=0; i<str.length; i++){ | ||||||||||||||
| if(ojt==btn[0]){ | ||||||||||||||
| point.y += 5; | ← Cy座標の増加 | |||||||||||||
| } else if(ojt==btn[1]){ | ||||||||||||||
| point.y -= 5; | ← Cy座標の減少 | |||||||||||||
| } else if(ojt==btn[2]){ | ||||||||||||||
| point.x += 5; | ← Cx座標の増加 | |||||||||||||
| } else if(ojt==btn[3]){ | ||||||||||||||
| point.x -= 5; | ← Cx座標の減少 | |||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| view.setViewPosition(point); | ← D座標の設定 | |||||||||||||
| } | ||||||||||||||
| public static void main(String[] args) { | ||||||||||||||
| SampleView myClass = new SampleView(); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| @JViewport view = new
JViewport(); JViewportのインスタンスを生成しています。 |
||||||||||||||
| Aview.setView(lbl); ビューポートのビューにラベル(lbl)を設定しています。ラベルには画像が設定されています。 |
||||||||||||||
| BPoint
point = view.getViewPosition(); ビューの現在の位置(左上角の座標)を取得しています。 |
||||||||||||||
| Cpoint.y
+= 5; (座標の値の増加又は減少) Bで取得した座標の値を増加、又は減少させています。※
|
||||||||||||||
| Dview.setViewPosition(point); ビューの位置をpointの座標の位置に設定しています。 |
||||||||||||||
| ※swingの座標システムはコンポーネントの左上角を原点(0,0)としています。 X座標は右に向かって、Y座標は下に向かって大きくなります。 |
||||||||||||||
| もどる | ||||||||||||||