Professional Eclipse 3 for Java Developers 2006 phần 3 pdf

61 246 0
Professional Eclipse 3 for Java Developers 2006 phần 3 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ContentPane To make the content pane visible to the application, declare the getContentPane() method as public. In addition, insert the initialization of the sliders into this method. Also, you can now insert all setLabelFor() methods, because after the execution of the method body, you can be sure that all tar- get objects of setLabelFor() exist. public javax.swing.JPanel getContentPane() { if(contentPane == null) { contentPane = new javax.swing.JPanel(); contentPane.setLayout(new java.awt.BorderLayout()); contentPane.add(getControlAreaPane(), java.awt.BorderLayout.CENTER); contentPane.add(getTextAndButtonPanel(), java.awt.BorderLayout.SOUTH); contentPane.setSize(new java.awt.Dimension(600,500)); volumeLabel.setLabelFor(volumeSlider); speedLabel.setLabelFor(speedSlider); pitchLabel.setLabelFor(pitchSlider); rangeLabel.setLabelFor(rangeSlider); textAreaLabel.setLabelFor(textInputArea); updateSliders(); } return contentPane; } The updateSliders() method is defined as follows: private void updateSliders() { // Volume int volume = (int) playerModel.getVolume(); if (volume >= 0) volumeSlider.setValue(volume); // Speed int rate = (int) playerModel.getSpeakingRate(); if (rate >= 0) speedSlider.setValue(rate); // Pitch int pitch = (int) playerModel.getPitch(); if (pitch >= 0) pitchSlider.setValue(pitch); // Variation int range = (int) playerModel.getRange(); if (range >= 0) rangeSlider.setValue(range); } Listing 5.7 contains the complete source code of the PlayerPanel class as generated by the Visual Editor, with the necessary source code modifications applied. 96 Chapter 5 07_020059_ch05.qxd 10/8/04 10:52 AM Page 96 package com.bdaum.dukeSpeaks; import java.awt.GridLayout; import javax.swing.JPanel; public class PlayerPanel { // The data model private PlayerModel playerModel; // The JPanel instance for the face private JPanel face; private javax.swing.JPanel contentPane = null; private javax.swing.JPanel controlAreaPane = null; private javax.swing.JPanel leftSlidersPanel = null; private javax.swing.JPanel centerPanel = null; private javax.swing.JPanel rightSlidersPanel = null; private javax.swing.JPanel volumePanel = null; private javax.swing.JLabel volumeLabel = null; private javax.swing.JSlider volumeSlider = null; private javax.swing.JPanel speedPanel = null; private javax.swing.JLabel speedLabel = null; private javax.swing.JSlider speedSlider = null; private javax.swing.JPanel pitchPanel = null; private javax.swing.JLabel pitchLabel = null; private javax.swing.JSlider pitchSlider = null; private javax.swing.JPanel rangePanel = null; private javax.swing.JLabel rangeLabel = null; private javax.swing.JSlider rangeSlider = null; private javax.swing.JPanel textAndButtonPanel = null; private javax.swing.JLabel textAreaLabel = null; private javax.swing.JPanel buttonPanel = null; private javax.swing.JButton speakButton = null; private javax.swing.JButton deleteButton = null; private javax.swing.JTextArea textInputArea = null; private javax.swing.JLabel jLabel5 = null; private javax.swing.JLabel jLabel6 = null; /** * */ public PlayerPanel(PlayerModel playerModel, JPanel face) { super(); // Save parameters into fields this.playerModel = playerModel; this.face = face; } /** * Method updateSliders. * updates all the sliders with values from the PlayerModel. */ private void updateSliders() { // Volume 97 Project One: Duke Speaks Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 97 int volume = (int) playerModel.getVolume(); if (volume >= 0) volumeSlider.setValue(volume); // Speed int rate = (int) playerModel.getSpeakingRate(); if (rate >= 0) speedSlider.setValue(rate); // Pitch int pitch = (int) playerModel.getPitch(); if (pitch >= 0) pitchSlider.setValue(pitch); // Variation int range = (int) playerModel.getRange(); if (range >= 0) rangeSlider.setValue(range); } public javax.swing.JPanel getContentPane() { if(contentPane == null) { contentPane = new javax.swing.JPanel(); contentPane.setLayout(new java.awt.BorderLayout()); contentPane.add(getControlAreaPane(), java.awt.BorderLayout.CENTER); contentPane.add(getTextAndButtonPanel(), java.awt.BorderLayout.SOUTH); contentPane.setSize(new java.awt.Dimension(600,500)); volumeLabel.setLabelFor(volumeSlider); speedLabel.setLabelFor(speedSlider); pitchLabel.setLabelFor(pitchSlider); rangeLabel.setLabelFor(rangeSlider); textAreaLabel.setLabelFor(textInputArea); updateSliders(); } return contentPane; } /** * This method initializes controlAreaPane * * @return javax.swing.JPanel */ private javax.swing.JPanel getControlAreaPane() { if(controlAreaPane == null) { controlAreaPane = new javax.swing.JPanel(); controlAreaPane.setLayout(new GridLayout(1, 3)); controlAreaPane.add(getLeftSlidersPanel()); controlAreaPane.add(getCenterPanel()); controlAreaPane.add(getRightSlidersPanel()); } return controlAreaPane; } /** * This method initializes leftSlidersPanel * * @return javax.swing.JPanel 98 Chapter 5 Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 98 */ private javax.swing.JPanel getLeftSlidersPanel() { if(leftSlidersPanel == null) { leftSlidersPanel = new javax.swing.JPanel(); leftSlidersPanel.setLayout(new java.awt.GridLayout(1,3)); leftSlidersPanel.add(getVolumePanel()); leftSlidersPanel.add(getSpeedPanel()); leftSlidersPanel.add(getJLabel5(), null); } return leftSlidersPanel; } /** * This method initializes centerPanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getCenterPanel() { if(centerPanel == null) { centerPanel = new javax.swing.JPanel(); centerPanel.setLayout(new java.awt.GridLayout(1,1)); centerPanel.add(face); } return centerPanel; } /** * This method initializes rightSlidersPanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getRightSlidersPanel() { if(rightSlidersPanel == null) { rightSlidersPanel = new javax.swing.JPanel(); java.awt.GridLayout layGridLayout1 = new java.awt.GridLayout(1, 2); layGridLayout1.setColumns(3); rightSlidersPanel.setLayout(layGridLayout1); rightSlidersPanel.add(getJLabel6(), null); rightSlidersPanel.add(getPitchPanel()); rightSlidersPanel.add(getRangePanel()); } return rightSlidersPanel; } /** * This method initializes volumePanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getVolumePanel() { if(volumePanel == null) { volumePanel = new javax.swing.JPanel(); volumePanel.setLayout(new java.awt.BorderLayout()); volumePanel.add(getVolumeLabel(), java.awt.BorderLayout.NORTH); volumePanel.add(getVolumeSlider(), java.awt.BorderLayout.CENTER); 99 Project One: Duke Speaks Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 99 } return volumePanel; } /** * This method initializes volumeLabel * * @return javax.swing.JLabel */ private javax.swing.JLabel getVolumeLabel() { if(volumeLabel == null) { volumeLabel = new javax.swing.JLabel(); volumeLabel.setText("Volume"); volumeLabel.setHorizontalTextPosition( javax.swing.SwingConstants.CENTER); volumeLabel.setHorizontalAlignment( javax.swing.SwingConstants.CENTER); volumeLabel.setDisplayedMnemonic(java.awt.event.KeyEvent.VK_V); } return volumeLabel; } /** * This method initializes volumeSlider * * @return javax.swing.JSlider */ private javax.swing.JSlider getVolumeSlider() { if(volumeSlider == null) { volumeSlider = new javax.swing.JSlider(); volumeSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); volumeSlider.setMaximum(10); volumeSlider.setMinorTickSpacing(1); volumeSlider.setMajorTickSpacing(5); volumeSlider.setOrientation(javax.swing.JSlider.VERTICAL); volumeSlider.setToolTipText("Volume"); volumeSlider.setPaintLabels(true); volumeSlider.setPaintTicks(true); volumeSlider.addChangeListener( new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { playerModel.setVolume((float) volumeSlider.getValue()); } }); } return volumeSlider; } /** * This method initializes speedPanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getSpeedPanel() { if(speedPanel == null) { speedPanel = new javax.swing.JPanel(); 100 Chapter 5 Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 100 speedPanel.setLayout(new java.awt.BorderLayout()); speedPanel.add(getSpeedLabel(), java.awt.BorderLayout.NORTH); speedPanel.add(getSpeedSlider(), java.awt.BorderLayout.CENTER); } return speedPanel; } /** * This method initializes speedLabel * * @return javax.swing.JLabel */ private javax.swing.JLabel getSpeedLabel() { if(speedLabel == null) { speedLabel = new javax.swing.JLabel(); speedLabel.setText("Words/min"); speedLabel.setHorizontalAlignment( javax.swing.SwingConstants.CENTER); speedLabel.setHorizontalTextPosition( javax.swing.SwingConstants.CENTER); speedLabel.setDisplayedMnemonic(java.awt.event.KeyEvent.VK_W); } return speedLabel; } /** * This method initializes speedSlider * * @return javax.swing.JSlider */ private javax.swing.JSlider getSpeedSlider() { if(speedSlider == null) { speedSlider = new javax.swing.JSlider(); speedSlider.setOrientation(javax.swing.JSlider.VERTICAL); speedSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); speedSlider.setMaximum(400); speedSlider.setMinorTickSpacing(50); speedSlider.setMajorTickSpacing(100); speedSlider.setToolTipText("Speed"); speedSlider.setPaintLabels(true); speedSlider.setPaintTicks(true); speedSlider.setPaintTrack(true); speedSlider.addChangeListener( new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { playerModel.setSpeakingRate((float) speedSlider.getValue()); } }); } return speedSlider; } /** * This method initializes pitchPanel * * @return javax.swing.JPanel */ 101 Project One: Duke Speaks Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 101 private javax.swing.JPanel getPitchPanel() { if(pitchPanel == null) { pitchPanel = new javax.swing.JPanel(); pitchPanel.setLayout(new java.awt.BorderLayout()); pitchPanel.add(getPitchLabel(), java.awt.BorderLayout.NORTH); pitchPanel.add(getPitchSlider(), java.awt.BorderLayout.CENTER); } return pitchPanel; } /** * This method initializes pitchLabel * * @return javax.swing.JLabel */ private javax.swing.JLabel getPitchLabel() { if(pitchLabel == null) { pitchLabel = new javax.swing.JLabel(); pitchLabel.setText("Hz"); pitchLabel.setHorizontalAlignment( javax.swing.SwingConstants.CENTER); pitchLabel.setHorizontalTextPosition( javax.swing.SwingConstants.CENTER); pitchLabel.setDisplayedMnemonic(java.awt.event.KeyEvent.VK_H); } return pitchLabel; } /** * This method initializes pitchSlider * * @return javax.swing.JSlider */ private javax.swing.JSlider getPitchSlider() { if(pitchSlider == null) { pitchSlider = new javax.swing.JSlider(); pitchSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); pitchSlider.setOrientation(javax.swing.JSlider.VERTICAL); pitchSlider.setMinimum(50); pitchSlider.setMaximum(200); pitchSlider.setMinorTickSpacing(25); pitchSlider.setMajorTickSpacing(50); pitchSlider.setValue(50); pitchSlider.setToolTipText("Pitch"); pitchSlider.setPaintTicks(true); pitchSlider.addChangeListener( new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { playerModel.setPitch((float) pitchSlider.getValue()); } }); } return pitchSlider; } /** * This method initializes rangePanel 102 Chapter 5 Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 102 * * @return javax.swing.JPanel */ private javax.swing.JPanel getRangePanel() { if(rangePanel == null) { rangePanel = new javax.swing.JPanel(); rangePanel.setLayout(new java.awt.BorderLayout()); rangePanel.add(getRangeLabel(), java.awt.BorderLayout.NORTH); rangePanel.add(getRangeSlider(), java.awt.BorderLayout.CENTER); } return rangePanel; } /** * This method initializes rangeLabel * * @return javax.swing.JLabel */ private javax.swing.JLabel getRangeLabel() { if(rangeLabel == null) { rangeLabel = new javax.swing.JLabel(); rangeLabel.setText("Range"); rangeLabel.setHorizontalAlignment( javax.swing.SwingConstants.CENTER); rangeLabel.setHorizontalTextPosition( javax.swing.SwingConstants.CENTER); rangeLabel.setDisplayedMnemonic(java.awt.event.KeyEvent.VK_R); } return rangeLabel; } /** * This method initializes rangeSlider * * @return javax.swing.JSlider */ private javax.swing.JSlider getRangeSlider() { if(rangeSlider == null) { rangeSlider = new javax.swing.JSlider(); rangeSlider.setOrientation(javax.swing.JSlider.VERTICAL); rangeSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE); rangeSlider.setMaximum(50); rangeSlider.setMajorTickSpacing(10); rangeSlider.setMinorTickSpacing(5); rangeSlider.setValue(0); rangeSlider.setToolTipText("Variation"); rangeSlider.setPaintLabels(true); rangeSlider.setPaintTicks(true); rangeSlider.addChangeListener( new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent e) { playerModel.setRange((float) rangeSlider.getValue()); } }); } return rangeSlider; } 103 Project One: Duke Speaks Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 103 /** * This method initializes textAndButtonPanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getTextAndButtonPanel() { if(textAndButtonPanel == null) { textAndButtonPanel = new javax.swing.JPanel(); textAndButtonPanel.setLayout(new java.awt.BorderLayout()); textAndButtonPanel.add(getTextAreaLabel(), java.awt.BorderLayout.NORTH); textAndButtonPanel.add(getTextInputArea(), java.awt.BorderLayout.CENTER); textAndButtonPanel.add(getButtonPanel(), java.awt.BorderLayout.SOUTH); textAndButtonPanel.setBorder( javax.swing.BorderFactory.createEtchedBorder( javax.swing.border.EtchedBorder.RAISED)); } return textAndButtonPanel; } /** * This method initializes textAreaLabel * * @return javax.swing.JLabel */ private javax.swing.JLabel getTextAreaLabel() { if(textAreaLabel == null) { textAreaLabel = new javax.swing.JLabel(); textAreaLabel.setText("Enter Text:"); textAreaLabel.setDisplayedMnemonic( java.awt.event.KeyEvent.VK_T); } return textAreaLabel; } /** * This method initializes buttonPanel * * @return javax.swing.JPanel */ private javax.swing.JPanel getButtonPanel() { if(buttonPanel == null) { buttonPanel = new javax.swing.JPanel(); buttonPanel.add(getSpeakButton(), null); buttonPanel.add(getDeleteButton(), null); } return buttonPanel; } /** * This method initializes speakButton * * @return javax.swing.JButton */ private javax.swing.JButton getSpeakButton() { 104 Chapter 5 Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 104 if(speakButton == null) { speakButton = new javax.swing.JButton(); speakButton.setText("Speak Text"); speakButton.setMnemonic(java.awt.event.KeyEvent.VK_S); speakButton.setToolTipText("Speak text in text area"); speakButton.setBackground(new java.awt.Color(250,250,250)); speakButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { String inputText = textInputArea.getText(); if (inputText.length() > 0) playerModel.play(inputText); } }); } return speakButton; } /** * This method initializes deleteButton * * @return javax.swing.JButton */ private javax.swing.JButton getDeleteButton() { if(deleteButton == null) { deleteButton = new javax.swing.JButton(); deleteButton.setText("Delete Text"); deleteButton.setMnemonic(java.awt.event.KeyEvent.VK_D); deleteButton.setToolTipText("Delete all text in text area"); deleteButton.setBackground(new java.awt.Color(250,250,250)); deleteButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { textInputArea.setText(""); } }); } return deleteButton; } /** * This method initializes textInputArea * * @return javax.swing.JTextArea */ private javax.swing.JTextArea getTextInputArea() { if(textInputArea == null) { textInputArea = new javax.swing.JTextArea(); textInputArea.setLineWrap(true); textInputArea.setRows(5); textInputArea.setText("Hello, this is your animated voice!"); } return textInputArea; } /** * This method initializes jLabel5 105 Project One: Duke Speaks Listing 5.7 (Continues) 07_020059_ch05.qxd 10/8/04 10:52 AM Page 105 [...]... other parts of the Eclipse workbench, the Debugger can be configured by the user in various ways For example, under Window > Preferences > Java > Debug > Detail Formatters, you can specify how the values of Java types are to be displayed in the Details section of the Variables View The default formatting uses the toString() method for displaying the variable’s value To add a new formatter, press the... that the Javadoc Command entry points to a valid Javadoc processor such as \j2sdk1.4.2\bin\javadoc.exe and that the Destination entry specifies a destination folder for the Javadoc pages You can preset a default value by applying the Properties > Javadoc Location context function to the project You can also specify for which packages and for which methods Javadoc is to be generated In addition, you have... script for Javadoc generation (Figure 6.10) I will discuss Ant in more detail in Chapter 13 After you press Finish, the Javadoc generation is started as a batch job The output of the batch job appears on the Eclipse Java console 126 Project Development Figure 6.9 Figure 6.10 127 Chapter 6 Summar y In this chapter you have learned how both local and remote Java programs can be debugged with Eclipse. .. specify additional command-line options for javadoc exe, if necessary In addition, you can specify a file containing additional text for the Javadoc Overview page The JRE 1.4 Source Compatibility option must be checked if your source code contains the Java assert keyword Without this option set, Java 1.4 programs that contain this instruction would cause errors during the Javadoc generation Checking this... class In addition, you must checkmark the public static void main(…) option This will generate a stub for the main() method package com.bdaum.dukeSpeaks; import import import import import java. awt.BorderLayout; java. awt.event.WindowAdapter; java. awt.event.WindowEvent; java. net.URL; java. util.Locale; import javax.swing.*; import com.sun.speech.freetts.Age; import com.sun.speech.freetts.Gender; 106 Project... using a CVS repository with Eclipse I will also show how external tools can be embedded into Eclipse Developing in a Team In this book, I want to take only a short excursion into Eclipse s support for development teams Detailed information can be found in the Eclipse help pages under Workbench User Guide > Tasks > Working in the Team Environment Different concepts exist for working collaboratively... people behind JUnit are Kent Beck and Erich Gamma Since Erich Gamma is also significantly involved in the development of Eclipse, it is no surprise that JUnit is contained in the Eclipse SDK distribution Detailed information is available at www.junit.org or in Professional Java Tools for Extreme Programming by Richard Hightower, et al Test tools such as JUnit are used to test program modules repeatedly,... exported as an HTML Javadoc documentation To generate the Javadoc documentation for the completed DukeSpeaks project, select the project and invoke the Export context function On the first page of the Export Wizard, select Javadoc from the list and press the Next button On the following page, you can specify in detail what should be exported to Javadoc (Figure 6.8) But first make sure that the Javadoc Command... second part I will introduce the JUnit test tool, which is part of the Eclipse SDK distribution Finally, in the third part I will show how Javadoc documentation can be exported Debugging Searching for bugs in a complex application is always a time-consuming task A powerful debugger can be of great help here Fortunately, the Eclipse Java IDE is equipped with a full-featured debugger that leaves hardly...Chapter 5 * * @return javax.swing.JLabel */ private javax.swing.JLabel getJLabel5() { if(jLabel5 == null) { jLabel5 = new javax.swing.JLabel(); jLabel5.setText(""); } return jLabel5; } /** * This method initializes jLabel6 * * @return javax.swing.JLabel */ private javax.swing.JLabel getJLabel6() { if(jLabel6 == null) { jLabel6 = new javax.swing.JLabel(); jLabel6.setText(""); } . stub for the main() method. package com.bdaum.dukeSpeaks; import java. awt.BorderLayout; import java. awt.event.WindowAdapter; import java. awt.event.WindowEvent; import java. net.URL; import java. util.Locale; import. null; private javax.swing.JPanel speedPanel = null; private javax.swing.JLabel speedLabel = null; private javax.swing.JSlider speedSlider = null; private javax.swing.JPanel pitchPanel = null; private javax.swing.JLabel. null; private javax.swing.JSlider pitchSlider = null; private javax.swing.JPanel rangePanel = null; private javax.swing.JLabel rangeLabel = null; private javax.swing.JSlider rangeSlider = null; private javax.swing.JPanel

Ngày đăng: 12/08/2014, 23:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan