0

When i add another JPanel into frame previous will dissapper (or probably overlaps the previous one). How can i stop this overlapping?

public Attack() {
    JFrame frame = new JFrame("Oracle Padding Attack");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel1.setLayout(null);
    JLabel label1 = new JLabel("Inicialization vector:");
    createTextField(10, 40, arrayIV, panel1, "HH", true, false);
    label1.setBounds(10, 0, 120, 50);
    panel1.add(label1);

    panel2.setLayout(null);
    JLabel label2 = new JLabel("Encrypted text:");
    createTextField(400, 40, encryptedTextArray, panel2, "00", true, false);
    label2.setBounds(400, 0, 120, 50);
    panel2.add(label2);

    frame.add(panel1);
    frame.add(panel2);
    frame.setSize(900, 400);
    frame.setVisible(true);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DahakkaSVK
  • 25
  • 4
  • 1
    You need to use a [Layout Manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – gla3dr Apr 29 '14 at 22:03
  • @gla3dr - It's recommended, but not required. I will say, though, that it's easier to use a LayoutManager if you're building a desktop app. – jefflunt Apr 29 '14 at 22:05
  • 1
    @jefflunt In my experience, using a null layout is _always_ a bad idea. – gla3dr Apr 29 '14 at 22:06
  • @gla3dr - I was just softening the response a bit. Generally I agree, but I won't go so far as to say *always*. But yes, 99% of the time it's a terrible, horrible, idea that's barely better than committing suicide. Not that LayoutManagers aren't a completely half-baked idea on their own, but unless you're writing an app that you can guarantee will run at a fixed resolution everywhere (which is pretty much impossible unless it's an embedded solution) then LayoutManagers are a necessary evil for Java desktop apps. On the other hand...there's always web development, but CSS is its own hell. – jefflunt Apr 29 '14 at 23:53
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Apr 30 '14 at 02:31

1 Answers1

3

It depends on how you want your two panels placed inside your frame. You will need a layout for your frame. If you want to be able to "jump" from one to the other, cardLayout is your answer.

Otherwise if you want both side to side for example, you will have to use a layout inside your frame. I like the MigLayout, but GridLayout will do the job just fine.

http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

All build in layout handlers can be found here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Also it's not recommended to use null layout at all since it will break the look if the window is resized.

Jakkra
  • 641
  • 8
  • 25
  • *"..not recommended to use null layout at all since it will break the look if the window is resized."* Or if the direction of the breeze changes. Really, there are many factors that can break a `null` layout. – Andrew Thompson Apr 30 '14 at 02:33