How to program java GUI?

Simple Java GUI Program?

  • I have to write a program contains two containers, one aligned left, and one aligned right. Each container must contain 3 buttons. It must use FlowLayout. Here's what I have, but it's not working. import java.awt.*; import javax.swing.*; public class Buttons extends JFrame { public Buttons() { JPanel p1 = new JPanel(); setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10)); add(new JButton("Button 1")); add(new JButton("Button 2")); add(new JButton("Button 3")); JPanel p2 = new JPanel(); setLayout(new FlowLayout(FlowLayout.RIGHT 5, 10)); add(new JButton("Button 4")); add(new JButton("Button 5")); add(new JButton("Button 6")); } public static void main(String[] args) { Buttons frame = new Buttons(); frame.setTitle("Test"); frame.setSize(625, 80); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFram… frame.setVisible(true); } } It's all aligning to the right.... It's getting overridden.. HELP! Thanks!

  • Answer:

    Both you (Witty) and Magician mistakenly set the layout twice on the contentPane rather than on the two panels. //code change for Witty: //panel w/ left layout JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10)); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 3")); setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10)); //panel with right alignment JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout(FlowLayout.RIGHT 5, 10)); p2.add(new JButton("Button 4")); p2.add(new JButton("Button 5")); p2.add(new JButton("Button 6")); //add panels to the JFrame's contentPane Container pane = getContentPane(); //putting p1 in NORTH pane.add(p1, BorderLayout.NORTH); //putting p2 in SOUTH pane.add(p2, BorderLayout.SOUTH); Now when you run it, you should be able to see the top buttons stay to the left while the bottom buttons move to the right as you stretch the JFrame horizontally.

Witty at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Fixed, changed the class name for my convenience. Note: There are a few of fundamental swing concepts that's misused in the code. I suggest taking a good look at Swing framework and relearn the layouts. import java.awt.FlowLayout; public class Calculator extends JFrame { public Calculator() { JPanel p1 = new JPanel(); getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10)); p1.add(new JButton("Button 1")); p1.add(new JButton("Button 2")); p1.add(new JButton("Button 3")); JPanel p2 = new JPanel(); p2.add(new JButton("Button 4")); p2.add(new JButton("Button 5")); p2.add(new JButton("Button 6")); getContentPane().add(p1); getContentPane().add(p2); } public static void main(String[] args) { Calculator frame = new Calculator(); frame.setTitle("Test"); frame.setSize(800, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.