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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;

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

public class Main extends JPanel {
    static Random random = new Random();
    static String format = "00000000";
    static DecimalFormat df = new DecimalFormat(format);
    static JTabbedPane tabbedPane = new JTabbedPane();
    Timer timer;
    JLabel odometer = new JLabel(df.format(0));
    int km;

    public Main() {
        setPreferredSize(new Dimension(320, 240));
        add(odometer);
        JComboBox colorBox = new JComboBox(new String[] { "a", "b" });
        colorBox.addActionListener(e -> {
            tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
        });
        this.add(colorBox);
        timer = new Timer(250, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                km += random.nextInt(100);
                odometer.setText(df.format(km));
            }
        });
        timer.start();
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tabbedPane.add("Cab #1", new Main());
        tabbedPane.add("Cab #2", new Main());
        tabbedPane.add("Cab #3", new Main());
        f.add(tabbedPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}