java – JTabBar中的JPanel不可滚动
发布时间:2020-12-15 04:21:22 所属栏目:Java 来源:网络整理
导读:在这里输入代码我有以下问题. 我在一个面板上添加了两个JPanels,最后将它添加到我的TabbedPane中.但是,我想让它可滚动,因此我只是使Panel可滚动,只是在栏的中间添加我的滚动条. 这是我的例子: public class Test extends JFrame { private static final lon
在这里输入代码我有以下问题.
我在一个面板上添加了两个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",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","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(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","Brown"},"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",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",BoxLayout.Y_AXIS)); return panel1; } } } 解决方法
如果这是你需要的 然后你可以做这样的事情: 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |