Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default Edit layout Layout please help me

    Hello everyone !!

    I want to design a Layout a look as the Calculator. because I learn about the interface should I use: GridBagLayout to learn. I use various panel to add the button then add the panel to GBL. I don't want to add them directly to GBL.

    below is code i write:
    file:

    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.*;

    public class ToCalculator extends JFrame
    {

    private JMenuBar menuBar;
    private JMenu file,view,help;
    private JMenuItem mnew,exit,colorred, colorblue,colordefalt,helpitem;
    private JTextField input;


    private JButton btn[],btnSum, btnSub, btnDiv, btnMul, btnEqual,btnSqrt,btnSquare;
    private String str[] = {"0","1","2","3","4","5","6","7","8","9"};
    private GridBagLayout gridBagLayout;
    private GridBagConstraints gridBagConstraint;
    private Container container;

    private JPanel panel123,p;
    private JPanel panel470;
    private JPanel panel5689;
    public ToCalculator()
    {
    //supre must call the first on create function
    super("Calculator v1.0");
    this.setBackground(Color.red);

    file = new JMenu("File");
    mnew = new JMenuItem("New");
    exit = new JMenuItem("Exit");
    file.add(mnew);
    file.add(exit);

    view = new JMenu("View");
    colorred = new JMenuItem("Red");
    colorblue = new JMenuItem("Blue");
    colordefalt = new JMenuItem("Default");
    view.add(colorred) ;
    view.add(colorblue);
    view.add(colordefalt);

    help = new JMenu("Help");
    helpitem = new JMenuItem("Help");
    help.add(helpitem);

    menuBar = new JMenuBar();

    menuBar.add(file);
    menuBar.add(view);
    menuBar.add(help);
    setJMenuBar(menuBar);

    //layout
    btnEqual = new JButton("=");
    btnDiv = new JButton("/");
    btnMul = new JButton("*");
    btnSquare = new JButton("x^2");
    btnSqrt = new JButton("Sqrt");
    btnSub = new JButton("-");
    btnSum = new JButton("+");

    btn = new JButton[str.length];
    for(int i = 0;i<str.length;i++)
    {
    btn[i] = new JButton(str[i]);
    }

    panel123 = new JPanel( new GridLayout(1,4));
    panel123.add(btn[1]);
    panel123.add(btn[2]);
    panel123.add(btn[3]);
    panel123.add(btnSum);

    panel470 = new JPanel(new GridLayout(3,1));
    panel470.add(btn[4]);
    panel470.add(btn[7]);
    panel470.add(btn[0]);

    panel5689 = new JPanel( new GridLayout(3,3));
    panel5689.add(btn[5]);
    panel5689.add(btn[6]);
    panel5689.add(btnSub);
    panel5689.add(btn[8]);
    panel5689.add(btn[9]);
    panel5689.add(btnMul);
    panel5689.add(btnSqrt);
    panel5689.add(btnSquare);
    panel5689.add(btnDiv);

    input = new JTextField(20);

    gridBagLayout = new GridBagLayout();
    container = this.getContentPane();
    container.setLayout(gridBagLayout);

    gridBagConstraint = new GridBagConstraints();
    //add test field
    gridBagConstraint.gridy = 0;
    gridBagConstraint.gridx = 0;
    gridBagConstraint.gridwidth = 2;
    gridBagConstraint.gridheight = 1;
    container.add(input,gridBagConstraint);

    gridBagConstraint.gridy =1;
    gridBagConstraint.gridx = 0;
    gridBagConstraint.gridwidth = 2;
    gridBagConstraint.gridheight = 1;
    container.add(panel123,gridBagConstraint);

    gridBagConstraint.gridy = 2;
    gridBagConstraint.gridx = 0;
    gridBagConstraint.gridwidth = 1;
    gridBagConstraint.gridheight = 1;
    container.add(panel470,gridBagConstraint);

    gridBagConstraint.gridx = 1;
    gridBagConstraint.gridwidth = 1;
    gridBagConstraint.gridheight = 1;
    container.add(panel5689,gridBagConstraint);

    //to display Frame, must call the end on create function
    setSize(300,300);
    setVisible(true);
    }
    }
    [/CODE]

    If can. You don't change the panels

    thank all you.

    ( - I from viet nam - )

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    it seems like Fubarable and I are chasing each other ......gridbag is a layout method for advanced programmers i have used it and it takes lots of lines of code to get it right.......if your layout is important then don't let the user resize the window F.setResizable(false); but first you need to umderstand inheritance

  4. #4
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    I use: setResizable to disable maximize button. so what use to disable minimize button.

    Thank you

  5. #5
    Join Date
    Nov 2009
    Posts
    22
    Rep Power
    0

    Default

    I post LINK, but forum report "You have to post more 20 "
    :D I'm from vietnam - I hope you help me :D

Similar Threads

  1. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 10:37 PM
  2. Layout manager
    By Nicholas Jordan in forum Java Applets
    Replies: 2
    Last Post: 11-24-2008, 04:02 PM
  3. Java GUI layout
    By nick2price in forum New To Java
    Replies: 2
    Last Post: 05-27-2008, 10:42 PM
  4. Border Layout
    By mark-mlt in forum Java Applets
    Replies: 5
    Last Post: 05-12-2008, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •