/*
* 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 tjtabbedpane;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class TJTabbedPane extends JApplet {
JTabbedPane tabbedPane;
String[] aircraft = {"Aircraft1.jpg",
"Aircraft2.jpg",
"Aircraft3.jpg"};
String[] tips = {"Cruise", "Banking", "Take-off"};
public void init() {
// 1. Create a tabbed pane object.
tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
tabbedPane.addChangeListener(new TabbedPaneListener());
// 2. Add tabs to the tabbed pane. Each tab displays
// an aircraft.
for (int i=0; i<aircraft.length; i++) {
JLabel label = new JLabel(new ImageIcon(aircraft[i]));
tabbedPane.addTab("Aircraft-"+(i+1), // Tab text
new ImageIcon("smiley.gif"), // Tab icon
label, // component to be displayed
tips[i]); // Some tip at the tab
}
// 3. Add the tabbed pane to the applet.
getContentPane().add(tabbedPane);
}
// 4. Tabbed Pane listener class.
class TabbedPaneListener implements ChangeListener {
int selectedIndex = -1; // -1 = default number less than 0
JTabbedPane tp;
public void stateChanged(ChangeEvent ce) {
tp = (JTabbedPane) ce.getSource();
// 5. Code to disable a tab that has been selected, and
// enable the tab that has already been disabled.
if (selectedIndex == -1 || // if pressed for first time
selectedIndex != tp.getSelectedIndex()) {
tp.setEnabledAt(tp.getSelectedIndex(), false);
if (selectedIndex != -1) // if not the first press
tp.setEnabledAt(selectedIndex, true);
}
// Store the index of the newly selected tab.
selectedIndex = tp.getSelectedIndex();
}
}
}
Tidak ada komentar:
Posting Komentar