Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable

Thread: Help with this keyboard GUI

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Help with this keyboard GUI

    Hi, so far I have:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    
    public class GUI extends JFrame{
    	
    	private JPanel keyboardPanel = new JPanel(new GridLayout(4,0));
    	private JPanel numberKeyboardPanel = new JPanel(new GridLayout(0,12));
    
    	private JPanel topKeyboardPanel = new JPanel(new GridLayout(0,11));
    	private JPanel middleKeyboardPanel = new JPanel(new GridLayout(0,12));
    	private JPanel bottomKeyboardPanel = new JPanel(new GridLayout(0,11));
    	
    	private JPanel middlePanel = new JPanel(new BorderLayout());
    	private JTextArea outputArea = new JTextArea(20,20);
    	
    	//Sets up the numbers
    	private JButton oneBtn = new JButton("1");
    	private JButton twoBtn = new JButton("2");
    	private JButton threeBtn = new JButton("3");
    	private JButton fourBtn = new JButton("4");
    	private JButton fiveBtn = new JButton("5");
    	private JButton sixBtn = new JButton("6");
    	private JButton sevenBtn = new JButton("7");
    	private JButton eightBtn = new JButton("8");
    	private JButton nineBtn = new JButton("9");
    	private JButton zeroBtn = new JButton("0");
    	
    	//Sets up Q to P keys
    	private JButton qBtn = new JButton("Q");
    	private JButton wBtn = new JButton("W");
    	private JButton eBtn = new JButton("E");
    	private JButton rBtn = new JButton("R");
    	private JButton tBtn = new JButton("T");
    	private JButton yBtn = new JButton("Y");
    	private JButton uBtn = new JButton("U");
    	private JButton iBtn = new JButton("I");
    	private JButton oBtn = new JButton("O");
    	private JButton pBtn = new JButton("P");
    	private JButton delBtn = new JButton("Del");
    	
    	//Sets up A to L
    	private JButton aBtn = new JButton("A");
    	private JButton sBtn = new JButton("S");
    	private JButton dBtn = new JButton("D");
    	private JButton fBtn = new JButton("F");
    	private JButton gBtn = new JButton("G");
    	private JButton hBtn = new JButton("H");
    	private JButton jBtn = new JButton("J");
    	private JButton kBtn = new JButton("K");
    	private JButton lBtn = new JButton("L");
    	private JButton retBtn = new JButton("Ret");
    	
    	//Sets up Z to M
    	private JButton zBtn = new JButton("Z");
    	private JButton xBtn = new JButton("X");
    	private JButton cBtn = new JButton("C");
    	private JButton vBtn = new JButton("V");
    	private JButton bBtn = new JButton("B");
    	private JButton nBtn = new JButton("N");
    	private JButton mBtn = new JButton("M");
    	
    	public GUI () {
    		this.setTitle("Stock Ticker");
    		this.setLayout(new BorderLayout());
    		numberKeyboardPanel.add(new JLabel());
    		numberKeyboardPanel.add(oneBtn);
    		numberKeyboardPanel.add(twoBtn);
    		numberKeyboardPanel.add(threeBtn);
    		numberKeyboardPanel.add(fourBtn);
    		numberKeyboardPanel.add(fiveBtn);
    		numberKeyboardPanel.add(sixBtn);
    		numberKeyboardPanel.add(sevenBtn);
    		numberKeyboardPanel.add(eightBtn);
    		numberKeyboardPanel.add(nineBtn);
    		numberKeyboardPanel.add(zeroBtn);
    				
    		topKeyboardPanel.add(qBtn);
    		topKeyboardPanel.add(wBtn);
    		topKeyboardPanel.add(eBtn);
    		topKeyboardPanel.add(rBtn);
    		topKeyboardPanel.add(tBtn);
    		topKeyboardPanel.add(yBtn);
    		topKeyboardPanel.add(uBtn);
    		topKeyboardPanel.add(iBtn);
    		topKeyboardPanel.add(oBtn);
    		topKeyboardPanel.add(pBtn);
    		topKeyboardPanel.add(delBtn);
    		
    		middleKeyboardPanel.add(new JLabel());
    		middleKeyboardPanel.add(aBtn);
    		middleKeyboardPanel.add(sBtn);
    		middleKeyboardPanel.add(dBtn);
    		middleKeyboardPanel.add(fBtn);
    		middleKeyboardPanel.add(gBtn);
    		middleKeyboardPanel.add(hBtn);
    		middleKeyboardPanel.add(jBtn);
    		middleKeyboardPanel.add(kBtn);
    		middleKeyboardPanel.add(lBtn);
    		middleKeyboardPanel.add(retBtn);
    		
    		bottomKeyboardPanel.add(new JLabel());
    		bottomKeyboardPanel.add(new JLabel());
    		bottomKeyboardPanel.add(zBtn);
    		bottomKeyboardPanel.add(xBtn);
    		bottomKeyboardPanel.add(cBtn);
    		bottomKeyboardPanel.add(vBtn);
    		bottomKeyboardPanel.add(bBtn);
    		bottomKeyboardPanel.add(nBtn);
    		bottomKeyboardPanel.add(mBtn);
    		
    		keyboardPanel.add(numberKeyboardPanel);
    		keyboardPanel.add(topKeyboardPanel);
    		keyboardPanel.add(middleKeyboardPanel);
    		keyboardPanel.add(bottomKeyboardPanel);
    		
    		middlePanel.add(new JLabel("Output: "));
    		
    		middlePanel.add(outputArea);
    		middlePanel.add(new JLabel("Input: "));
    		
    		this.getContentPane().add(middlePanel, BorderLayout.CENTER);
    		this.getContentPane().add(keyboardPanel, BorderLayout.SOUTH);
    		
    		this.setSize(530,300);
    		this.setVisible(true);
    		
    	}
    	
    	public static void main(String[] args) {
    		GUI gui = new GUI();
    	}
    
    }
    Can somone explain why the output label isn't showing a long with its text field.

    Thanks
    Last edited by Fubarable; 02-18-2012 at 12:39 AM. Reason: code tags added

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

    Default Re: Help with this keyboard GUI

    Don't set size of anything. Instead what happens if you call pack() on the JFrame prior to calling setVisible(true)?

    Also, your code can be greatly simplified by using arrays of Strings and for loops to create your JButtons.

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

    Default Re: Help with this keyboard GUI

    Also, I've added code tags to your code to help make it readable. Consider doing that in future posts by adding [code] above the code block and [/code] below the block.

    As an example of what I meant by using String arrays, check out this code to make the keyboard portion of a simple calculator:

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Calc {
       private static final String[][] KEYS = {
          {"7", "8", "9", "-"},
          {"4", "5", "6", "+"},
          {"1", "2", "3", "*"},
          {"0", ".", "=", "/"}
       };
       private JPanel panel = new JPanel();
       
       public Calc() {
          ActionListener actionListener = new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                System.out.println("Key pressed: " + actionCommand);
             }
          };
          panel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
          panel.setLayout(new GridLayout(KEYS.length, KEYS[0].length, 5, 5));
          for (int row = 0; row < KEYS.length; row++) {
             for (int col = 0; col < KEYS[row].length; col++) {
                JButton btn = new JButton(KEYS[row][col]);
                btn.addActionListener(actionListener);
                panel.add(btn);
             }
          }
       }
       
       public JPanel getPanel() {
          return panel;
       }
       
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Calc");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new Calc().getPanel());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
       
    }

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Help with this keyboard GUI

    Can someone please explain how to get the output label to show aswell as the textfield next to the output.

    Also for input I need a text field to show what the user enters and allos the delete button to work.

    Can some offer me advice on how to do this please.

    THanks

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Help with this keyboard GUI

    Quote Originally Posted by stuckonjava View Post
    Can someone please explain how to get the output label to show aswell as the textfield next to the output.
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Help with this keyboard GUI

    can someone tell me how to get rid of the code i posted above?

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

    Default Re: Help with this keyboard GUI

    Quote Originally Posted by stuckonjava View Post
    can someone tell me how to get rid of the code i posted above?
    You really shouldn't do that. Your question is not owned by you but belongs to the forum and to anyone else who uses the forum and can learn from the information it holds. That includes students with the same problem and that includes instructors looking to see what their students have posted here.

  8. #8
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Help with this keyboard GUI

    Could you explain how to delete it please as it's wrong

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

    Default Re: Help with this keyboard GUI

    Quote Originally Posted by stuckonjava View Post
    Could you explain how to delete it please as it's wrong
    Again, I don't advise deleting and will prevent deleting unless you can give a good reason for doing so.

  10. #10
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Help with this keyboard GUI

    Because it is an inefficient way to program , hence I am trying to learn to cut down code and it' not a good example for people that are confused. I don't mind to be honest but the quality isn't good and not many people help here, they just provide links so I feel I can't improve from here.

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

    Default Re: Help with this keyboard GUI

    Quote Originally Posted by stuckonjava View Post
    Because it is an inefficient way to program , hence I am trying to learn to cut down code and it' not a good example for people that are confused. I don't mind to be honest but the quality isn't good and not many people help here, they just provide links so I feel I can't improve from here.
    Then let's just leave it up and let's work on posting some more efficient code in this thread.
    DarrylBurke likes this.

Similar Threads

  1. need help for onscreen Keyboard
    By rajnic in forum AWT / Swing
    Replies: 1
    Last Post: 03-15-2011, 10:16 AM
  2. restrict keyboard
    By srivempala in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 10:38 PM
  3. Keyboard
    By UJJAL DHAR in forum New To Java
    Replies: 4
    Last Post: 01-29-2011, 03:26 PM
  4. KeyBoard Problem
    By arimakidd in forum Java Applets
    Replies: 3
    Last Post: 09-19-2008, 05:05 PM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 03:51 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
  •