Results 1 to 5 of 5
  1. #1
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default JButtons expanding when screen is resized

    Hi i'm new here and pretty new to java(about to finish 1st semester in AP CS at my HS), which is probably why i'm having a dilemma with a simple answer. I'm creating a hangman game as my own little project: I have the JFrame with two JPanels in it(top for drawing the hanging, bottom for text and stuff), and i'm adding 26 JButtons(letters) in the bottom panel and I finally got them positioned right, but whenever I drag the corner of the screen to make it bigger my JButtons turn into giant columns that take up the whole screen.(And my panels don't expand with the screen which is another minor problem).

    My main class:
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    import java.io.*;
    
    public class Hangman {
    	private static JFrame frame;
    	private static String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k",
    			"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    	private static JButton[] buttonArr= new JButton[26];
    	
    	public static void main(String[] args) {
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(700, 500);
    		frame.setTitle("Simple Hangman");
    		frame.setVisible(true);
    		
    		HangmanPanel hPanel = new HangmanPanel();
    		hPanel.setSize(700, 300);
    		hPanel.setBounds(0, 0, 700, 300);
    		hPanel.setBackground(new Color(30, 144, 255));
    		
    		TextPanel tPanel = new TextPanel(new GridLayout());
    		tPanel.setSize(700, 200);
    		tPanel.setBounds(0, 30, 700, 200);
    		tPanel.setBackground(Color.DARK_GRAY);
    		frame.add(hPanel);
    		frame.add(tPanel);
    		
    		hPanel.setWrong(6);
    		
                    //initializing and placing the buttons in the panel
    		for(int i = 0; i < 12; i++){
    			buttonArr[i] = new JButton(alphabet[i]);
    			tPanel.add(buttonArr[i]);
    			buttonArr[i].setBounds(i*50, 380, 50, 40);
    			buttonArr[i].setSize(50, 40);
    		}
    		for(int i = 13; i < buttonArr.length; i++){
    			buttonArr[i] = new JButton(alphabet[i]);
    			tPanel.add(buttonArr[i]);
    			buttonArr[i].setBounds((i-13)*50, 420, 50, 40);
    			buttonArr[i].setSize(50, 40);
    		}
    		
    	}
    	
    }
    Top panel for drawing:
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class HangmanPanel extends JPanel{
    	private BufferedImage img;
    	private int wrong;
    	
    	public HangmanPanel(){
    		wrong = 0;
    	}
    	
    	public void setWrong(int x){
    		wrong = x;
    	}
    	
    	public void readImage(){
    		try{
    			img = ImageIO.read(new File("lardog.jpg"));
    		}catch (IOException ex){
    			ex.printStackTrace();
    		}
    	}
    	
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(Color.BLACK);
    		int[] x = {362, 392, 412, 442};
    		int[] y = {230, 220, 220, 230};
    		g.fillPolygon(x, y, 4);
    		g.fillRect(400, 70, 5, 150);
    		g.fillRect(300, 70, 100, 5);
    		g.fillRect(300, 70, 5, 20);
    		
    		if(wrong >= 1){
    			g.setColor(Color.GREEN);
    			g.fillOval(280, 75, 40, 40);
    			g.setColor(Color.BLACK);
    			g.drawOval(290, 85, 7, 6);
    			g.drawOval(305, 85, 7, 6);
    			g.fillOval(290, 85, 4, 4);
    			g.fillOval(305, 85, 4, 4);
    			g.drawOval(290, 98, 20, 8);
    		}
    		if(wrong >= 2){
    			g.drawLine(300, 115, 300, 195);
    		}
    		if(wrong >= 3){
    			g.drawLine(300, 145, 330, 140);
    		}
    		if(wrong >= 4){
    			g.drawLine(300, 145, 270, 140);
    		}
    		if(wrong >= 5){
    			g.drawLine(300, 195, 320, 210);
    		}
    		if(wrong >= 6){
    			g.drawLine(300, 195, 280, 210);
    		}
    		if(wrong >= 7){
    			readImage();
    			g.drawImage(img, 280, 76, null);
    		}
    	}
    }
    Bottom panel for buttons and text:
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class TextPanel extends JPanel{
    	
    	public TextPanel(LayoutManager l){
    		setLayout(l);
    	}
    }
    I only started it yesterday so it's really rough.

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

    Default Re: JButtons expanding when screen is resized

    You'll want to read up on how to use the layout managers at the Swing tutorials as this will show you how you're using them wrong, and how to use them correctly. You'll also want to get rid of all the setBounds calls in your code and instead let the layout managers set the sizes of your components for you.

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

    Default Re: JButtons expanding when screen is resized

    For instance I'll give you a hint. A JFrame's contentPane uses BorderLayout by default, and so when you add a component to a JFrame, you are actually adding it to its contentPane and by default at the BorderLayout.CENTER position. So when you add to comopnents to the JFrame, they both get added to that same position. Even though you set the bounds of these components, they will try to fill the whole JFrame's contentPane, and the one added last will eventually cover the one added previously. The solution is not set bounds and not to use layouts in such a simplistic manner but rather to use other layout managers and to nest containers (JPanels) each using its own layout managers.

  4. #4
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: JButtons expanding when screen is resized

    I see what you mean. I read up a little on them and didn't really see what I was trying to achieve, so I tried the bounds, which was the lazy way out. But right after I posted I removed the GridLayout and now everything seems to work fine haha. Is that bad?

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

    Default Re: JButtons expanding when screen is resized

    Quote Originally Posted by Bestsanchez View Post
    I see what you mean. I read up a little on them and didn't really see what I was trying to achieve, so I tried the bounds, which was the lazy way out. But right after I posted I removed the GridLayout and now everything seems to work fine haha. Is that bad?
    Well, it's probably OK in the short run, but in the long run, you'll want to learn to use the layout managers well as they are the key to creating easily maintained and functioning GUI's. Imagine creating a complex GUI by hand with setBounds only to later realize that you left out an additional JRadioButton in the middle of the GUI. If you manually set the location of everything, you'll have to manually re-set the locations just to add in one more component. If you instead you used layouts correctly, you wouldn't have to do anything special to add in the extra component, just add it. Then there's the issue of GUI appearance on other OS's and screen resolutions that layout managers will help you with, and I can go on and on...

Similar Threads

  1. JPanel expanding
    By fam2315 in forum AWT / Swing
    Replies: 1
    Last Post: 10-05-2011, 11:18 PM
  2. Replies: 11
    Last Post: 08-13-2011, 06:27 AM
  3. Replies: 4
    Last Post: 04-27-2011, 08:50 AM
  4. JTree not expanding?
    By captain alge in forum New To Java
    Replies: 1
    Last Post: 04-14-2011, 05:27 PM
  5. JFrame expanding to fit content
    By pcman312 in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 08:09 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
  •