我如何在 JTabbedPane 的选项卡中添加 JScrollPane
How can i add JScrollPane inside a JTabbedPane's Tab
我想用 JScrollPane.But 将 'Tab' 包装在 JTabbedPane 中 我找不到方法 it.This 是我目前的代码 ...
"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);
}
}
在 "Burger.java" 这部分的最后,我尝试添加一个 JScrollPane,尽管我知道这不是我的正确做法。如果我没有在 "Burger.java" 中扩展 JPanel,我将能够通过这样做来添加一个滚动窗格:
"JScrollPane scroll = new JScrollPane(JPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);"
但是,我现在 "Burger.java" 怎么办?如何在 "Burger" 'Tab' 中添加 JScrollPane? ...
此类问题的第一站是 public Java API 文档。在那里你会找到 JScrollPane
:
的另一个构造函数
据此,您的问题可以通过以下方式解决:
scroll = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
编辑:不要扩展JScrollPane
,这里没有必要。
您可以:
- 延长
JPanel
- 通过
将JPanel
添加到JScrollPane
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
- 将
JScrollPane
添加到 JTabbedPane
例如:
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]);
}
}
}
}
我想用 JScrollPane.But 将 'Tab' 包装在 JTabbedPane 中 我找不到方法 it.This 是我目前的代码 ...
"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);
}
}
在 "Burger.java" 这部分的最后,我尝试添加一个 JScrollPane,尽管我知道这不是我的正确做法。如果我没有在 "Burger.java" 中扩展 JPanel,我将能够通过这样做来添加一个滚动窗格: "JScrollPane scroll = new JScrollPane(JPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);"
但是,我现在 "Burger.java" 怎么办?如何在 "Burger" 'Tab' 中添加 JScrollPane? ...
此类问题的第一站是 public Java API 文档。在那里你会找到 JScrollPane
:
据此,您的问题可以通过以下方式解决:
scroll = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
编辑:不要扩展JScrollPane
,这里没有必要。
您可以:
- 延长
JPanel
- 通过
将JPanel
添加到JScrollPane
JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
- 将
JScrollPane
添加到JTabbedPane
例如:
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]);
}
}
}
}