public
Created

Code of layout issue - StackOverflow

  • Download Gist
test1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
package org.dchan.context;
 
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
 
public class test1 extends JFrame {
 
JPanel contentPane;
 
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test1 frame = new test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
 
/**
* Create the frame.
*/
public test1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
 
JButton btnView = new JButton("View2");
contentPane.add(btnView, BorderLayout.CENTER);
btnView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contentPane.removeAll();
contentPane.invalidate();
test2 obj = new test2();
obj.contentPane.setVisible(true);
contentPane.add(obj.contentPane);
((JPanel) contentPane).revalidate();
contentPane.repaint();
}
});
}
 
}
test2.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package org.dchan.context;
 
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
 
public class test2 extends JFrame {
 
JPanel contentPane;
 
/**
* Launch the application.
*/
public static void main(String[] args) {
 
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test2 frame = new test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
 
/**
* Create the frame.
*/
public test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
 
JLabel lblThisIsView = new JLabel("this is view 2!");
lblThisIsView.setBounds(178, 107, 110, 14);
contentPane.add(lblThisIsView,BorderLayout.CENTER);
 
JButton btnView = new JButton("View 1!");
btnView.setBounds(168, 161, 89, 23);
contentPane.add(btnView,BorderLayout.SOUTH);
 
btnView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contentPane.removeAll();
contentPane.invalidate();
test1 obj = new test1();
obj.contentPane.setVisible(true);
contentPane.add(obj.contentPane);
((JPanel) contentPane).revalidate();
contentPane.repaint();
}
});
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.