//************************************************************* // Java application // 「StringChanger」 // //          作 成 者:ルート高菜    //          作成開始月:2006/5 //          最終更新月:2006/5 [TN076/J66] //************************************************************* import java.awt.*; import java.awt.event.*; import javax.swing.*; public class StringChanger extends JFrame { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // public static void main(String[] args) { StringChanger frame=new StringChanger(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } //コントロール // JTextArea area=new JTextArea("文字を入力"); // JCheckBox bold=new JCheckBox("bold"); JCheckBox italic=new JCheckBox("italic"); // ButtonGroup color=new ButtonGroup(); JRadioButton red=new JRadioButton("red"); JRadioButton black=new JRadioButton("black"); JRadioButton blue=new JRadioButton("blue"); // JComboBox size=new JComboBox(); JButton buttonSize=new JButton("OK"); // JTextField text=new JTextField(12); JButton buttonText=new JButton("OK"); //コンストラクタ public StringChanger() { // setTitle("StringChanger"); setSize(600,300); setResizable(false); //コンテナ枠 Container cp=getContentPane(); //中央テキスト Font tmpFont=area.getFont(); area.setFont(new Font(tmpFont.getFontName(),Font.PLAIN,20)); // JPanel panelCenter=new JPanel(); panelCenter.setBackground(Color.WHITE); panelCenter.add(area); cp.add(panelCenter,BorderLayout.CENTER); //下のパネル bold.setBackground(Color.CYAN); italic.setBackground(Color.CYAN); red.setBackground(Color.CYAN); black.setBackground(Color.CYAN); blue.setBackground(Color.CYAN); // color.add(red); color.add(black); color.add(blue); // JPanel panelSouth=new JPanel(); panelSouth.setBackground(Color.CYAN); panelSouth.add(new Label("修飾:")); panelSouth.add(bold); panelSouth.add(italic); panelSouth.add(new Label(" 色:")); panelSouth.add(red); panelSouth.add(black); panelSouth.add(blue); panelSouth.setBorder(BorderFactory.createLineBorder(Color.BLACK)); cp.add(panelSouth,BorderLayout.SOUTH); //上のパネル size.addItem("10"); size.addItem("20"); size.addItem("30"); size.addItem("40"); size.setEditable(true); // JPanel panelNorth=new JPanel(); panelNorth.setBackground(Color.ORANGE); panelNorth.add(new Label("文字列:")); panelNorth.add(text); panelNorth.add(buttonText); panelNorth.add(new Label(" サイズ:")); panelNorth.add(size); panelNorth.add(buttonSize); panelNorth.setBorder(BorderFactory.createLineBorder(Color.BLACK)); cp.add(panelNorth,BorderLayout.NORTH); //イベント処理 ControlAction btn=new ControlAction(); bold.addActionListener(btn); italic.addActionListener(btn); red.addActionListener(btn); black.addActionListener(btn); blue.addActionListener(btn); buttonSize.addActionListener(btn); buttonText.addActionListener(btn); } //イベント処理用の内部クラス(アクションリスナーを定義) class ControlAction implements ActionListener { public void actionPerformed(ActionEvent event){ Object object=event.getSource(); if(object==bold || object==italic){ int mode=0; if(bold.isSelected()){ mode+=Font.BOLD; } if(italic.isSelected()){ mode+=Font.ITALIC; } Font tmpFont=area.getFont(); area.setFont(new Font(tmpFont.getFontName(),mode,tmpFont.getSize())); }else if(object==red || object==black || object==blue){ if(red.isSelected()){ area.setForeground(Color.RED); }else if(black.isSelected()){ area.setForeground(Color.BLACK); }else if(blue.isSelected()){ area.setForeground(Color.BLUE); } }else if(object==buttonSize){ int tmpSize; try{ tmpSize=Integer.parseInt((String)size.getSelectedItem()); }catch(NumberFormatException e){ tmpSize=20; size.setSelectedItem("20"); } Font tmpFont=area.getFont(); area.setFont(new Font(tmpFont.getFontName(),tmpFont.getStyle(),tmpSize)); }else if(object==buttonText){ area.setText(text.getText()); } } } }