MLaskun

JTabbedPane

May 17th, 2021 (edited)
525
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.awt.BorderLayout;
  5. import javax.swing.JButton;
  6. import javax.swing.JPanel;
  7.  
  8.  
  9. public class Tabbed1 extends JFrame implements ActionListener, Constants{
  10.     JTabbedPane tp = new JTabbedPane();
  11.    
  12.     Tabbed1(){
  13.         Color[] back = {BLUE, YELLOW, RED, WHITE, BLACK};
  14.         Color[] fore = {WHITE, BLACK, YELLOW, BLACK, WHITE};
  15.         String[] txt = {"Top","Left","Right","Bottom","Default"};
  16.         String[] loc = {"North","West","East","South","Center"};
  17.         int[] place = {TOP, LEFT, RIGHT, BOTTOM, TOP};
  18.         JButton b = null;
  19.         JPanel p = null;
  20.         for(int i=0;i<back.length;i++){
  21.             p = new JPanel(new BorderLayout());
  22.             for(int j=0;j<txt.length;j++){
  23.                 b = new JButton(txt[j]);
  24.                 b.addActionListener(this);
  25.                 b.putClientProperty("Place", new Integer(place[j]);
  26.                 p.add(b, loc[j]);
  27.             }
  28.             tp.addTab("Tab"+(i+1),p);
  29.             tp.setBackgroundAt(i, back[i]);
  30.             tp.setForegroundAt(i, fore[i]);
  31.         }
  32.         getContentPane().add(tp);
  33.         setSize(800,600);
  34.         setVisible(true);
  35.     }
  36.    
  37.     public void actionPerformed(ActionEvent e){
  38.         JComponent c = (JComponent) e.getSource();
  39.         Integer prop = (Integer) c.getClientProperty("Place");
  40.         tp.setTabPlacement(prop.intValue());
  41.         JComponent p = (JComponent) tp.getSelectedComponent();
  42.         Component[] b = p.getComponents();
  43.         for(int i=0;i<b.length;i++) b[i].setEnabled(false);
  44.         int tabs = tp.getTabCount();
  45.         int index = tp.getSelectedIndex();
  46.         if(index==tabs-1) index=0;
  47.         else index++;
  48.         tp.setSelectedIndex(index);
  49.     }
  50. }
RAW Paste Data