Wednesday, February 20, 2013

JTabbedPane with icon

Tabbed panel with icon
Tabbed panel with icon


package javajframeapplication;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaJFrameApplication {
    
    static public class MyTabJPanel extends JPanel {
        
        public MyTabJPanel(){
            super(new GridLayout(1, 1));
            
            JTabbedPane jTabbedPane = new JTabbedPane();
            
            JPanel jPaneA = new JPanel();
            JLabel jLabelA = new JLabel("It's Tab A");
            jPaneA.add(jLabelA);
            URL imageUrl = JavaJFrameApplication.class.getResource("duke_20x36.png");
            ImageIcon imageIconA = new ImageIcon(imageUrl);
            
            JPanel jPaneB = new JPanel();
            JButton jButton = new JButton("Button in Tab B");
            jPaneB.add(jButton);
            
            JPanel jPaneC = new JPanel();
            JButton buttonExit = new JButton("Exit button on Tab C");
            buttonExit.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }

            });
            jPaneC.add(buttonExit);
            
            jTabbedPane.addTab("Tab A", imageIconA, jPaneA);
            jTabbedPane.addTab("Tab B", jPaneB);
            jTabbedPane.addTab("Tab C", jPaneC);
            
            add(jTabbedPane);
        }
        
    }
    
    public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                JFrame mainJFrame = new JFrame();
                mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainJFrame.setTitle("java-buddy.blogspot.com");
                mainJFrame.setSize(400, 300);

                MyTabJPanel myTabJPanel = new MyTabJPanel();
                mainJFrame.add(myTabJPanel, BorderLayout.CENTER);
                mainJFrame.setVisible(true);
            }
        });
    }
}


1 comment:

  1. mình muốn cho cái icon nằm trên cái chữ thì sao bạn

    ReplyDelete