Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Main extends JPanel {
    static int MAX = 5;
    JTabbedPane pane = new JTabbedPane();

    public Main() {
        for (int i = 0; i < MAX; i++) {
            Color color = Color.getHSBColor((float) i / MAX, 1, 1);
            pane.add("Tab " + String.valueOf(i), new TabContent(i, color));
            pane.setBackgroundAt(i, color);
        }
        this.add(pane);
    }

    private void display() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Main().display();
    }
}

class TabContent extends JPanel {

    public TabContent(int i, Color color) {
        setOpaque(true);
        setBackground(color);
        add(new JLabel("Tab content " + String.valueOf(i)));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(320, 240);
    }
}