您好我正在尝试使用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);
}
}
如何实现我想要的输出 提前致谢
答案 0 :(得分:7)
将按钮添加到具有合适布局的面板(例如GridLayout
或FlowLayout
)。将面板添加到滚动窗格。