java GUI 滚动条
有n按钮(数目不定)放置在JPanel组件中,布局是每行4个按钮,希望当行数超过3行后自动出现滚动条。求大神给出这部分的实现代码。...
有n按钮(数目不定)放置在JPanel组件中,布局是每行4个按钮,希望当行数超过3行后自动出现滚动条。
求大神给出这部分的实现代码。 展开
求大神给出这部分的实现代码。 展开
2个回答
展开全部
程序如下:
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JscrollDemo extends JFrame
{
private JScrollPane btnPanel;
private JPanel panel;
private JButton button;
int btnNum = 10; //按钮数,可以是任意数
int closNum = 4;//每行按钮数,可自定义
public JscrollDemo()
{
panel = new JPanel();
setTitle("滚动条测试");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 500)/2, 500, 400);
panel.setLayout(new GridLayout(0,closNum));
for(int i=0;i<btnNum;i++)
{
button = new JButton("按钮" + (i+1));
button.setSize(100, 80);
panel.add(button);
}
if((btnNum*1.0/closNum) > 3)
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
else
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
add(btnPanel);
setVisible(true);
setResizable(false);
}
public static void main(String[] args)
{
new JscrollDemo();
}
}
有问题欢迎提问,满意请采纳,谢谢!
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JscrollDemo extends JFrame
{
private JScrollPane btnPanel;
private JPanel panel;
private JButton button;
int btnNum = 10; //按钮数,可以是任意数
int closNum = 4;//每行按钮数,可自定义
public JscrollDemo()
{
panel = new JPanel();
setTitle("滚动条测试");
setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 500)/2,
(Toolkit.getDefaultToolkit().getScreenSize().height - 500)/2, 500, 400);
panel.setLayout(new GridLayout(0,closNum));
for(int i=0;i<btnNum;i++)
{
button = new JButton("按钮" + (i+1));
button.setSize(100, 80);
panel.add(button);
}
if((btnNum*1.0/closNum) > 3)
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
else
{
btnPanel = new JScrollPane(panel,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}
add(btnPanel);
setVisible(true);
setResizable(false);
}
public static void main(String[] args)
{
new JscrollDemo();
}
}
有问题欢迎提问,满意请采纳,谢谢!
展开全部
JScrollPane scroll = new JScrollPane();
JPanel contentPanel = new JPanel();
scroll.setViewport(contentPanel );
contentPanel.add(new JLabel("客户须知"), BorderLayout.NORTH);
contentPanel.add(new JTextArea("欢迎...", BorderLayout.CENTER);
JPanel radioPanel= new JPanel();
radioPanel.add(new JRadioButton("同意"));
radioPanel.add(new JRadioButton("不同意"));
contentPanel.add(radioPanel, BorderLayout.SOUTH);
JPanel contentPanel = new JPanel();
scroll.setViewport(contentPanel );
contentPanel.add(new JLabel("客户须知"), BorderLayout.NORTH);
contentPanel.add(new JTextArea("欢迎...", BorderLayout.CENTER);
JPanel radioPanel= new JPanel();
radioPanel.add(new JRadioButton("同意"));
radioPanel.add(new JRadioButton("不同意"));
contentPanel.add(radioPanel, BorderLayout.SOUTH);
追问
和我的问题不符啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询