Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

165
LINES

< > BotCompany Repo | #1007244 // Test TableLayout 2

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 110K of libraries. Click here for Pure Java version (1610L/10K/40K).

!7

lib 1007242 // TableLayout

import layout.TableLayout;

p-awt-noconsole {
  new TypicalGui;
}

sclass TypicalGui extends JFrame
{
    public TypicalGui ()
    {
        super ("A Typical GUI");
        
        Container pane = getContentPane();
        
        double b = 10;
        double f = TableLayout.FILL;
        double p = TableLayout.PREFERRED;
        double size[][] = {{b, f, 5, p, 5, p, b}, {p, b, f, 10, p, b}};
        TableLayout layout = new TableLayout(size);
        pane.setLayout (layout);
        
        addMenu(pane);
        addCommandButtons(pane, layout);
        addColorBoxes(pane, layout);
        addTextArea(pane, layout);
        
        allowClosing();
        setSize (640, 480);
        show();
    }
    
    
    
    public void addMenu (Container pane)
    {
        JMenuBar menuBar = new JMenuBar();
        
        String menuText[] = {"File", "Edit", "View", "Help"};
        String itemText[][] =
            {{"New", "Open", "Save", "Print", "Exit"},
             {"Cut", "Copy", "Paste"},
             {"Zoom in", "Zoom out"},
             {"About", "Index", "Search"}};

        for (int i = 0; i < menuText.length; i++)
        {
            JMenu menu = new JMenu(menuText[i]);
            menuBar.add (menu);
            
            for (int j = 0; j < itemText[i].length; j++)
            {
                JMenuItem item = new JMenuItem(itemText[i][j]);
                menu.add (item);
            }
        }
        
        pane.add (menuBar, "0, 0, 6, 0");
    }

    

    public void addCommandButtons (Container pane, TableLayout layout)
    {
        JPanel buttonPanel = new JPanel();
        pane.add (buttonPanel, "1, 4, 5, 4");
        
        for (int i = 1; i <= 5; i++)
        {
            JButton button = new JButton("Button " + i);
            buttonPanel.add (button);
        }
    }
    
    
    
    public void addColorBoxes (Container pane, TableLayout layout)
    {
        Color color[][] =
            {{Color.white, Color.black},
             {Color.green, Color.blue},
             {Color.red, Color.yellow},
             {Color.pink, Color.orange},
             {Color.magenta, Color.cyan}};
        
        for (int i = 0; i < color.length; i++)
        {
            // Add a row for spacing and a row for the color boxes
            layout.insertRow (2, TableLayout.PREFERRED);
            layout.insertRow (2, 5);
            
            // Add color boxes
            pane.add (new ColorBox(color[i][0]), "3, 3");
            pane.add (new ColorBox(color[i][1]), "5, 3");
        }
             
        // Remove the unnecessary leading space
        layout.deleteRow (2);
    }
    
    
    
    public void addTextArea (Container pane, TableLayout layout)
    {
        int numRow = layout.getRow().length;
        JTextPane textArea = new JTextPane();
        pane.add (textArea, "1, 2, 1, " + (numRow - 4));
    }
    
    
    
    public void allowClosing ()
    {
        addWindowListener
            (new WindowAdapter()
                {
                    public void windowClosing (WindowEvent e)
                    {
                        System.exit (0);
                    }
                }
            );
    }
    
    
    
    //**************************************************************************
    //*** Inner classes                                                      ***
    //**************************************************************************
    
    
    
    protected class ColorBox extends Component
    {
        protected Color color;
        
        protected ColorBox (Color color)
        {
            this.color = color;
        }
        
        public void update (Graphics g)
        {
            paint (g);
        }
        
        public void paint (Graphics g)
        {
            Dimension d = getSize();
            g.setColor (Color.black);
            g.drawRect (0, 0, d.width - 1, d.height - 1);
            
            g.setColor (color);
            g.fillRect (1, 1, d.width - 1, d.height - 1);
        }
        
        public Dimension getPreferredSize ()
        {
            return new Dimension(40, 20);
        }
    }
}

Author comment

Began life as a copy of #1007243

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007244
Snippet name: Test TableLayout 2
Eternal ID of this version: #1007244/5
Text MD5: d6a6b35b88cd0437d31f16878995d124
Transpilation MD5: b60fbc86ade470adfc66f0bebddc9541
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-12 22:51:39
Source code size: 4386 bytes / 165 lines
Pitched / IR pitched: No / No
Views / Downloads: 490 / 554
Version history: 4 change(s)
Referenced in: [show references]