Index Stanisław Lem Dzienniki Gwiazdowe(Tom I) 306 33 (3) spis10 Cizia Zyke Goraczka (2) Harry Potter I Kamien Filozoficzny abc.com.pl 9 Frankl Viktor E. Psychoterapia dla każdego (7) abc.com.pl 6 Colour ls.pl |
[ Pobierz całość w formacie PDF ] .add(edit);setJMenuBar(mb);getContentPane().add(text);}class CopyL implements ActionListener {public void actionPerformed(ActionEvent e) {String selection = text.getSelectedText();if (selection == null)return;StringSelection clipString =new StringSelection(selection);clipbd.setContents(clipString,clipString);}}class CutL implements ActionListener {public void actionPerformed(ActionEvent e) {String selection = text.getSelectedText();if (selection == null)return;StringSelection clipString =new StringSelection(selection);clipbd.setContents(clipString, clipString);text.replaceRange("",text.getSelectionStart(),text.getSelectionEnd());}}class PasteL implements ActionListener {public void actionPerformed(ActionEvent e) {Transferable clipData =clipbd.getContents(CutAndPaste.this);try {String clipString =(String)clipData.getTransferData(DataFlavor.stringFlavor);text.replaceRange(clipString,text.getSelectionStart(),text.getSelectionEnd());} catch(Exception ex) {System.err.println("Not String flavor");}}}public static void main(String[] args) {Console.run(new CutAndPaste(), 300, 200);}} ///:~The creation and addition of the menuand JTextArea should by now seem a pedestrian activity.What’sdifferent is the creation of the Clipboard field clipbd, which isdone through the Toolkit.[ Add Comment ]All the action takes place in thelisteners.The CopyL and CutL listeners are the same except forthe last line of CutL, which erases the line that’s been copied.The special two lines are the creation of aStringSelection object from the String andthe call to setContents( ) with thisStringSelection.That’s all there is to putting a String onthe clipboard.[ Add Comment ]In PasteL, data is pulled off theclipboard using getContents( ).What comesback is a fairly anonymous Transferable object,and you don’t really know what it contains.One way to find out is to callgetTransferDataFlavors( ), which returns anarray of DataFlavor objects indicating whichflavors are supported by this particular object.You can also ask it directlywith isDataFlavorSupported( ), passing inthe flavor you’re interested in.Here, however, the bold approach istaken: getTransferData( ) is called assumingthat the contents supports the String flavor, and if it doesn’t theproblem is sorted out in the exception handler.[ Add Comment ]In the future you can expect more dataflavors to besupported.Packaging an applet into a JARfileAn important use of the JAR utility is tooptimize applet loading.In Java 1.0, people tended to try to cram all theircode into a single applet class so the client would need only a single serverhit to download the applet code.Not only did this result in messy, hard to read(and maintain) programs, but the.class file was still uncompressed sodownloading wasn’t as fast as it could have been.[ Add Comment ]JAR files solve the problem bycompressing all of your.class files into a single file that isdownloaded by the browser.Now you can create the right design without worryingabout how many.class files it will generate, and the user will get amuch faster download time.[ Add Comment ]Consider TicTacToe.java.It lookslike a single class, but in fact it contains five inner classes, so that’ssix in all.Once you’ve compiled the program, you package it into a JARfile with the line:jar cf TicTacToe.jar *.classThis assumes that the only.classfiles in the current directory are the ones from TicTacToe.java(otherwise you’ll get extra baggage).[ Add Comment ]Now you can create an HTML page with thenewarchivetag to indicate the name of the JAR file.Here is the tag using the old form ofthe HTML tag, as an illustration:<head><title>TicTacToe Example Applet</title></head><body><applet code=TicTacToe.classarchive=TicTacToe.jarwidth=200 height=100></applet></body>You’ll need to put it into the new (messy, complicated) form shown earlier in the chapter in order to get it to work.Programming techniquesBecause GUI programming in Java has beenan evolving technology with some very significant changes between Java 1.0/1.1and the Swing library in Java 2, there have been some old programming idiomsthat have seeped through to examples that you might see given for Swing.Inaddition, Swing allows you to program in more and better ways than were allowedby the old models.In this section, some of these issues will be demonstrated byintroducing and examining some programming idioms.[ Add Comment ]Binding events dynamicallyOne of the benefits of theSwing event model isflexibility.You can add and remove event behavior with single method calls.Thefollowing example demonstrates this://: c13:DynamicEvents.java// You can change event behavior dynamically.// Also shows multiple actions for an event.// <applet code=DynamicEvents// width=250 height=400></applet>import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import com.bruceeckel.swing.*;public class DynamicEvents extends JApplet {ArrayList v = new ArrayList();int i = 0;JButtonb1 = new JButton("Button1"),b2 = new JButton("Button2");JTextArea txt = new JTextArea();class B implements ActionListener {public void actionPerformed(ActionEvent e) {txt.append("A button was pressed\n");}}class CountListener implements ActionListener {int index;public CountListener(int i) { index = i; }public void actionPerformed(ActionEvent e) {txt.append("Counted Listener "+index+"\n");}}class B1 implements ActionListener {public void actionPerformed(ActionEvent e) {txt.append("Button 1 pressed\n");ActionListener a = new CountListener(i++);v.add(a);b2.addActionListener(a);}}class B2 implements ActionListener {public void actionPerformed(ActionEvent e) {txt.append("Button2 pressed\n");int end = v.size() - 1;if(end >= 0) {b2.removeActionListener((ActionListener)v.get(end));v.remove(end);}}}public void init() {Container cp = getContentPane();b1.addActionListener(new B());b1.addActionListener(new B1());b2.addActionListener(new B());b2.addActionListener(new B2());JPanel p = new JPanel();p.add(b1);p.add(b2);cp.add(BorderLayout.NORTH, p);cp.add(new JScrollPane(txt));}public static void main(String[] args) {Console.run(new DynamicEvents(), 250, 400);}} ///:~The new twists in this example are:[ Add Comment ]There is more than onelistener attached to each Button.Usually, components handle events asmulticast, meaning that you can register many listeners for a singleevent.In the special components in which an event is handled as unicast,you’ll get a TooManyListenersException.[ Add Comment ]Duringthe execution of the program, listeners are dynamically added and removed fromthe Button b2.Adding is accomplished in the way you’ve seenbefore, but each component also has a removeXXXListener( ) method toremove each type of listener [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl. | |||||