[英]JPanel in JTabBar is not scrollable
在这里输入代码我有以下问题。
我在一个面板上添加了两个JPanels,最后将它添加到我的TabbedPane中。 但是,我想让它可滚动,因此我只是使Panel可滚动,只是在栏的中间添加我的滚动条。
这是我的例子:
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JPanel {
public JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(new JScrollBar());
return panel;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name", "Last Name"};
Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"},
{"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
}
}
我有什么建议我做错了吗?
感谢您的回复!
UPDATE
我用JScrollPane尝试了它但它仍然不起作用:
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JComponent panel2 = new JPanel();
tabbedPane.addTab("Test1", null, panel2,
"Displays the results");
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JScrollPane {
public JPanel createLayout() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table3());
panel.add(Box.createRigidArea(new Dimension(0,10)));
add(new JScrollPane());
return panel;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table3() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
}
}
你必须在上面添加一个JScrollPane! :)
我真的不知道你在那里做了什么,但基本上就是这样:
JPanel myPanel = new JPanel(); // the main content is on the myPanel
myPanel.add(myContent);// add everything you want
new JFrame().add(new JScrollPane(myPanel));
在JScrollPane
添加JTable
,然后在JPanel
添加JScrollPane
。
JScrollPane pane = new JScrollPane(table);
panel1.add(pane);
不需要以下线:
panel.add(new JScrollBar());
请参阅如何使用表的 Swing教程 ,您也可以在其中找到示例。
您可以根据需要设置JPanel
的大小( 并非总是需要 ),然后将其添加到JScrollPane
JPanel panel = new JPanel(){
@Override
public Dimension getSize(){
return new Dimension(...,....);
}
};
您没有将它添加到JScrollPane
。 它应该是
tabbedPane.addTab("Settings", new JScrollPane(settings.createLayout()));
并删除以下行:
add(new JScrollPane());
谢谢你的答案! 但是,这只会使JTable可滚动。 是否也可以在表格所在的位置滚动JPanel?
如果这是你需要的
然后你可以做这样的事情:
public JScrollPane createLayout() {
JPanel panel = new JPanel();
JScrollPane sp = new JScrollPane(panel);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// or ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(table1());
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
panel.add(Box.createRigidArea(new Dimension(0,10)));
return sp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.