如何将滚动条添加到JTabbedPane。基本上我有一个管理面板,作为TAB(JTabbedPane)进一步附加在框架上

时间:2014-02-07 20:12:22

标签: java swing jframe jpanel jtabbedpane

//这里的admin是一个位于框架上的面板,作为第二个TAB,我想添加一个滚动条。

import java.awt.Color;
import java.awt.Image;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
public class admin extends JPanel
{
    private static final long serialVersionUID = 1L;
 JLabel lb;
 Image image;
 JScrollPane js;
 public admin()   //admin panel is invoked by a frame. 
 {

    setBackground(Color.white);

    add(new admsign()); // admsign is another panel located on admin which is a simple form. 


// if you want me to post the code for admsign() then tell me.




 }
}

//我觉得没必要。

1 个答案:

答案 0 :(得分:7)

  

“如何将滚动条添加到JTabbedPane。基本上我有一个管理面板,作为TAB(JTabbedPane)在框架上进一步附加”

基本上,你。您将JScollPane包裹在添加到JPanel

JTabbedPane周围
JTabbedPane tabbed = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

tabbed.add(new JScrollPane(panel1), "Panel 1");
tabbed.add(new JScrollPane(panel2), "Panel 2");

enter image description here

import java.awt.*;
import javax.swing.*;

public class TestTabbedScroll {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JPanel panel = new JPanel();
                Box box = Box.createVerticalBox();
                for (int i = 0; i < 100; i++) {
                    box.add(new JLabel("Hello, StackOverflow!"));
                }
                panel.add(box);
                panel.setBackground(Color.CYAN);

                JTabbedPane tab = new JTabbedPane();
                JScrollPane scroll = new JScrollPane(panel);
                scroll.setPreferredSize(new Dimension(300, 300));
                tab.add(scroll, "Panel 1");

                JOptionPane.showMessageDialog(
                        null, tab, "Test Tabbed", JOptionPane.PLAIN_MESSAGE); 
            }
        });
    }
}