JPanel的java滚动窗格?
我想将Scrollpane添加到另一个JPanel调用的JPanel
public class test {
JFrame jframe;
JPanel jpanel;
JScrollPane js= null;
public void createFrame()
{
jframe=new JFrame("Thick client Wallboard");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jframe.setSize(dim.width,dim.height);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setUndecorated(true);
jframe.setResizable(false);
jframe.setLocation(0, 0);
getJContentPane();
jframe.setContentPane(jpanel);
jframe.setVisible(true);
}
public void getJContentPane()
{
jpanel = new JPanel();
jpanel.setBackground(new Color(0x505050));
jpanel.setLayout(null);
jpanel.setFont(new Font("verdana",Font.BOLD,12));
jpanel.setBorder(new LineBorder(Color.BLACK,2));
getpanel();
jpanel.add(js);
jpanel.setBackground(Color.LIGHT_GRAY);
jpanel.setVisible(true);
}
public void getpanel()
{
JPanel mainPanel=new JPanel();
mainPanel.setBounds(50,50,920,650);
mainPanel.setBackground(Color.WHITE);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
for(int i = 0; i < 30; i++) {
mainPanel.add(new JButton("Button " + i));
}
js=new JScrollPane(mainPanel);
}
public static void main(String[] args) {
test t=new test();
t.createFrame();
}
}
我已经这样做了,但它不工作
# 1 楼答案
你可以这样做:
同时检查: http://www.coderanch.com/t/342486/GUI/java/Adding-ScrollPane-JPanel
# 2 楼答案
JScrollPane和所有JComponent衍生物一样,是一个装饰器,您可以用它装饰任何组件
您可以在滚动窗格中放置任何组件
只需添加滚动窗格而不是包装组件