/***************************************** 展示JTextArea *************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ui24 extends JFrame implements ActionListener { Container c; JButton bot1,bot2,bot0; ImageIcon icon1,icon2; JTextField txt1,txt2; JPasswordField pwd1; JTextArea tea; JLabel lab; String[] show=new String[3]; public ui24() { super("多行文字區展示"); int i; c=getContentPane(); icon1 = new ImageIcon("bridge/01.GIF"); icon2 = new ImageIcon("bridge/52.GIF"); c.setLayout(new FlowLayout(FlowLayout.CENTER)); bot1=new JButton("加",icon1); bot2=new JButton("減",icon2); tea=new JTextArea(20,20); bot1.setFont(new Font("Serif",Font.PLAIN,20));//可以設定文字 tea.setFont(new Font("Serif",Font.PLAIN,10));//設定文字 bot1.setPressedIcon(icon1);//繼承自AbstractButton,設定壓下去的icon bot0=new JButton("結束"); txt1=new JTextField("0",5);//內定文字與長度 txt2=new JTextField("答案",5);//內定文字與長度 pwd1=new JPasswordField(5); pwd1.setEchoChar('圈');//可以設定按下去的顯示文字 show[0]=new String("這是招牌一"); show[1]=new String("這是招牌二"); show[2]=new String("這是招牌三"); c.add(bot0); c.add(bot1); c.add(bot2); c.add(txt1); c.add(pwd1); c.add(txt2); c.add(tea); bot0.addActionListener(this); bot1.addActionListener(this); bot2.addActionListener(this); txt1.addActionListener(this); pwd1.addActionListener(this); setSize(400,500); setVisible(true); } public void paint(Graphics g) { super.paint(g); } /***按鈕事件的傾聽方法****/ public void actionPerformed(ActionEvent e) //按鈕事件的處理方法 { double a,b,c; if (e.getSource()==bot1) { a=Double.parseDouble(txt1.getText()); b=Double.parseDouble(new String(pwd1.getPassword())); c=a+b; txt2.setText(Double.toString(c)); tea.append(a+"+"+b+"="+c+"\n"); } else if (e.getSource()==bot2) { a=Double.parseDouble(txt1.getText()); b=Double.parseDouble(new String(pwd1.getPassword())); c=a-b; txt2.setText(Double.toString(c)); tea.append(a+"-"+b+"="+c+"\n"); } else if (e.getSource()==txt1) { txt2.setText(txt1.getText()); } else if (e.getSource()==pwd1) { txt2.setText(new String(pwd1.getPassword())); } else if (e.getSource()==bot0) { System.exit(0); } a=Double.parseDouble(txt2.getText()); if (a==3) txt2.setEditable(false);//設定成不能修改 repaint(); } public static void main(String args[]) //程式起點 { ui24 app=new ui24(); //畫圖 //處理視窗關閉要求 app.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} }); } }