Java滚动条选项

时间:2011-01-10 12:51:37

标签: java swing jscrollpane jscrollbar

如果可以,我希望有人帮助我使用滚动条选项创建我的java界面,这样我就可以在窗口上下移动。

我需要在我当前的代码中添加代码以及添加它的具体位置

这是代码:(我无法添加滚动窗格:(

package sms;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;





public class SMS4 extends JFrame implements ActionListener {


        JRadioButton entp = new JRadioButton("Enterprise");
    JRadioButton linux = new JRadioButton("Linux");
        JRadioButton tkodimi = new JRadioButton("TeoriKodimi");
        JRadioButton ings = new JRadioButton("InxhinieriSofti");
        JRadioButton cmp = new JRadioButton("Kompilator");

        JButton save= new JButton("Save");
        ButtonGroup btnG = new ButtonGroup();
        JPanel pane = new JPanel();
        JPanel pane1 = new JPanel();
        JPanel pane2 = new JPanel();

private DefaultTableModel students;
private JTable table;
private DefaultTableModel students1;
private JTable table1;
private DefaultTableModel students2;
private JTable table2;
private DefaultTableModel students3;
private JTable table3;
private DefaultTableModel students4;
private JTable table4;
    public SMS4() {
    super("Lendet");
    btnG.add(entp);
    btnG.add(linux);
    btnG.add(tkodimi);
    btnG.add(ings);
    btnG.add(cmp);

    pane.add(entp);
    pane.add(linux);
    pane.add(tkodimi);
    pane.add(ings);
    pane.add(cmp);

    pane.setLayout(new GridLayout(3, 3));

        pane2.add(save, "Center");
        save.addActionListener(this);

        entp.addActionListener(this);
    linux.addActionListener(this);
        tkodimi.addActionListener(this);
        ings.addActionListener(this);
        cmp.addActionListener(this);

        pane.setBorder(BorderFactory.createTitledBorder(
           BorderFactory.createEtchedBorder(), "Zgjidh lenden:"));
        pane1.setBorder(BorderFactory.createTitledBorder(
           BorderFactory.createEtchedBorder(), "Tabela e notave:"));
        pane2.setBorder(BorderFactory.createTitledBorder(
           BorderFactory.createEtchedBorder(), "Ruaj:"));

        // tabela e notave:
//tab 1:
    students = new DefaultTableModel();
    students.addColumn( "Emrat" );
    students.addColumn( "Notat" );


try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
                        Statement st= cn.createStatement();

                    ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Arkitekture Aplikimi'");
                                        if(rs!=null){
                                        while(rs.next()){

                                                  String[] student= new String[2];
                         student[0]=rs.getString("Name");
                                                   student[1]=rs.getString("Nota");


                                                      students.addRow(student);
                                            }
                                                    }
 else System.out.println("Gabim.");
table = new JTable(students);

pane1.add( new JScrollPane(table),BorderLayout.CENTER);
table.setVisible(false);

 }
catch(ClassNotFoundException e)  {
            System.err.println(e.getMessage());

        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        // end tabela
    //tab 1:
    students1 = new DefaultTableModel();
    students1.addColumn( "Emrat" );
    students1.addColumn( "Notat" );


try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn1 = DriverManager.getConnection("Jdbc:odbc:db");
                        Statement st1= cn1.createStatement();

                    ResultSet rs1= st1.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Linux'");
                                        if(rs1!=null){
                                        while(rs1.next()){

                                                  String[] student1= new String[2];
                         student1[0]=rs1.getString("Name");
                                                   student1[1]=rs1.getString("Nota");


                                                      students1.addRow(student1);
                                            }
                                                    }
 else System.out.println("Gabim.");
table1 = new JTable(students1);

pane1.add( new JScrollPane(table1),BorderLayout.CENTER);
table1.setVisible(false);

 }
catch(ClassNotFoundException e)  {
            System.err.println(e.getMessage());

        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        // end tab1
       //tab 2:
     students2 = new DefaultTableModel();
    students2.addColumn( "Emrat" );
    students2.addColumn( "Notat" );


try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
                        Statement st= cn.createStatement();

                    ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Teori Kodimi'");
                                        if(rs!=null){
                                        while(rs.next()){

                                                  String[] student2= new String[2];
                         student2[0]=rs.getString("Name");
                                                   student2[1]=rs.getString("Nota");


                                                      students2.addRow(student2);
                                            }
                                                    }
 else System.out.println("Gabim.");
table2 = new JTable(students2);

pane1.add( new JScrollPane(table2),BorderLayout.CENTER);
table2.setVisible(false);

 }
catch(ClassNotFoundException e)  {
            System.err.println(e.getMessage());

        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        // end tab2
           //tab 3:
     students3 = new DefaultTableModel();
    students3.addColumn( "Emrat" );
    students3.addColumn( "Notat" );


try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
                        Statement st= cn.createStatement();

                    ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Inxhinieri Softi'");
                                        if(rs!=null){
                                        while(rs.next()){

                                                  String[] student3= new String[2];
                         student3[0]=rs.getString("Name");
                                                   student3[1]=rs.getString("Nota");


                                                      students3.addRow(student3);
                                            }
                                                    }
 else System.out.println("Gabim.");
table3 = new JTable(students3);

pane1.add( new JScrollPane(table3),BorderLayout.CENTER);
table3.setVisible(false);

 }
catch(ClassNotFoundException e)  {
            System.err.println(e.getMessage());

        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        // end tab3
           //tab 4:
     students4 = new DefaultTableModel();
    students4.addColumn( "Emrat" );
    students4.addColumn( "Notat" );


try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("Jdbc:odbc:db");
                        Statement st= cn.createStatement();

                    ResultSet rs= st.executeQuery("SELECT Name, Nota FROM notat WHERE Emerlende='Kompilatore'");
                                        if(rs!=null){
                                        while(rs.next()){

                                                  String[] student4= new String[2];
                         student4[0]=rs.getString("Name");
                                                   student4[1]=rs.getString("Nota");


                                                      students4.addRow(student4);
                                            }
                                                    }
 else System.out.println("Gabim.");
table4 = new JTable(students4);

pane1.add( new JScrollPane(table4),BorderLayout.CENTER);
table4.setVisible(false);

 }
catch(ClassNotFoundException e)  {
            System.err.println(e.getMessage());

        }catch(SQLException e){
            System.err.println(e.getMessage());
            }
        // end tab4
    Container paneg= getContentPane();
        paneg.add(pane, "North");
        paneg.add(pane1, "Center");
        paneg.add(pane2, "South");

}


public void actionPerformed(ActionEvent e){

            Object source = e.getSource();
            if(entp.isSelected() ){
                            students = new DefaultTableModel();
                table.setVisible(true);

            }

 else if (linux.isSelected()) {
                       students1 = new DefaultTableModel();
                table1.setVisible(true);

            }
 else if (tkodimi.isSelected()) {
                       students2 = new DefaultTableModel();
                table2.setVisible(true);

            }
 else if (ings.isSelected())
                        {
                       students3 = new DefaultTableModel();
                    table3.setVisible(true);

            }
 else if (cmp.isSelected()) {
                       students4 = new DefaultTableModel();
                table4.setVisible(true);

            }
    }


   public void frame(){
    SMS4 s4 = new SMS4();
    s4.setSize(700,700);
    s4.setVisible(true);
    s4.setResizable(false);
    s4.setLocation(320,400);
    }
}

2 个答案:

答案 0 :(得分:2)

JScrollPane课程正是您要找的。 JavaDocs会告诉您需要知道的所有内容,甚至可以链接到简单易用的教程。

答案 1 :(得分:1)

将您的组件放入JScrollPane

无论您要滚动的项目是JPanel还是JTextArea,都应将其添加到JScrollPane,然后将JScrollPane添加到面板或其他容器。

所以,无论你在哪里找到它,都要找到:

container.add(item);

相反,这样做:

JScrollPane pane = new JScrollPane(item);
container.add(pane);