java – 如何在JScrollPane上添加JButton?
发布时间:2020-12-15 04:18:26 所属栏目:Java 来源:网络整理
导读:嗨,我想在这里制作桌面应用程序我在使用jscrollpane.我想在jscrollpane中添加multipul按钮.我只能添加单个按钮,我该怎么做 我的代码如下 public class AddingToJScrollPane { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed P
嗨,我想在这里制作桌面应用程序我在使用jscrollpane.我想在jscrollpane中添加multipul按钮.我只能添加单个按钮,我该怎么做
我的代码如下 public class AddingToJScrollPane { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000,1000)); JScrollPane jScrollPane = new JScrollPane(label); JButton jButton1 = new JButton("Hello"); JButton jButton2 = new JButton("Hello"); jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setViewportBorder(new LineBorder(Color.RED)); jScrollPane.getViewport().add(jButton1,jButton2); frame.add(jScrollPane,BorderLayout.NORTH); frame.setSize(400,150); frame.setVisible(true); } } 更新的代码 public class AddingToJScrollPane { public static void main(String args[]) { JFrame frame = new JFrame("Tabbed Pane Sample"); JPanel panel = new JPanel(); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout( new GridLayout() ); JLabel label = new JLabel("Label"); label.setPreferredSize(new Dimension(1000,1000)); JScrollPane jScrollPane = new JScrollPane(panel); JButton jButton1 = new JButton("Hello"); JButton jButton2 = new JButton("He"); // jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jScrollPane.setViewportBorder(new LineBorder(Color.RED)); // jScrollPane.getViewport().add(panel); frame.add(jScrollPane,BorderLayout.NORTH); panel.add(jButton1); panel.add(jButton2); frame.setSize(400,150); frame.setVisible(true); } } 我怎样才能实现我想要的输出 解决方法
将按钮添加到具有合适布局的面板(例如GridLayout或FlowLayout).将面板添加到滚动窗格.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |