/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tcardlayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class TCardLayout extends JApplet {
// 1. Declare some panel and button references.
JPanel controlPanel;
JPanel cardsPanel, swCompPanel, aeroCompPanel;
JButton nextButton, previousButton;
JButton javasoftButton, sunButton,
ibmButton, netscapeButton;
JButton nasaButton, boeingButton,
lockheedButton, northropButton;
URL currentSite = null;
// 2. Define the string arrays for the sites and site urls.
String[] swCompSites = {"JavaSoft", "Sun",
"IBM", "NetScape"};
String[] aeroCompSites = {"NASA", "Boeing",
"Lockheed", "Northrop"};
String[] siteURLs = { "http://www.javasoft.com/",
"http://www.sun.com/",
"http://www.ibm.com/",
"http://www.netscape.com/" };
Container container = null;
public void init() {
// 3. Get a handle on the applet's content pane.
container = this.getContentPane();
// 4. Create a control panel object.
controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1,2,10,10));
// 5. Create and add the next and previous control buttons.
ButtonListener listener = new ButtonListener();
nextButton = new JButton("Next Card");
nextButton.setActionCommand("Next Button");
nextButton.addActionListener(listener);
controlPanel.add(nextButton);
previousButton = new JButton("Previous Card");
previousButton.setEnabled(false);
previousButton.setActionCommand("Previous Button");
previousButton.addActionListener(listener);
controlPanel.add(previousButton);
// 6. Create a panel to contain the cards.
/* Each card will display a set of buttons to
visit a specific Web site. */
cardsPanel = new JPanel();
cardsPanel.setLayout(new CardLayout());
swCompPanel = new JPanel();
aeroCompPanel = new JPanel();
swCompPanel.setLayout(new GridLayout(2,2,10,10));
aeroCompPanel.setLayout(new GridLayout(2,2,10,10));
// 7. Add buttons to the these cards.
for (int i=0; i<swCompSites.length; i++) {
JButton b = new JButton(swCompSites[i]);
b.addActionListener(listener);
b.setActionCommand(swCompSites[i]);
swCompPanel.add(b);
}
for (int i=0; i<aeroCompSites.length; i++) {
JButton b = new JButton(aeroCompSites[i]);
b.addActionListener(listener);
b.setActionCommand(aeroCompSites[i]);
aeroCompPanel.add(b);
}
// 8. Add the company-list panels to cards panel.
cardsPanel.add("Next Card", swCompPanel);
cardsPanel.add("Previous Card", aeroCompPanel);
// 9. Finally, add the control and the cards panels to
// the applet's content pane.
((BorderLayout)container.getLayout()).setVgap(10);
container.add("North", controlPanel);
container.add("Center", cardsPanel);
}
// 10. Functionality to be executed when the next-button
// is activated.
public void showNextCard() {
nextButton.setEnabled(false);
((CardLayout) cardsPanel.getLayout()).next(cardsPanel);
previousButton.setEnabled(true);
}
// 11. Functionality to be executed when the previous-button
// is activated.
public void showPreviousCard() {
previousButton.setEnabled(false);
((CardLayout) cardsPanel.getLayout()).previous(cardsPanel);
nextButton.setEnabled(true);
}
// 12. Functionality to display the specific Web page.
public void toDestination(String aSite) {
System.out.println("Connecting to the web site...");
try {
currentSite = new URL(aSite);
}
catch (MalformedURLException e) {
System.out.println("Unknown URL Address: " + aSite);
}
getAppletContext().showDocument(currentSite);
}
// 13. The action listener class.
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
JButton tempButton = (JButton) ae.getSource();
// 14. If the next or previous buttons are pressed...
if (tempButton.getActionCommand() == "Next Button") {
showNextCard();
}
else if (tempButton.getActionCommand() == "Previous Button") {
showPreviousCard();
}
// 15. If the Web site buttons are pressed...
for (int i=0; i<swCompSites.length; i++) {
if (tempButton.getActionCommand() == swCompSites[i])
toDestination(siteURLs[i]);
}
}
}
}
Tidak ada komentar:
Posting Komentar