在此处输入代码我遇到以下问题。
我在一个面板中添加了两个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;
}
}
}
任何建议我做错了什么?
感谢您的回复!
更新
我用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;
}
}
}
答案 0 :(得分:3)
您必须在其上添加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));
答案 1 :(得分:3)
在JTable
内添加JScrollPane
,然后在JScrollPane
中添加JPanel
。
JScrollPane pane = new JScrollPane(table);
panel1.add(pane);
不需要下面的行:
panel.add(new JScrollBar());
请参阅How to Use Tables上的 Swing Tutorial ,您也可以在这里找到示例。
如果需要,您可以设置JPanel
的大小(并非总是需要),然后将其添加到JScrollPane
JPanel panel = new JPanel(){
@Override
public Dimension getSize(){
return new Dimension(...,....);
}
};
您未在JScrollPane
内添加。它应该是
tabbedPane.addTab("Settings", new JScrollPane(settings.createLayout()));
并删除以下行:
add(new JScrollPane());
答案 2 :(得分:1)
你的答案很多!但是,这只会使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;
}