Results 1 to 3 of 3
Thread: Java Swing ( GridBagLayout )
- 12-24-2012, 06:44 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 29
- Rep Power
- 0
Java Swing ( GridBagLayout )
Hello,
I am new to Java Swing and I am currently working on my own project attempting to make a GUI calculator.
Could someone please explain to me how I can resize my operator Jbuttons ( /,+,*, -) so that they are the same size as the number JButtons( 1 - 9) and Also how i can align my buttons correctly.
Thanks :)
Here is my code:
Java Code:import java.util.Stack; import java.io.*; import javax.swing.*; import java.util.Scanner; import java.awt.*; public class Calculator2{ public Calculator2(){} public void Interface(){ JFrame frame = new JFrame("Calculator 2.0"); frame.setSize(240,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu); frame.setJMenuBar(menuBar); JMenu menu2 = new JMenu("Exit"); menuBar.add(menu2); JPanel panel = new JPanel(new GridBagLayout()); frame.getContentPane().add(panel, BorderLayout.NORTH); GridBagConstraints a = new GridBagConstraints(); a.anchor = GridBagConstraints.NORTHWEST; a.insets = new Insets(2,2,2,2); JLabel label = new JLabel("hello"); a.gridx = 0; a.gridy = 0; label.setBackground(Color.lightGray); label.setOpaque(true); panel.add(label,a); a.weightx = 0.5; JButton bttn0 = new JButton("7"); a.gridx = 0; a.gridy = 1; panel.add(bttn0,a); JButton bttn1 = new JButton("8"); a.gridx = 1; a.gridy = 1; panel.add(bttn1,a); JButton bttn2 = new JButton("9"); a.gridx = 2; a.gridy = 1; panel.add(bttn2,a); JButton bttn3 = new JButton("4"); a.gridx = 0; a.gridy = 2; panel.add(bttn3,a); JButton bttn4 = new JButton("5"); a.gridx = 1; a.gridy = 2; panel.add(bttn4,a); JButton bttn5 = new JButton("6"); a.gridx = 2; a.gridy = 2; panel.add(bttn5,a); JButton bttn6 = new JButton("1"); a.gridx = 0; a.gridy = 3; panel.add(bttn6,a); JButton bttn7 = new JButton("2"); a.gridx = 1; a.gridy = 3; panel.add(bttn7,a); JButton bttn8 = new JButton("3"); a.gridx = 2; a.gridy = 3; panel.add(bttn8,a); JButton bttn9 = new JButton("0"); a.gridx = 0; a.gridy = 4; a.gridwidth = GridBagConstraints.RELATIVE; bttn9.setPreferredSize(new Dimension(92,26)); panel.add(bttn9,a); JButton btn1 = new JButton("."); a.gridx = 2; a.gridy = 4; panel.add(btn1,a); JButton btn2 = new JButton("/"); a.gridx = 3; a.gridy = 1; panel.add(btn2,a); JButton btn3 = new JButton("*"); a.gridx = 3; a.gridy = 2; panel.add(btn3,a); JButton btn4 = new JButton("-"); a.gridx = 3; a.gridy = 3; panel.add(btn4,a); JButton btn5 = new JButton("+"); a.gridx = 3; a.gridy = 4; panel.add(btn5,a); } public static void main(String[] args){ Calculator2 calc = new Calculator2(); calc.Interface(); } }
-
Re: Java Swing ( GridBagLayout )
You're forgetting the fill constraint:
Also, you're setting the JFrame's size, and you should avoid doing that but rather letting the components decide on their best size by calling pack() on the JFrame after adding components. Also you will want to call setVisible(true) on the JFrame *after* adding the components to the GUI and in fact after calling pack(), not before adding components as you're doing.Java Code:a.weightx = 0.5; a.fill = GridBagConstraints.BOTH;
- 12-24-2012, 09:39 PM #3
Similar Threads
-
Trouble displaying an image using GridBagLayout
By treeface99 in forum AWT / SwingReplies: 0Last Post: 03-15-2012, 09:52 PM -
GridBagLayout Help
By Zman3359 in forum AWT / SwingReplies: 0Last Post: 02-21-2011, 08:39 PM -
GridBagLayout
By Moncleared in forum New To JavaReplies: 1Last Post: 10-18-2009, 10:12 PM -
abt gridbaglayout
By pinky in forum AWT / SwingReplies: 1Last Post: 12-15-2008, 08:35 AM -
Java Swing GridBagLayout Problem
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 06-29-2008, 08:51 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks