如何在JScrollPane上添加JButton? [英] How to add JButton on JScrollPane?
本文介绍了如何在JScrollPane上添加JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jscrollpane制作一个桌面应用程序.我想在jscrollpane中添加multipul按钮.我只能在其中添加一个按钮
Hi i am trying to make a desktop application in swing here i am using jscrollpane. i want to add multipul button in jscrollpane. i am able to add only single button there how can i do this
我的代码在下面给出
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);
}
}
我如何实现所需的输出 预先感谢
How can i achieve my desired output Thanks in advance
推荐答案
将按钮添加到具有适当布局(例如GridLayout
或FlowLayout
)的面板中.将面板添加到滚动窗格.
Add the buttons to a panel with suitable layout (e.g. GridLayout
or FlowLayout
). Add the panel to the scroll pane.
这篇关于如何在JScrollPane上添加JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文