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);
    }}
以上为源码,请问怎么做,我自己尝试加上去的滚动条无法贴在边缘,不知道为什么

解决方案 »

  1.   

    建议在FlowLayoutPanel中添加个Java-API的javax.swing 类 JScrollPane;具体参考API文档;
      

  2.   

    JScrollPane js = new JScrollPane(panel);
    可以为panel添加滚动条
      

  3.   

    把代码中的add(flowpanel);
    改成JScrollPane js=new JScrollPane(flowpanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(js);
    因为你的窗口布局是setFlowLayoutPanel();所以只能是水平有滚动条。
    确定垂直滚动条何时显示在滚动窗格上。合法值是: 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
    确定水平滚动条何时显示在滚动窗格上。选项有:
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED 
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER 
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS