0

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);}
      
        }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Don't use a null layout! Don't use setBounds(...)! Swing was designed to be used with layout managers. Layout manager can be used to automatically resize components. Read the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for the basics. Every panel can use a different layout manager to achieve your desired layout. – camickr May 18 '21 at 17:20
  • @camickr already addressed the problem, so some general tips: 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson May 19 '21 at 07:38

0 Answers0