Results 1 to 3 of 3
  1. #1
    jbrasileiro is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Question Ploblems with Interface/JFrame - LayoutManager

    I'm here because I'm trying to create an interface with some components they are already in the interface and grouped as I would like to.
    But I've been facing some difficulties:
    I couldn't set the size of the first JButton, I would like to set it to a small/compacted square.
    Also I would like to know what the best solution to solve the problem when I maximize the frame, that results in the deconfiguration
    of the field's position.
    Should I at least define the minimum size of the frame?
    Main Class
    Java Code:
    public class TesteInterface {
    	public static void main(String[] args) {
    		new Interface();
    	}
    
    }

    Java Code:
    package uni.TeoInf.GA;
    
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Interface extends JFrame {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -5973439426236872140L;
    	JButton btnEncode = new JButton("DoIt");
    	JButton btnDecode = new JButton("DDoItII");
    	JButton btnSeachPathE = new JButton("Search...");
    	JButton btnSeachPathD = new JButton("Search...");
    	
    	Dimension DimensionLongText = new Dimension(200,15);
    	Dimension DimensionSquareButton = new Dimension(10,10);
    	
    	JLabel lblPathfile2E = new JLabel("Path of file: ", JLabel.RIGHT);
    	
    	JTextField txtPathFile2E = new JTextField(15);
    	JTextField txtPathFile2D = new JTextField(15);
    	JTextField txtParameter = new JTextField(3);
    	
    	
    	JCheckBox chkboxParameter = new JCheckBox("Parameter: ");
    	
    	JTextArea txtBoxResult = new JTextArea(30,35);
    
    	public JPanel getH1(){
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(1,3));
    		p.add(lblPathfile2E);
    		p.add(txtPathFile2E);
    		txtPathFile2E.setPreferredSize(DimensionLongText);
    		btnSeachPathE.setPreferredSize(DimensionSquareButton);
    		p.add(btnSeachPathE);
    		
    		return p;
    	}
    	public JPanel getH2(){
    		JPanel p = new JPanel();
    		p.setLayout(new GridLayout(1,2));
    		p.add(chkboxParameter);
    		p.add(txtParameter);
    		return p;
    	}
    	public JPanel getH3(){
    		JPanel p = new JPanel();
    		//p.setLayout(new GridLayout(2,1));
    		p.setLayout(new FlowLayout());
    		p.add(btnEncode);
    		return p;
    	}
    	
    	public JPanel getH4(){
    		JPanel p = new JPanel();
    		p.setLayout(new FlowLayout());
    		p.add(txtBoxResult);
    		return p;
    	}
    	public Interface(){
    		JFrame frame = new JFrame("Golomb Encoding/Decoding");
    		JTabbedPane tab = new JTabbedPane();
    		frame.add(tab);
    		
    		JPanel Mainpanel = new JPanel();
    		//setLayout(new FlowLayout());
    		//Mainpanel.setLayout(new GridLayout(4,1));
    		//Mainpanel.setLayout(new FlowLayout());
    		Mainpanel.add(getH1());
    		Mainpanel.add(getH2());
    		Mainpanel.add(getH3());
    		Mainpanel.add(getH4());
    		
    		tab.add("Enconding", Mainpanel);
    		//Second tab
    		JPanel panel1 = new JPanel();
    		JButton btnPathDec = new JButton("Path");
    		//button1.addActionListener(new MyAction());
    		panel1.add(txtPathFile2D);
    		panel1.add(btnPathDec);
    		tab.add("Remove Tab", panel1);
    		
    		
    		frame.pack();
    		frame.setSize(450, 700);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }

  2. #2
    AndrewC is offline Member
    Join Date
    Apr 2012
    Location
    Ireland
    Posts
    5
    Rep Power
    0

    Default Re: Ploblems with Interface/JFrame - LayoutManager

    For your button what i do is

    button.setPreferredSize(new Dimension(80, 80));

    where the values are the x, y sizes.

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

    Default Re: Ploblems with Interface/JFrame - LayoutManager

    No -- don't set the size of anything, and avoid setting preferred size as well if at all possible. Instead, just let the layout set the best sizes for you. For e.g.,

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    
    public class Interface2 {
       private static void createAndShowUI() {
          JPanel topPanel = new JPanel(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          int gap = 7;
          gbc.insets = new Insets(gap,0,gap,gap);
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          gbc.fill = GridBagConstraints.HORIZONTAL;
          gbc.anchor = GridBagConstraints.EAST;
          topPanel.add(new JLabel("Fubar:", JLabel.RIGHT), gbc);
          gbc.gridx = 1;
          gbc.anchor = GridBagConstraints.CENTER;
          topPanel.add(new JTextField(10), gbc);
          gbc.gridx = 2;
          gbc.insets = new Insets(gap,0,gap,0);
          topPanel.add(new JButton("WTF"), gbc);
          
          gbc.gridx = 0;
          gbc.gridy = 1;
          gbc.insets = new Insets(gap,0,gap,0);
          gbc.anchor = GridBagConstraints.EAST;
          topPanel.add(new JRadioButton("DILLYGAF:"), gbc);
          gbc.gridx = 1;
          gbc.anchor = GridBagConstraints.CENTER;
          gbc.insets = new Insets(gap,0,gap,gap);
          topPanel.add(new JTextField(10), gbc);
          gbc.gridx = 2;
          gbc.insets = new Insets(gap,0,gap,0);
          topPanel.add(new JButton("Don't Ask Me"), gbc);
          
          JPanel mainPanel = new JPanel(new BorderLayout(gap, gap));
          mainPanel.add(topPanel, BorderLayout.NORTH);
          mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
          mainPanel.add(new JScrollPane(new JTextArea(30, 30)));
          
          JFrame frame = new JFrame("Fubarlicious!");
          frame.getContentPane().add(mainPanel);
          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();
             }
          });
       }
    }

Similar Threads

  1. What is calling LayoutManager.layoutContainer?
    By kreyszig in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2011, 03:09 PM
  2. question about LayoutManager
    By temyong in forum AWT / Swing
    Replies: 2
    Last Post: 12-30-2010, 03:04 PM
  3. LayoutManager Best Choice
    By 67726e in forum AWT / Swing
    Replies: 2
    Last Post: 11-10-2010, 02:59 AM
  4. Changing buttonsizes in layoutmanager
    By frederic202 in forum AWT / Swing
    Replies: 2
    Last Post: 03-23-2010, 06:08 PM
  5. What layoutmanager is suitable for forms.
    By Somelauw in forum AWT / Swing
    Replies: 9
    Last Post: 12-05-2009, 01:04 PM

Tags for this Thread

Posting Permissions

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