I am using java swing to create the UI of my application. I want my Jcomponents inside my JFrame to auto-resize as I increase or decrease the size of my JFrame. right now with my code, the application opens up in full screen but when I try to increase or decrease the size of my frame the size of the components remains constant.
public class projecta implements ActionListener{
JPanel panel, panela;
JLabel l1,l2,l3,l4;
JButton b,b2;
JTextArea area;
String out,er;
Dimension cc= Toolkit.getDefaultToolkit().getScreenSize();
int cw= cc.width;
int ch= cc.height;
Font font = new Font("tahoma", Font.PLAIN, ((int)Math.round(cw*0.018)));
public void ui(){
GraphicsEnvironment graphics = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = graphics.getDefaultScreenDevice();
JFrame f= new JFrame ();{
panel = new JPanel ();{
int px=(int)Math.round(cw*0.050);
int py=(int)Math.round(ch*0.080);
int pw=(int)Math.round(cw*0.866);
int ph= (int)Math.round(ch*0.400);
panel.setBounds(px,py,pw,ph);
panel.setBackground(Color.white);
l1= new JLabel();{
l1= new JLabel("Enter your Gherkin steps");
int l1x=(int)Math.round(cw*0.058);
int l1y=(int)Math.round(ch*0.090);
int l1w=(int)Math.round(cw*0.400);
int l1h= (int)Math.round(ch*0.030);
l1.setBounds(l1x,l1y,l1w,l1h);
l1.setForeground(Color.decode("#2A3F54"));
f.add(l1);
}
l4= new JLabel();{
int l4x=(int)Math.round(cw*0.166);
int l4y=(int)Math.round(ch*0.420);
int l4w=(int)Math.round(cw*0.166);
int l4h= (int)Math.round(ch*0.030);
l4.setBounds(l4x,l4y,l4w,l4h);
l4.setForeground(Color.red);
f.add(l4);
}
b= new JButton("Format");{
int bx=(int)Math.round(cw*0.058);
int by=(int)Math.round(ch*0.420);
int bw=(int)Math.round(cw*0.083);
int bh= (int)Math.round(ch*0.040);
b.setBounds(bx,by,bw,bh);
b.setBackground(Color.decode("#1ABB9C"));
b.setForeground(Color.white);
b.addActionListener(this);
f.add(b);
}
area= new JTextArea();
int areax=(int)Math.round(cw*0.061);
int areay=(int)Math.round(ch*0.130);
int areaw=(int)Math.round(cw*0.841);
int areah= (int)Math.round(ch*0.250);
area.setBounds(areax,areay,areaw,areah);
area.setBackground(Color.decode("#F3F3F3"));
f.add(area);
panela = new JPanel ();{
int pax=(int)Math.round(cw*0.050);
int pay=(int)Math.round(ch*0.540);
int paw=(int)Math.round(cw*0.866);
int pah= (int)Math.round(ch*0.200);
panela.setBounds(pax,pay,paw,pah);
panela.setBackground(Color.WHITE);
l2 = new JLabel("Generate Step Definition");
int l2x=(int)Math.round(cw*0.058);
int l2y=(int)Math.round(ch*0.550);
int l2w=(int)Math.round(cw*0.333);
int l2h= (int)Math.round(ch*0.030);
l2.setBounds(l2x, l2y, l2w, l2h);
l2.setForeground(Color.decode("#2A3F54"));
f.add(l2);
l3 = new JLabel("Select Language");
int l3x=(int)Math.round(cw*0.058);
int l3y=(int)Math.round(ch*0.690);
int l3w=(int)Math.round(cw*0.333);
int l3h= (int)Math.round(ch*0.030);
l3.setBounds(l3x, l3y, l3w, l3h);
l3.setForeground(Color.decode("#707070"));
f.add(l3);
String lang[] = {"JAVA","C#","PYTHON"};
JComboBox<String> cb= new JComboBox<>(lang);
int cbx=(int)Math.round(cw*0.166);
int cby=(int)Math.round(ch*0.690);
int cbw=(int)Math.round(cw*0.083);
int cbh= (int)Math.round(ch*0.036);
cb.setBounds(cbx, cby, cbw, cbh);
f.add(cb);
b2= new JButton("Generate");
int b2x=(int)Math.round(cw*0.333);
int b2y=(int)Math.round(ch*0.690);
int b2w=(int)Math.round(cw*0.083);
int b2h= (int)Math.round(ch*0.036);
b2.setBounds(b2x,b2y,b2w,b2h);
b2.setBackground(Color.decode("#1ABB9C"));
b2.setForeground(Color.white);
f.add(b2);
}
f.add(panel); f.add(panela);
f.setSize(cw,ch);
f.setLayout(null);
f.setVisible(true);
f.setResizable(true);
f.getContentPane().setBackground( Color.decode("#F2FBFF"));
device.setFullScreenWindow(f);}
}
}