Работа с панелями |
Исходный текст аплета OptionsИсходный текст аплета Options представлен в листинге 1. Листинг 1. Файл Options.java import java.applet.*; import java.awt.*; public class Options extends Applet { FirstPanel pPanel1; CardPanel pCard; ControlPanel pControl; public String getAppletInfo() { return "Name: Options"; } public void init() { setLayout(new GridLayout(3, 1)); pPanel1 = new FirstPanel(); add(pPanel1); pCard = new CardPanel(pPanel1); add(pCard); pControl = new ControlPanel(pCard); add(pControl); pPanel1.setBackground(Color.yellow); pPanel1.setForeground(Color.black); repaint(); } } class FirstPanel extends Panel { String szFontName = "TimesRoman"; public void paint(Graphics g) { Dimension dimAppWndDimension = getSize(); g.drawRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1); g.setFont(new Font(szFontName, Font.PLAIN, 24)); g.drawString("First panel", 10, 50); super.paint(g); } } class CardPanel extends Panel { Panel pBgColor; Panel pFgColor; Panel pFont; Panel pControlled; Choice chBgColor; Choice chFgColor; Choice chFont; Label lbBgColor; Label lbFgColor; Label lbFont; public CardPanel(Panel pControlledPanel) { pControlled = pControlledPanel; setLayout(new CardLayout(5, 5)); pBgColor = new Panel(); pFgColor = new Panel(); pFont = new Panel(); add("BgColor", pBgColor); add("FgColor", pFgColor); add("Font", pFont); chBgColor = new Choice(); chFgColor = new Choice(); chFont = new Choice(); chBgColor.add("Yellow"); chBgColor.add("Green"); chBgColor.add("White"); chFgColor.add("Black"); chFgColor.add("Red"); chFgColor.add("Green"); chFont.add("TimesRoman"); chFont.add("Helvetica"); chFont.add("Courier"); lbBgColor = new Label("Background color"); lbFgColor = new Label("Foreground color"); lbFont = new Label("Font"); pBgColor.add(lbBgColor); pBgColor.add(chBgColor); pFgColor.add(lbFgColor); pFgColor.add(chFgColor); pFont.add(lbFont); pFont.add(chFont); } public void paint(Graphics g) { Dimension dimAppWndDimension = getSize(); g.drawRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1); super.paint(g); } public boolean action(Event evt, Object obj) { Choice ch; if(evt.target instanceof Choice) { ch = (Choice)evt.target; if(evt.target.equals(chBgColor)) { if(ch.getSelectedIndex() == 0) pControlled.setBackground( Color.yellow); else if(ch.getSelectedIndex() == 1) pControlled.setBackground( Color.green); else if(ch.getSelectedIndex() == 2) pControlled.setBackground( Color.white); } else if(evt.target.equals(chFgColor)) { if(ch.getSelectedIndex() == 0) pControlled.setForeground( Color.black); else if(ch.getSelectedIndex() == 1) pControlled.setForeground( Color.red); else if(ch.getSelectedIndex() == 2) pControlled.setForeground( Color.green); } else if(evt.target.equals(chFont)) { if(ch.getSelectedIndex() == 0) ((FirstPanel)pControlled).szFontName = "TimesRoman"; else if(ch.getSelectedIndex() == 1) ((FirstPanel)pControlled).szFontName = "Helvetica"; else if(ch.getSelectedIndex() == 2) ((FirstPanel)pControlled).szFontName = "Courier"; } else { return false; } pControlled.repaint(); return true; } return false; } } class ControlPanel extends Panel { Button btNext; Button btPrev; Button btBgColor; Button btFgColor; Button btFont; Panel pCard; public ControlPanel(Panel pCardPanel) { pCard = pCardPanel; setLayout(new GridLayout(2,3)); btBgColor = new Button("Background Color"); btFgColor = new Button("Foreground Color"); btFont = new Button("Set Font"); btNext = new Button("Next"); btPrev = new Button("Prev"); add(btBgColor); add(btFgColor); add(btFont); add(btNext); add(btPrev); } public boolean action(Event evt, Object obj) { if(evt.target instanceof Button) { if(evt.target.equals(btBgColor)) { ((CardLayout)pCard.getLayout()).show( pCard, "BgColor"); } else if(evt.target.equals(btFgColor)) { ((CardLayout)pCard.getLayout()).show( pCard, "FgColor"); } else if(evt.target.equals(btFont)) { ((CardLayout)pCard.getLayout()).show( pCard, "Font"); } else if(evt.target.equals(btNext)) { ((CardLayout)pCard.getLayout()).next( pCard); } else if(evt.target.equals(btPrev)) { ((CardLayout)pCard.getLayout()). previous(pCard); } else { return false; } return true; } return false; } } Исходный текст документа HTML, созданный для аплета Options системой Java WorkShop, представлен в листинге 2. Листинг 2. Файл Options.tmp.html <applet name="Options" code="Options" codebase= "file:/E:/Sun/Articles/vol8/src/Options" width="500" height="600" align="Top" alt="If you had a java-enabled browser, you would see an applet here."> <hr>If your browser recognized the applet tag, you would see an applet here.<hr> </applet> |