Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple Java GUI problem

Options
  • 01-05-2005 6:22pm
    #1
    Closed Accounts Posts: 680 ✭✭✭


    This is probably a very rudimentary problem, but i can't find a straight answer for it online. I have a GUI, and i want it to display its buttons in a set way, sitting 3 under a 1 line display like so

    +
    ---
    where + is just the display field, but - are each of the 3 buttons. But so far when i add the JPanel to the JFrame, i can only manipulate it to look like either this

    +-
    --
    or this
    +
    -
    -
    -

    Anybody know the answer to this? I've tried adding a second contentsPane, but apparently JFrames only accept 1 pane, and it uses the last one specified?


Comments

  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    the code that im supplying with this shows how to easily position items where ever you like.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class hjg extends JFrame
    {
    	private JPanel contentPane;
    
    	private JTextArea jTextArea2;
    	private JScrollPane jScrollPane1;
    	private JButton jButton3;
    	private JButton jButton4;
    	private JButton jButton5;
    	private JPanel jPanel1;
    
    	public hjg()
    	{
    		super();
    		initializeComponent();
    
    
    		this.setVisible(true);
    	}
    
    
    	private void initializeComponent()
    	{
    		contentPane = (JPanel)this.getContentPane();
    		jTextArea2 = new JTextArea();
    		jScrollPane1 = new JScrollPane();
    		jButton3 = new JButton();
    		jButton4 = new JButton();
    		jButton5 = new JButton();
    		jPanel1 = new JPanel();
    
    		//
    		// contentPane
    		//
    		contentPane.setLayout(null);
    		addComponent(contentPane, jPanel1, 4,4,374,263);
    		//
    		// jTextArea2
    		//
    		jTextArea2.setText("jTextArea2");
    		//
    		// jScrollPane1
    		//
    		jScrollPane1.setViewportView(jTextArea2);
    		//
    		// jButton3
    		//
    		jButton3.setText("jButton3");
    		jButton3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				jButton3_actionPerformed(e);
    			}
    
    		});
    		//
    		// jButton4
    		//
    		jButton4.setText("jButton4");
    		jButton4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				jButton4_actionPerformed(e);
    			}
    
    		});
    		//
    		// jButton5
    		//
    		jButton5.setText("jButton5");
    		jButton5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				jButton5_actionPerformed(e);
    			}
    
    		});
    		//
    		// jPanel1
    		//
    		jPanel1.setLayout(null);
    		addComponent(jPanel1, jScrollPane1, 18,11,339,132);
    		addComponent(jPanel1, jButton3, 22,177,83,28);
    		addComponent(jPanel1, jButton4, 137,177,83,28);
    		addComponent(jPanel1, jButton5, 265,176,83,28);
    		//
    		// hjg
    		//
    		this.setTitle("title");
    		this.setLocation(new Point(0, 0));
    		this.setSize(new Dimension(390, 300));
    		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	}
    
    	private void addComponent(Container container,Component c,int x,int y,int width,int height)
    	{
    		c.setBounds(x,y,width,height);
    		container.add(c);
    	}
    
    
    	private void jButton3_actionPerformed(ActionEvent e)
    	{
    
    	}
    
    	private void jButton4_actionPerformed(ActionEvent e)
    	{
    
    
    	private void jButton5_actionPerformed(ActionEvent e)
    	{
    
    
    	}
    
    
    	public static void main(String[] args)
    	{
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JDialog.setDefaultLookAndFeelDecorated(true);
    		try
    		{
    			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    		}
    		catch (Exception ex)
    		{
    			System.out.println("Failed loading L&F: ");
    			System.out.println(ex);
    		}
    		new hjg();
    	}
    
    
    }
    
    
    


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    That's probably good code, but i actually eventually managed to get gridbaglayout working, which is perfect. Thanks for posting your reply though.

    One more question, i want there to be pictures instead of buttons, as in i'm drawing my own buttons. Right now i'm using icons, but they only cover a partial area of the button themselves, leaving a different colour border, which just looks stupid. Is there any way to make the whole button an image?


  • Closed Accounts Posts: 92 ✭✭tempest


    the code that im supplying with this shows how to easily position items where ever you like.
    ......
    

    And which GUI Builder would that be from then?

    It's very little use to anyone to give them generated code anyway, but to pass it off as developed by you is even worse....

    Looks like this to me anyway, but feel free to correct me if I'm wrong.


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    Amaru wrote:
    That's probably good code, but i actually eventually managed to get gridbaglayout working, which is perfect. Thanks for posting your reply though.

    One more question, i want there to be pictures instead of buttons, as in i'm drawing my own buttons. Right now i'm using icons, but they only cover a partial area of the button themselves, leaving a different colour border, which just looks stupid. Is there any way to make the whole button an image?
    One solution is to program your own button class which handles clicks on the image using mouse events. You can also include a second image to give it a clicked look.


  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    i wasent passing it off as my own. I got this from a gui builder originally but since then i always use this way to place my items. I find it much easier to use than the other layout managers


  • Advertisement
  • Closed Accounts Posts: 92 ✭✭tempest


    i wasent passing it off as my own. I got this from a gui builder originally but since then i always use this way to place my items. I find it much easier to use than the other layout managers

    In my opinion, by not saying where it came from you said it was yours through omission.

    Don't get me wrong. GUI Builders are a great timesaver. GUI Builders are powerful tools, but the fact is that they don't write good code. They write adequate code, which "does the job", but they don't write well designed code.

    The code you supplied is not a good example of how to build a GUI Application. It's as simple as that. If you pointed to the GUI Builder that you use then that's good advice.

    I may be harsh, but bad advice is worse than none.

    It's just my opinion.....


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Imposter wrote:
    One solution is to program your own button class which handles clicks on the image using mouse events. You can also include a second image to give it a clicked look.

    How would i go about that exactly?


Advertisement