import java.awt.*; import javax.swing.*; public class Ex12_1 extends JPanel { JButton bt1 = new JButton("start!"); // GUI部品(Button)を生成し,インスタンス変数に代入 JButton bt2 = new JButton("end"); JTextField txt = new JTextField(); // GUI部品(TextBox)を生成し,インスタンス変数に代入 JLabel lb1 = new JLabel("好きな町 選択"); JCheckBox cb1 = new JCheckBox("茅ヶ崎",true); // true指定 = チェック済み JCheckBox cb2 = new JCheckBox("寒川"); JCheckBox cb3 = new JCheckBox("藤沢"); JLabel lb2 = new JLabel("所属学部 選択"); JRadioButton rb1 = new JRadioButton("情報学部"); JRadioButton rb2 = new JRadioButton("国際学部"); JRadioButton rb3 = new JRadioButton("経営学部", true); // true指定 = チェック済 ButtonGroup grp = new ButtonGroup(); // 1個だけ選択可にするためグループ化する用 JLabel lb3 = new JLabel("本籍地 選択"); JComboBox cmb = new JComboBox(new String[]{"埼玉県","千葉県","東京都","神奈川県"}); JLabel lb4 = new JLabel("情報端末 選択"); JList lst = new JList(new String[]{"スマホ","ガラケー","タブレット","もたない"}); JScrollPane scp = new JScrollPane(lst); // GUI部品(ListBox)にScrollバーを追加 JSlider sld = new JSlider(-60, 60, 0); // GUI(Slider) 範囲-90〜90で,位値0にスライドレバー配置 JTextArea mem = new JTextArea("長い文章を書くことが出来る領域だよ"); public Ex12_1() { // コンストラクタ setLayout(null); // GUI部品の自動配置機能をOFFに add(bt1); // GUI部品(btn1)を追加 bt1.setBounds(50, 50, 120, 30); // 位値(50,50)に幅120,高さ30で設定 add(bt2); bt2.setBounds(200, 50, 120, 30); // Button (200,50) w120,h30 add(txt); txt.setBounds(50, 100, 270, 30); // TextBox (50,100) w270,h30 add(lb1); lb1.setBounds(30, 150, 90, 25); // Label (30,150) w90,h25 add(cb1); cb1.setBounds(30, 175, 90, 25); // CheckBox (30,175) w90,h25 add(cb2); cb2.setBounds(30, 200, 90, 25); add(cb3); cb3.setBounds(30, 225, 90, 25); add(lb2); lb2.setBounds(130, 150, 90, 25); // Label (130,150) w90,h25 grp.add(rb1); grp.add(rb2); grp.add(rb3); // ラジオボタンをグループ化 add(rb1); rb1.setBounds(130, 175, 90, 25); // RadioButton (130,175) w90,h25 add(rb2); rb2.setBounds(130, 200, 90, 25); // RadioButton (130,200) w90,h25 add(rb3); rb3.setBounds(130, 225, 90, 25); // RadioButton (130,225) w90,h25 add(lb3); lb3.setBounds(240, 150, 90, 25); // Label (240,150) w90,h25 add(cmb); cmb.setBounds(240, 175, 90, 25); // ComboBox (240,175) w90,h25 add(lb4); lb4.setBounds(240, 200, 90, 25); // Label (240,200) w90,h25 add(scp); scp.setBounds(240, 225, 90, 60); // ListBox(lst)でなくScroll(scp)を追加する sld.createStandardLabels(20); // 20毎にメモリ・ラベルをつくる sld.setPaintLabels(true); // 作ったメモリ・ラベルを視覚化 sld.setMajorTickSpacing(20); // 主メモリ線を20毎につくる sld.setMinorTickSpacing(10); // 副メモリ線を10毎につくる sld.setPaintTicks(true); // 作ったメモリ線を視覚化 add(sld); sld.setBounds(50, 300, 250, 50); // Slider (50,300) w250,h50 add(mem); mem.setBounds(50, 360, 270, 70); // TextArea (50,360) w270,h70 } public static void main(String[] args) { JFrame app = new JFrame(); app.add(new Ex12_1()); app.setSize(400, 500); app.setTitle("GUI部品"); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }