我的JFrame

时间:2016-03-21 07:20:40

标签: java swing

我已将3 JPanels添加到Jframe。那些JPanels包含O +图像和标签。

当我尝试显示JFrame时,它没有正确显示所有元素。请帮忙

        JButton next=new JButton("next");
        next.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {}
            @Override
            public void mousePressed(MouseEvent e) {}
            @Override
            public void mouseExited(MouseEvent e) {}
            @Override
            public void mouseEntered(MouseEvent e) {}
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("mouse next clicked");
                 nextAction(n, tempList);
            }
        });

        JButton ok= new JButton("ok");
        JLabel pageLabel= new JLabel("page"+x);
        btnPanel.add(pre);
        btnPanel.add(pageLabel);
        btnPanel.add(next);
        okBtn.add(ok,BorderLayout.PAGE_END);
        layout.setHgap(10);
        layout.setVgap(10);
        extPanel.setSize(50,50);
        extPanel.setLayout(layout);

        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new GridLayout());
        //frame.setLayout(new BorderLayout());

        frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
        frame.add(extPanel);
        frame.add(btnPanel);
        frame.add(okBtn,BorderLayout.PAGE_END);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

2 个答案:

答案 0 :(得分:1)

假设您提到的三个面板是btnPanel,okBtn(?)和extPanel,并且包含一个您忘记在上面复制的预按钮,如果我启动您的代码 - 使用SwingUtilities.invokeLater,如npinti评论 - ,我获得一个小窗口。

enter image description here

我已在您的代码中通过替换 frame 。我的主要类Test来自JFrame。

你的意思是"正确"?

答案 1 :(得分:0)

        Statement st = null;

        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/jframe";
        String user = "root";
        String password = "root";

        ArrayList<JPanel> panelList =new ArrayList<JPanel>();


            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            String query ="SELECT * FROM sample";
            rs =  st.executeQuery(query);
            int k= 1; 
    if(rs.next()){

        do{

            JPanel panel1= new JPanel();
            String s1= rs.getString("id");
            String s2= rs.getString("name");
            String s3= rs.getString("path");
            String s4= rs.getString("duration");
            String s5= rs.getString("date");

            ImageIcon image = new ImageIcon(s3);
            JLabel idLabel= new JLabel(s1);
            JLabel nameLabel= new JLabel(s2);
            JLabel imageLabel = new JLabel("", image, JLabel.CENTER);
            //JLabel imageLabel= new JLabel(image);
            //imageLabel.setSize(100,100);

           //imageLabel.setOpaque(true);
            JLabel durationLabel= new JLabel(s4);
            JLabel dateLabel= new JLabel(s5);
            panel1.add(idLabel);
            panel1.add(imageLabel);
            panel1.add(nameLabel);
            panel1.add(durationLabel);
            panel1.add(dateLabel);
            panel1.setLayout(new FlowLayout());
            panel1.setName("panel"+k);
            //panel1.setSize(10,10);
            //panel1.setBorder(BorderFactory.createLineBorder(Color.blue, 4, true));
            panel1.setBorder(blueBorder);
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
            final JPanel tempPanel= panel1;

            panel1.addMouseListener(new MouseListener() {

                @Override
                public void mouseReleased(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mousePressed(MouseEvent me) {


                }

                @Override
                public void mouseExited(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mouseEntered(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void mouseClicked(MouseEvent me) {
                    JPanel test= (JPanel)me.getComponent();
                    MainClass.panelColor(test);

                }
            });
            panelList.add(panel1);
            panelList.size();
            k++;
        }while(rs.next());

    }//if
    int i;
    do{
    for (i=0;i<4;i++){
        extPanel.add(panelList.get(i));     
    }//for
    int x=1;
    display(panelList,extPanel,x);
    }while(1<0);


    }catch(Exception e){
        System.out.println(e);
    }//catch
}
public static void nextAction(int n,ArrayList<JPanel> panelList){
    try{
    System.out.println("next action executed");
     extPanel.removeAll();
     btnPanel.removeAll();
     okBtn.removeAll();
    int i;
    for(i=4*n;i<4*(n+1);i++){
        extPanel.add(panelList.get(i)); 
        //Panel2 pan= new Panel2(extPanel);
    }

    int x=n+1;
    display(panelList, extPanel, x);
    }catch(Exception e){
        System.out.println(e);
    }
}   

public static void preAction(int n,ArrayList<JPanel> panelList){
    try{
    System.out.println("next action executed");
     extPanel.removeAll();
     btnPanel.removeAll();
     okBtn.removeAll();
    int i;
    for(i=(n-1)*4;i<4*n;i--){
        extPanel.add(panelList.get(i)); 
    }

    int x=n-1;
    display(panelList, extPanel, x);
    }catch(Exception e){
        System.out.println(e);
    }
}
public static void display(ArrayList<JPanel> list,JPanel extPanel,int x){

    final int n=x;
    int temp=1;
    final ArrayList<JPanel> tempList= list;
    JButton pre=new JButton("previous");
    if(x==1) {
        pre.setVisible(false);
    }
    pre.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}         
        @Override
        public void mouseEntered(MouseEvent arg0) {}

        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.out.println("mouse pre clicked");
            preAction(n, tempList); 
        }
    });


    JButton next=new JButton("next");
    next.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("mouse next clicked");
             nextAction(n, tempList);
        }
    });
    JButton ok= new JButton("ok");
    JLabel pageLabel= new JLabel("page"+x);
    btnPanel.add(pre);
    btnPanel.add(pageLabel);
    btnPanel.add(next);
    okBtn.add(ok,BorderLayout.PAGE_END);
    layout.setHgap(10);
    layout.setVgap(10);
    extPanel.setSize(50,50);
    extPanel.setLayout(layout);

    //frame.setLayout(new FlowLayout());
    GridLayout gr= new GridLayout(0,1);
    frame.setLayout(gr);

    //frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
    frame.add(extPanel);
    frame.add(btnPanel);
    frame.add(okBtn,BorderLayout.PAGE_END);

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    temp=x;
    }//method
public static JPanel expMethod(JPanel jp){
    jp.setBorder(BorderFactory.createLineBorder(Color.red, 4, true));
    return jp;
}