0
eclipseにて。フレームにスクロール機能をつけたい。
はじめまして。javaは勉強し始めたばかりです。
eclipseを使って、「ボタンをクリックするとファイルが開く」プログラムを作っています。
まずフレームを作り、そこにラベルとボタンを配置して、ボタンのアクションを設定するという手順でやってきましたが、ここにきてスクロール機能を付けたくなりました。
(ボタンが増えて表示しきれなくなるのを考慮)
JScrollPaneをフレームに配置しようとしましたが、他のボタンとかと同じように、ただそこに表示されるだけでうまくいきません。
どのようにすればよいのでしょうか?教えてください。
コードは以下のようになっております。
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class fileFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel1 = null;
private JLabel jLabel11 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
public JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("ボタン【1】");
jButton.setSize(new Dimension(200, 35));
jButton.setLocation(new Point(20, 55));
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
File fn = new File("d:/エクセルファイル1.xls");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(fn);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setLocation(new Point(20, 100));
jButton1.setText("ボタン【2】");
jButton1.setSize(new Dimension(200, 35));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
File fn = new File("d:/エクセルファイル2.xls");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(fn);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
return jButton1;
}
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setLocation(new Point(230, 100));
jButton2.setText("ボタン【3】");
jButton2.setSize(new Dimension(200, 35));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
File fn = new File("d:/エクセルファイル3.xls");
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(fn);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
return jButton2;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
fileFrame thisClass = new fileFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public fileFrame() {
super();
initialize();
}
private void initialize() {
this.setSize(500, 400);
this.setContentPane(getJContentPane());
this.setFont(new Font("Dialog", Font.PLAIN, 24));
this.setTitle("ファイル管理");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel11 = new JLabel();
jLabel11.setBounds(new Rectangle(21, 158, 76, 24));
jLabel11.setText("見出し1");
jLabel11.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(18, 18, 76, 24));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1.setText("見出し2");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel1, null);
jContentPane.add(jLabel11, null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
}
return jContentPane;
}
}