0

The program i want to make is Divide an applet/frame(awt) into two parts using panel. The first panel contains four buttons naming ellipse, rectangle, circle and triangle. Taking one button and asking coordinates and make a figure on the other panel.

Please someone explain me the concept or working because im not good at layouts and know methods to use to convey from one panel to other.. Thank you very much

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
amber
  • 31
  • 9
  • Your question is a bit broad, perhaps too broad for this site. I suggest that you break down your problem into its constituent steps and then try to solve each one individually. These steps could be: create a JPanel that holds two JPanels, deciding on which layout to use, responding to a JButton,... As for "not good at layouts" -- that's what the tutorials are for. Please have a look at them first before coming here. If you do this, you should be able to ask a much more specific question about your point of confusion. – Hovercraft Full Of Eels Dec 16 '14 at 18:15
  • You can find links to the Swing tutorials and other Swing resources here: [Swing Info](http://stackoverflow.com/questions/tagged/swing) – Hovercraft Full Of Eels Dec 16 '14 at 18:17
  • 1
    Use a `JSplitPane`, as shown in this [duplicate](http://stackoverflow.com/q/11942961/230513). – trashgod Dec 16 '14 at 20:29

1 Answers1

0

I would recommend starting out with a layout manager.. if you want 2 JPanels next to each other, you can use the GridLayout layout manager. It takes 2 arguments, and one of it's overloaded constructors takes 4 arguments.

setLayout(new GridLayout(rows, columns)); //one commonly used constructor
setLayout(new GridLayout(rows, columns, horizontalSpacePixels, verticleSpace));

GridLayout, when used will reshape to fit the largest component, and make each part of the grid an equal size-- however this isn't the case when you use a GridLayout inside of a GridLayout (the inner GridLayout might be too big to fit within the confines that the outer GridLayout puts on it.). If I simply do

JFrame jf = new JFrame("Laying the grid out");
jf.setLayout(new GridLayout(5, 5));
JPanel[] jp = new JPanel[25];
JLabel[] jl = new JLabel[25];
for(int i = 0; i < 25; i++) {
    jp[i] = new JPanel();
    jp[i].setBackground(Color.YELLOW);
    jl[i] = new JLabel("This is label no. " + (i+1));
    jp[i].add(jl[i]);
}
//now to add all 25 components in the 5x5 grid; you simply add them, and it
//automatically positions the jpanels in the order that you place them.. left to right.
for(int i = 0; i < 25; i++)
    jf.add(jp[i]);

Here is an example program that involves a simple GridLayout, and an actionListener that responds to button events by changing one of the JPanel's color.

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui {
    private JPanel p2;
    private JLabel side2;
    private JFrame jf;
    public static void main(String[] args){
        new Gui();
    }
    public Gui(){
        jf = new JFrame("Holds 2 panels side by side.");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLayout(new GridLayout(1, 2));
        JPanel p1 = new JPanel();
        p2 = new JPanel();
        p1.setBackground(Color.BLACK);
        p2.setBackground(Color.BLACK);
        p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
        JLabel[] space = new JLabel[20];
        for(int i = 0; i < 20; i++)
            space[i] = new JLabel(" ");
        JButton jb1 = new JButton("Button 1");
        JButton jb2 = new JButton("Button 2");
        jb1.addActionListener(new BListen());
        jb2.addActionListener(new BListen());
        p1.add(space[0]);
        p1.add(jb1);
        p1.add(space[1]);
        p1.add(jb2);
        p1.add(space[2]);
        jf.add(p1);
        side2 = new JLabel("Change the color here with the buttons there.");
        side2.setForeground(Color.GREEN);
        p2.add(side2);
        jf.add(p2);
        jf.setSize(600, 200);
        jf.setVisible(true);
    }
    private class BListen implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonClicked = e.getActionCommand();
            if(buttonClicked.equals("Button 1")) {
                JOptionPane.showMessageDialog(null, "You pressed Button 1.");
                p2.setBackground(Color.BLUE);
                side2.setForeground(Color.MAGENTA);
                jf.setVisible(true);
        }
            else if(buttonClicked.equals("Button 2")) {
                JOptionPane.showMessageDialog(null, "You pressed Button 2.");
                p2.setBackground(Color.ORANGE);
                side2.setForeground(Color.DARK_GRAY);
                jf.setVisible(true);
            }
        }
    }
}
Woodrow
  • 136
  • 2
  • 10