Results 1 to 10 of 10
  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default Calculator Switching

    Hey

    I am new to java and i am making a scientific calculator, not full-fledge one but a basic version of a scientific calculator. I am using netBeans 6.8 to do it, and what i want to do is that once you run the program a basic calculator shows up but once you click the scientific button it switches to like a new frame and shows a calculator with all the scientific functions, a bit like the calculator on windows and how it switches, any clue how i do that?

    Thanks

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

    Default

    If this were my project I wouldn't have a new JFrame or swap JFrames but instead would keep the same JFrame and have it expand or contract to accommodate more or less buttons. I'd probably place the scientific buttons in their own JPanel (scientificPanel) that uses a GridLayout, and then add that JPanel to the JFrame's contentPane in the BorderLayout.EAST or WEST position. Then when there is a command to show or hide the scientific buttons, I'd call setVisible(true) or false on the scientificPanel, repack the JFrame and Voilà, my buttons would appear or disappear on command, and the layout managers would take care of the hard part.

    If any of this is not fully understandable, please have a look in the Sun Swing tutorials, especially the layout manager section which can be found here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Luck!

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,546
    Rep Power
    23

    Default

    Or how about the Cardlayout or JTabbedPane layouts?

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

    Default

    Quote Originally Posted by Norm View Post
    Or how about the Cardlayout or JTabbedPane layouts?
    Yep, this could work too. I guess I had my mind stuck on the Calculator that comes with Windows. :rolleyes:

  5. #5
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks for the replies, the tabbed method worked in a flash

    thank you

  6. #6
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    i have another question, i have sone if the same buttons in the second tab as the first, how do i use the same code to make the others work?

    EDIT

    Don't worry, adding panels and packing the jFrame works fine

    Thanks
    Last edited by dilpreet28; 06-08-2010 at 01:42 AM.

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

    Default

    Quote Originally Posted by dilpreet28 View Post
    i have another question, i have sone if the same buttons in the second tab as the first, how do i use the same code to make the others work?
    Actions! Such as AbstractAction.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,546
    Rep Power
    23

    Default

    I'm not sure you can have the same component in different containers.
    Can they be different buttons and just have the same text?

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

    Default

    Sorry for full code, but here's an example of using a JPanel that is set visible/invisible by menu as I suggested above:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class SciCalc {
      private static void createAndShowUI() {
        SciCalcGui gui = new SciCalcGui();
        SciCalcMenu menu = new SciCalcMenu(gui);
    
        JFrame frame = new JFrame("Calculator");
        frame.getContentPane().add(gui.getMainPanel());
        frame.setJMenuBar(menu.getJMenuBar());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }
    
    class SciCalcGui {
      private static final String[][] STANDARD_BTN_TEXTS = {
        {"7", "8", "9", "/"}, 
        {"4", "5", "6", "*"},
        {"1", "2", "3", "-"}, 
        {"0", ".", "=", "+"}};
      private static final String[][] SCIENTIFIC_BTN_TEXTS = {
        {"sqrt", "1/x", "sin"}, 
        {"%",    "Exp", "cos"},
        {"x^y",  "ln",  "tan"}, 
        {"x^2",  "n!",  "sec"}};
      private static final int GAP = 5;
      private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
      private JPanel mainPanel = new JPanel();
      private JPanel sciPanel;
      private JTextField display = new JTextField();
    
      public SciCalcGui() {
        display.setFont(BTN_FONT);
        JPanel standardPanel = createBtnPanel(STANDARD_BTN_TEXTS, "Standard");
        sciPanel = createBtnPanel(SCIENTIFIC_BTN_TEXTS, "Scientific");
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        mainPanel.add(standardPanel, BorderLayout.CENTER);
        mainPanel.add(sciPanel, BorderLayout.WEST);
        mainPanel.add(display, BorderLayout.NORTH);
    
        sciPanel.setVisible(false);
      }
    
      public void sciPanelSetVisible(boolean visible) {
        sciPanel.setVisible(visible);
        Window win = SwingUtilities.getWindowAncestor(mainPanel);
        win.pack();
      }
    
      public JPanel getMainPanel() {
        return mainPanel;
      }
    
      private JPanel createBtnPanel(String[][] texts, String title) {
        JPanel btnPanel = new JPanel();
        int rows = texts.length;
        int cols = texts[0].length;
        btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
        for (int row = 0; row < texts.length; row++) {
          for (int col = 0; col < texts[row].length; col++) {
            JButton btn = new JButton(texts[row][col]);
            btn.setFont(BTN_FONT);
            btnPanel.add(btn);
          }
        }
        btnPanel.setBorder(BorderFactory.createTitledBorder(title));
        return btnPanel;
      }
    
    }
    
    class SciCalcMenu {
      private static final String STANDARD = "Standard";
      private static final String SCIENTIFIC = "Scientific";
      private SciCalcGui gui;
      private JMenuBar menuBar = new JMenuBar();
      private JMenuItem standardView;
      private JMenuItem scientificView;
    
      public SciCalcMenu(SciCalcGui gui) {
        this.gui = gui;
    
        standardView = new JMenuItem(STANDARD, KeyEvent.VK_T);
        scientificView = new JMenuItem(SCIENTIFIC, KeyEvent.VK_S);
        ViewAction viewAction = new ViewAction();
        standardView.addActionListener(viewAction);
        scientificView.addActionListener(viewAction);
        standardView.setEnabled(false);
        
        JMenu viewMenu = new JMenu("View");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        viewMenu.add(standardView);
        viewMenu.add(scientificView);
        
        menuBar.add(new JMenu("Edit"));
        menuBar.add(viewMenu);
        menuBar.add(new JMenu("Help"));
      }
    
      public JMenuBar getJMenuBar() {
        return menuBar;
      }
    
      private class ViewAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();
          if (command.equals(STANDARD)) {
            gui.sciPanelSetVisible(false);
            standardView.setEnabled(false);
            scientificView.setEnabled(true);
          } else if (command.equals(SCIENTIFIC)) {
            gui.sciPanelSetVisible(true);
            standardView.setEnabled(true);   
            scientificView.setEnabled(false);
          }
        }
      }
    }

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

    Default

    Quote Originally Posted by Norm View Post
    I'm not sure you can have the same component in different containers.
    Can they be different buttons and just have the same text?
    You're right, you can't have a component in more than one container, but you can have components share models, but with JButtons, I think it's slicker to just have them share the same Action object.

Similar Threads

  1. Switching Frames
    By jonnytabpni in forum New To Java
    Replies: 1
    Last Post: 11-08-2009, 10:12 PM
  2. (Solved)switching from frame to another
    By HosHos in forum AWT / Swing
    Replies: 2
    Last Post: 08-11-2009, 02:19 PM
  3. Multi Protocol Label Switching ?!
    By gopithamizh in forum New To Java
    Replies: 1
    Last Post: 02-11-2009, 07:08 AM
  4. switching packages
    By DuceDuceExplorer in forum NetBeans
    Replies: 4
    Last Post: 07-09-2008, 11:11 PM
  5. switching between HTTP and HTTPS
    By mutuah in forum Advanced Java
    Replies: 6
    Last Post: 08-03-2007, 10:08 PM

Posting Permissions

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