Placing a JLabel at a specific x,y coordinate on a JPanel

前端 未结 6 637
梦毁少年i
梦毁少年i 2020-12-05 16:12

I\'m trying to place a series of JLabels at specific X and Y coordinates on a JPanel (and set its height and width, too). No matter what I do, each label winds up immediatel

6条回答
  •  一整个雨季
    2020-12-05 16:46

    You can use your own method that calling by setSize, setLocation values for directly....! ` As well i show you how to use JProgress Bar

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    class installComp{
        void install(Component comp, int w, int h, int x, int y){
            comp.setSize(w,h);
            comp.setLocation(x,y);
        }
    }
    class MyFrame extends JFrame{
        int cur_val = 0;
        JButton btn = new JButton("Mouse Over");
        JProgressBar progress = new JProgressBar(0,100);
        MyFrame (){
            installComp comp=new installComp();
    
            comp.install(btn,150,30,175,20);
            comp.install(progress,400,20,50,70);
    
            btn.addMouseListener(new MouseAdapter(){
                public void mouseEntered(MouseEvent evt){
                    cur_val+=2;
                    progress.setValue(cur_val);
                    progress.setStringPainted(true);
                    progress.setString(null);
                }
            }); 
            add(btn);
            add(progress);
            setLayout(null);    
            setSize(500,150); 
            setResizable(false);
            setDefaultCloseOperation(3);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    }
    class Demo{
        public static void main(String args[]){
            MyFrame f1=new MyFrame();   
        }
    }
    

提交回复
热议问题