Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default GUI Button text problem

    Hey guys,

    I'm designing a "phone" that will text for an assignment. Layout wise everything works, but I can't get the text on the buttons to display on two separate lines on each button? For buttonTwo I tried to insert another layout, but it still shows up on the left side of the button. Any suggestions?

    Java Code:
     
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class TextMessage  
    { 	
    	public void go()
    	{
    	
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	
    		JPanel panel = new JPanel();
    		JPanel buttonPanel = new JPanel();
    		
    		final int ROWS = 5;
    		final int COLUMNS = 300;
    		JTextArea display = new JTextArea(ROWS, COLUMNS);
        
    		JButton buttonSend = new JButton("Send");
    		JButton buttonBlank = new JButton("");
    		JButton buttonArrow = new JButton("<<");
    
    		JButton buttonOne = new JButton("1"); 
    		
    		// Text will not center, also a large gap between the two lines?
    		JButton buttonTwo = new JButton();
    		buttonTwo.setLayout(new BorderLayout());
    		JLabel label1 = new JLabel("2");
    		JLabel label2 = new JLabel("ABCD");
    		buttonTwo.add(BorderLayout.NORTH, label1);
    		buttonTwo.add(BorderLayout.CENTER, label2);
    		buttonTwo.setHorizontalAlignment(buttonTwo.CENTER);
    
    		JButton buttonThree = new JButton("3 \nDEF");
    		JButton buttonFour = new JButton("4 \nGHI");
    		JButton buttonFive = new JButton("5 \nJKL");
    		JButton buttonSix = new JButton("6 \nMNO");
    		JButton buttonSeven = new JButton("7 \nPQRS");
    		JButton buttonEight = new JButton("8 \nTUV");
    		JButton buttonNine = new JButton("9\n WXYZ");
    		JButton buttonStar = new JButton("*");
    		JButton buttonZero = new JButton("0");
    		JButton buttonPound = new JButton("#\n space");
        
        	panel.setLayout(new BorderLayout());
           	buttonPanel.setLayout(new GridLayout(5, 3));
            
           	// Won't let me add an ActionListener for each button?
        	buttonPanel.add(buttonSend);
        	// buttonSend.addActionListener(this);
        	
        	buttonPanel.add(buttonBlank);
        	// buttonBlank.addActionListener(this);
        	
        	buttonPanel.add(buttonArrow);
        	// buttonArrow.addActionListener(this);
            
        	buttonPanel.add(buttonOne);
        	// buttonOne.addActionListener(this);
            
        	buttonPanel.add(buttonTwo);
        	// buttonTwo.addActionListener(this);
            
        	buttonPanel.add(buttonThree);
        	//buttonThree.addActionListener(this);
            
        	buttonPanel.add(buttonFour);
        	buttonPanel.add(buttonFive);
        	buttonPanel.add(buttonSix);
        	buttonPanel.add(buttonSeven);  
        	buttonPanel.add(buttonEight);
           	buttonPanel.add(buttonNine);
        	buttonPanel.add(buttonStar);
        	buttonPanel.add(buttonZero);
            buttonPanel.add(buttonPound);  
           
            panel.add(buttonPanel, BorderLayout.CENTER);
            panel.add(display, BorderLayout.NORTH);
            frame.getContentPane().add(panel);
            frame.setSize(500,600); 
        }
    	
    	public static void main(String[] args)
    	{
    		
    		TextMessage textGui = new TextMessage(); 
    		textGui.go(); 
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: GUI Button text problem

    Try using html
    Java Code:
    new JButton("<html><p>The following should be a</p><p>New Line</p></html>");
    You should be able to center the text using css styling...eg

    <p style="text-align:center">

  3. #3
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: GUI Button text problem

    Quote Originally Posted by doWhile View Post
    Try using html
    Java Code:
    new JButton("<html><p>The following should be a</p><p>New Line</p></html>");
    You should be able to center the text using css styling...eg

    <p style="text-align:center">
    Hmm, I'm not sure we're allowed to do that. It's my first Java class :/

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: GUI Button text problem

    Quote Originally Posted by simon.black325 View Post
    Hmm, I'm not sure we're allowed to do that. It's my first Java class :/
    Well if you have restrictions, you should lay them out on the table. Do the restrictions include getting help online? ;)

  5. #5
    Join Date
    Sep 2011
    Posts
    18
    Rep Power
    0

    Default Re: GUI Button text problem

    Quote Originally Posted by doWhile View Post
    Well if you have restrictions, you should lay them out on the table. Do the restrictions include getting help online? ;)
    Haha no they encourage us to get help actually. But I would just assume to stick to things they have taught us, and this section is just learning how to make GUI's

Similar Threads

  1. unicode ok for text pane but not for button label
    By madroadbiker in forum Advanced Java
    Replies: 6
    Last Post: 06-01-2011, 05:58 PM
  2. Assigning text in JTable by clicking a button
    By Aiquoc in forum New To Java
    Replies: 3
    Last Post: 06-01-2011, 03:29 PM
  3. Button text to specific SQL reference
    By Beck123 in forum NetBeans
    Replies: 1
    Last Post: 01-13-2011, 04:36 PM
  4. How can I change text for paginate button?
    By robot in forum NetBeans
    Replies: 0
    Last Post: 11-20-2008, 06:04 AM
  5. problem aligning both image/text inside a button
    By rick_cols in forum SWT / JFace
    Replies: 0
    Last Post: 09-18-2008, 08:20 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
  •