如何在JTabbedPane选项卡中添加JScrollPane

时间:2016-12-23 21:15:41

标签: java swing jscrollpane jtabbedpane

我想要一个' Tab'在一个带有JScrollPane的JTabbedPane内部。但是我无法找到办法。这是我的代码到目前为止......

" HomeDelivery.java"

import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;

class HomeDelivery extends JFrame
{
JTabbedPane menu;
JPanel pizza,pastry,drinks;

public HomeDelivery()
{
    setSize(800,800);
    setVisible(true);
    setLayout(new BorderLayout());

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    menu = new JTabbedPane();

    menu.addTab("Burger",new Burger());

    pizza = new JPanel();
    menu.addTab("Pizza",pizza);

    pastry = new JPanel();
    menu.addTab("Pastry",pastry);

    drinks = new JPanel();
    menu.addTab("Drinks",drinks);

    add(menu);
}

public static void main(String args[])
{
    HomeDelivery h = new HomeDelivery();
}
}

" Burger.java"

import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.LineBorder;

class Burger extends JPanel
{
JFrame f;
//JPanel s;
JPanel p[];
JLabel image[];
JLabel name[];
JLabel price[];
JButton b[];
JScrollPane scroll;
int row;

public Burger()
{   
    p = new JPanel[40];
    image = new JLabel[40];
    name = new JLabel[40];
    price = new JLabel[40];
    b = new JButton[40];

    for(int i=0;i<40;i++)
    {
        p[i] = new JPanel();
        p[i].setLayout(new BoxLayout(p[i],BoxLayout.LINE_AXIS));
        p[i].add(Box.createRigidArea(new Dimension(100,50)));
        image[i] = new JLabel("Image");
        b[i] = new JButton("ADD");
        name[i] = new JLabel("Burger");
        price[i] = new JLabel("150");

        p[i].add(image[i]);
        p[i].add(name[i]);
        p[i].add(price[i]);
        p[i].add(b[i]);

        add(p[i]);
    }

    scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setPreferredSize(new Dimension(100,50));
    add(scroll);
}
}

在&#34; Burger.java&#34;的最后一部分,我试图添加一个JScrollPane,虽然我知道这不是我做的正确方法。如果我没有在&#34; Burger.java&#34;中扩展JPanel。 ,我可以通过这样做添加一个滚动窗格: &#34; JScrollPane scroll = new JScrollPane(JPanel,JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_​​NEVER);&#34;

但是,我怎么能在&#34; Burger.java&#34;现在?如何在我的&#34; Burger&#34;中添加JScrollPane? &#39;标签&#39; ? ......

2 个答案:

答案 0 :(得分:2)

像这样的问题的第一站是公共Java API文档。在那里你会找到JScrollPane的另一个构造函数:

http://docs.oracle.com/javase/8/docs/api/javax/swing/JScrollPane.html#JScrollPane-java.awt.Component-int-int-

据此,您的问题可以通过以下方式解决:

scroll = new JScrollPane(p,  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

答案 1 :(得分:1)

修改:不要展开JScrollPane,这里没有必要。

你可以:

例如:

import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class TabExample {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            final JFrame frame = new JFrame("Example");
            final JTabbedPane tp = new JTabbedPane();
            final BurgerTab burgerTab = new BurgerTab();
            final JScrollPane scrollPane = new JScrollPane(burgerTab, 
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            tp.addTab("Burgers", scrollPane);
            frame.getContentPane().add(tp);
            frame.pack();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }

    private static class BurgerTab extends JPanel {

        BurgerTab() {
            setLayout(new BoxLayout(this, SwingConstants.VERTICAL));
            final JPanel[] burgers = new JPanel[40];

            for(int i = 0; i < burgers.length; i++) {
                burgers[i] = new JPanel();
                burgers[i].add(new JLabel("Burger #" + (i + 1)));
                add(burgers[i]);
            }
        }

    }

}