日期:2014-05-20  浏览次数:20713 次

如何在FlowLayoutPanel加滚动条,右边的和下边的
Java code
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

public class MainFrame extends Frame {
    private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
    private String user = "root"; // 数据库用户
    private String password = ""; // 数据库用户密码
    private Panel borderpanel = new Panel();
    private Panel flowpanel;
    private Panel gridpanel;
    private JLabel jl1 = new JLabel("序号:" +"     "+ "\t书名:" + "             " + "\t副标题:" + "                              "
            + "\t作者:" + "           " + "\t国籍:" + "       " + "\t出版社:" + "                 " + "\t价格:" + "       "
            + "\tISBN:" + "               ");
   
    
    public MainFrame(String title) throws SQLException, ClassNotFoundException {
        super(title);  
        MenuBar mb = new MenuBar();
          this.setMenuBar(mb);
          Menu m1 = new Menu("菜单");
          Menu m2 = new Menu("编辑");
          mb.add(m1);
          mb.add(m2);
          MenuItem mi1 = new MenuItem("添加一本书");
          mi1.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   UpdateDemo aa=new UpdateDemo();
                   aa.setVisible(true);
               }
              });
          
          MenuItem mi2 = new MenuItem("退出");
          mi2.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
            System.exit(0);
           }
          });
          m1.add(mi1);
          m1.add(mi2);
        setSize(1024, 768);// 窗口大小1024*768
        //setLocation(0,0);// 距离左上角图标距离100,100
        setLayout(new GridLayout(2, 1));//将窗口布局设置为网格式布局,网格的行数和列数分别是2和1.
        setFlowLayoutPanel();
        add(flowpanel);
    //flowpanel.add(jscbHort,FlowLayout.RIGHT);        
        //flowpanel.add(jscbVert, FlowLayout.RIGHT);
        borderpanel.setLayout(new BorderLayout());
        borderpanel.add(jl1, BorderLayout.NORTH);
        add(borderpanel);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void setFlowLayoutPanel() throws SQLException,
            ClassNotFoundException {
        flowpanel = new Panel();
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);//设置图标靠左
        flowpanel.setLayout(layout);
        Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
        Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
        String sqlStr = "select * from user"; // SQL查询语句
        Statement st = con.createStatement(); // 获取PreparedStatement对象
        ResultSet rs = st.executeQuery(sqlStr); // 执行查询
        while (rs.next()) { // 遍历ResultSet
            String str=rs.getString("url");
            final String strid=rs.getString("id"); // 获取数据
            final String strtitle=rs.getString("title");
            final String strsubhead=rs.getString("subhead");
            final String strauthors=rs.getString("authors");
            final String strnationality=rs.getString("nationality");
            final String strpublisher=rs.getString("publisher");
            final String strprice=rs.getString("price");
            final String strisbn=rs.getString("isbn");
        JButton btnSmall=new JButton("");
        ImageIcon icon = new ImageIcon(str);
        btnSmall.setIcon(scaleImage(icon));
            btnSmall.setSize(100, 150);
            btnSmall.setVisible(true);
            btnSmall.invalidate();

            flowpanel.add(btnSmall);

            btnSmall.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jl1.setText("序号:" + strid + "\t书名:" + strtitle + "\t副标题:"
                            + strsubhead + "\t作者:" + strauthors + "\t国籍:"
                            + strnationality + "\t出版社:" + strpublisher
                            + "\t价格:" + strprice + "\tISBN:" + strisbn);
                }
            });

        }
    }

    private ImageIcon scaleImage(ImageIcon icon) {
        int width = icon.getIconWidth();
        // System.out.print(width);
        int height = icon.getIconHeight();
        // if (width <= 100 && height <= 150) {
        // return icon;
        // }
        Image image = icon.getImage();
        image = image.getScaledInstance(100, 150, Image.SCALE_DEFAULT);
        return new ImageIcon(image);
    }

    public static void main(String[] args) throws SQLException,
            ClassNotFoundException {
        MainFrame yf = new MainFrame("我的书架");
        yf.setVisible(true);
    }

}