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).

1  
!7
2  
3  
lib 1007242 // TableLayout
4  
5  
import layout.TableLayout;
6  
7  
p-awt-noconsole {
8  
  new TypicalGui;
9  
}
10  
11  
sclass TypicalGui extends JFrame
12  
{
13  
    public TypicalGui ()
14  
    {
15  
        super ("A Typical GUI");
16  
        
17  
        Container pane = getContentPane();
18  
        
19  
        double b = 10;
20  
        double f = TableLayout.FILL;
21  
        double p = TableLayout.PREFERRED;
22  
        double size[][] = {{b, f, 5, p, 5, p, b}, {p, b, f, 10, p, b}};
23  
        TableLayout layout = new TableLayout(size);
24  
        pane.setLayout (layout);
25  
        
26  
        addMenu(pane);
27  
        addCommandButtons(pane, layout);
28  
        addColorBoxes(pane, layout);
29  
        addTextArea(pane, layout);
30  
        
31  
        allowClosing();
32  
        setSize (640, 480);
33  
        show();
34  
    }
35  
    
36  
    
37  
    
38  
    public void addMenu (Container pane)
39  
    {
40  
        JMenuBar menuBar = new JMenuBar();
41  
        
42  
        String menuText[] = {"File", "Edit", "View", "Help"};
43  
        String itemText[][] =
44  
            {{"New", "Open", "Save", "Print", "Exit"},
45  
             {"Cut", "Copy", "Paste"},
46  
             {"Zoom in", "Zoom out"},
47  
             {"About", "Index", "Search"}};
48  
49  
        for (int i = 0; i < menuText.length; i++)
50  
        {
51  
            JMenu menu = new JMenu(menuText[i]);
52  
            menuBar.add (menu);
53  
            
54  
            for (int j = 0; j < itemText[i].length; j++)
55  
            {
56  
                JMenuItem item = new JMenuItem(itemText[i][j]);
57  
                menu.add (item);
58  
            }
59  
        }
60  
        
61  
        pane.add (menuBar, "0, 0, 6, 0");
62  
    }
63  
64  
    
65  
66  
    public void addCommandButtons (Container pane, TableLayout layout)
67  
    {
68  
        JPanel buttonPanel = new JPanel();
69  
        pane.add (buttonPanel, "1, 4, 5, 4");
70  
        
71  
        for (int i = 1; i <= 5; i++)
72  
        {
73  
            JButton button = new JButton("Button " + i);
74  
            buttonPanel.add (button);
75  
        }
76  
    }
77  
    
78  
    
79  
    
80  
    public void addColorBoxes (Container pane, TableLayout layout)
81  
    {
82  
        Color color[][] =
83  
            {{Color.white, Color.black},
84  
             {Color.green, Color.blue},
85  
             {Color.red, Color.yellow},
86  
             {Color.pink, Color.orange},
87  
             {Color.magenta, Color.cyan}};
88  
        
89  
        for (int i = 0; i < color.length; i++)
90  
        {
91  
            // Add a row for spacing and a row for the color boxes
92  
            layout.insertRow (2, TableLayout.PREFERRED);
93  
            layout.insertRow (2, 5);
94  
            
95  
            // Add color boxes
96  
            pane.add (new ColorBox(color[i][0]), "3, 3");
97  
            pane.add (new ColorBox(color[i][1]), "5, 3");
98  
        }
99  
             
100  
        // Remove the unnecessary leading space
101  
        layout.deleteRow (2);
102  
    }
103  
    
104  
    
105  
    
106  
    public void addTextArea (Container pane, TableLayout layout)
107  
    {
108  
        int numRow = layout.getRow().length;
109  
        JTextPane textArea = new JTextPane();
110  
        pane.add (textArea, "1, 2, 1, " + (numRow - 4));
111  
    }
112  
    
113  
    
114  
    
115  
    public void allowClosing ()
116  
    {
117  
        addWindowListener
118  
            (new WindowAdapter()
119  
                {
120  
                    public void windowClosing (WindowEvent e)
121  
                    {
122  
                        System.exit (0);
123  
                    }
124  
                }
125  
            );
126  
    }
127  
    
128  
    
129  
    
130  
    //**************************************************************************
131  
    //*** Inner classes                                                      ***
132  
    //**************************************************************************
133  
    
134  
    
135  
    
136  
    protected class ColorBox extends Component
137  
    {
138  
        protected Color color;
139  
        
140  
        protected ColorBox (Color color)
141  
        {
142  
            this.color = color;
143  
        }
144  
        
145  
        public void update (Graphics g)
146  
        {
147  
            paint (g);
148  
        }
149  
        
150  
        public void paint (Graphics g)
151  
        {
152  
            Dimension d = getSize();
153  
            g.setColor (Color.black);
154  
            g.drawRect (0, 0, d.width - 1, d.height - 1);
155  
            
156  
            g.setColor (color);
157  
            g.fillRect (1, 1, d.width - 1, d.height - 1);
158  
        }
159  
        
160  
        public Dimension getPreferredSize ()
161  
        {
162  
            return new Dimension(40, 20);
163  
        }
164  
    }
165  
}

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: 491 / 555
Version history: 4 change(s)
Referenced in: [show references]