|
Os
arquivos abaixo foram extraídos do Livro: An Introduction to Graphical User Interfaces with Java Swing |
|
package
its.TextAnalysisGUI; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
TextAnalysisDriver { public static void main(String[] args) { TextAnalysisFrame taFrame = new
TextAnalysisFrame(); taFrame.showIt("Text
analysis"); } } |
|
package
its.TextAnalysisGUI; import
its.SimpleFrame.SimpleFrame; import java.awt.*; import
javax.swing.JButton; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
TextAnalysisFrame extends SimpleFrame { public TextAnalysisFrame() { this.setSize(300,150); TextAnalysisPanel taPanel = new
TextAnalysisPanel(); this.getContentPane().add(taPanel,BorderLayout.CENTER); JButton analyseButton = new
JButton("Analyse"); analyseButton.setBackground(Color.blue);
analyseButton.setForeground(Color.yellow); this.getContentPane().add(analyseButton,BorderLayout.SOUTH); TextAnalysisListener
taList = new TextAnalysisListener(taPanel);
analyseButton.addActionListener(taList); } } |
|
package
its.TextAnalysisGUI; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
TextAnalysisListener implements ActionListener { private TextAnalysisPanel taPanel; public
TextAnalysisListener(TextAnalysisPanel t) { taPanel = t; } public void actionPerformed(ActionEvent
evt) { taPanel.startAnalysisAndDisplayResult(); } } |
|
package
its.TextAnalysisGUI; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
TextAnalysisModel { private int totalNumberOfEs; private int currentNumberOfEs; private int totalNumberOfTexts; private String currentText; public TextAnalysisModel() { totalNumberOfEs = 0; totalNumberOfTexts = 0; currentText = ""; } public void analyse(String str){ currentText = str.toUpperCase(); currentNumberOfEs = 0; for (int i = 0; i <
currentText.length(); i++) { if(currentText.charAt(i) == 'E'){ currentNumberOfEs++; }//if }//for i totalNumberOfEs += currentNumberOfEs; totalNumberOfTexts++; }// analyse public int getCurrentNumberOfEs(){ return(currentNumberOfEs); } public String getCurrentText(){ return(currentText); } public int getTotalNumberOfEs(){ return(totalNumberOfEs); } public int getTotalNumberOfTexts(){ return(totalNumberOfTexts); } } |
|
package
its.TextAnalysisGUI; import java.awt.Color; import
java.awt.GridLayout; import javax.swing.JLabel; import javax.swing.JPanel; import
javax.swing.JTextField; /** * Example program for
Introduction to Swing * @author Paul Fischer, IMM, DTU * @version 1.0 */ public class
TextAnalysisPanel extends JPanel { private JLabel lastTextLabel; private JLabel numberOfEsLabel; private JLabel numberOfTextsLabel; private JTextField inputField; private TextAnalysisModel analysisModel; public TextAnalysisPanel() { analysisModel = new TextAnalysisModel(); this.setBackground(Color.yellow); this.setLayout(new
GridLayout(3,2,10,10)); JLabel questionLabel = new JLabel("Enter text:"); JLabel replyLabel = new JLabel("Current
text:"); JLabel numberTextLabel = new
JLabel("No. of Es in current text:"); lastTextLabel = new JLabel(""); numberOfEsLabel = new JLabel("--"); inputField = new JTextField(""); questionLabel.setOpaque(true); questionLabel.setBackground(Color.black); questionLabel.setForeground(Color.white); replyLabel.setOpaque(true); replyLabel.setBackground(Color.black); replyLabel.setForeground(Color.white); numberTextLabel.setOpaque(true); numberTextLabel.setBackground(Color.black);
numberTextLabel.setForeground(Color.white); numberOfEsLabel.setOpaque(true); numberOfEsLabel.setBackground(Color.red); numberOfEsLabel.setForeground(Color.white); lastTextLabel.setOpaque(true); lastTextLabel.setBackground(Color.red); lastTextLabel.setForeground(Color.white); this.add(questionLabel); this.add(inputField); this.add(replyLabel); this.add(lastTextLabel); this.add(numberTextLabel); this.add(numberOfEsLabel); } public void startAnalysisAndDisplayResult() { String text = inputField.getText(); analysisModel.analyse(text);
lastTextLabel.setText(analysisModel.getCurrentText()); int noOfEs =
analysisModel.getCurrentNumberOfEs();
numberOfEsLabel.setText(Integer.toString(noOfEs)); inputField.setText(""); } } |
esboço:
