/* Swingサンプル */ import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JScrollPane; import java.awt.Container; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.text.BadLocationException; class SSample14_1 extends JFrame{ JTextArea area; JTextField text; public static void main(String args[]){ SSample14_1 frame = new SSample14_1("タイトル"); frame.setVisible(true); } SSample14_1(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); area = new JTextArea(); area.setLineWrap(true); JScrollPane scrollpane = new JScrollPane(area); scrollpane.setPreferredSize(new Dimension(200, 120)); p.add(scrollpane); JPanel bottomp = new JPanel(); text = new JTextField(10); JButton button1 = new JButton("追加"); button1.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ area.append(text.getText()); } } ); JButton button2 = new JButton("挿入"); button2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ try{ area.insert(text.getText(), area.getLineStartOffset(1)); }catch(BadLocationException e){ System.out.println("Bad Location Error!"); } } } ); bottomp.add(text); bottomp.add(button1); bottomp.add(button2); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); contentPane.add(bottomp, BorderLayout.SOUTH); } }