微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自动调整 Jframe 中 Jcomponent 的大小

如何解决自动调整 Jframe 中 Jcomponent 的大小

我正在使用 java swing 创建应用程序的 UI。我希望 JFrame 中的 Jcomponents 在我增加或减少 JFrame 的大小时自动调整大小。现在使用我的代码,应用程序以全屏方式打开,但是当我尝试增加或减少框架的大小时,组件的大小保持不变。

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);}
      
        }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。