如何为窗口附加滚动条?

时间:2016-08-26 04:24:16

标签: java swing jscrollpane

如何为我的窗口添加滚动窗格?

程序正确编译,但未创建窗口的滚动窗格。我真的不知道为什么会这样。我定义了JScrollPane,甚至用scrollPane = new JScrollPane

实现了它

我的错误在哪里?

以下是我的代码:

import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

public class RegForm extends JFrame implements ItemListener{ 
JLabel l0,li,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13;
JButton b1,b2,b3,b4,b5;
JTextField t1,t2,t3,t4,t5,t6,t7,t8;
JTextArea a1,a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton( " Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox (" C ");
JCheckBox cb2 = new JCheckBox (" C++ ");
JCheckBox cb3 = new JCheckBox (" Java ");
JCheckBox cb4 = new JCheckBox (" Oracle ");
JCheckBox cb5 = new JCheckBox (" Android ");
JCheckBox cb6 = new JCheckBox (" iOS ");
JCheckBox cb7 = new JCheckBox (" Web Designing ");
JCheckBox cb8 = new JCheckBox (" .Net ");
JCheckBox cb9 = new JCheckBox (" Same as Contact Address ");
JScrollPane scrollPane = new JScrollPane();
RegForm()
{

    l0 = new JLabel("REGISTRATION FORM");   
    Font f0 = new Font("Algerian",Font.ITALIC,20);
    l0.setFont(f0);
    l0.setBounds(600,10,250,50);
    scrollPane.add(l0);

    li = new JLabel(" * Fields are mandatory");
    Font fi = new Font("Arabic TypeSetting",Font.PLAIN,17);
    li.setFont(fi);
    li.setForeground(Color.RED);
    li.setBounds(10,50,150,30);
    scrollPane.add(li);


    l1 = new JLabel(" * FirstName: ");
    Font f1 = new Font("Bookman Old Style",Font.PLAIN,12);
    l1.setFont(f1);
    l1.setBounds(10,70,100,50);
    scrollPane.add(l1);

    t1 = new JTextField(20);
    t1.setBounds(165,85,140,20);
    scrollPane.add(t1);

    l2 = new JLabel("* Confirm FirstName: ");
    l2.setFont(f1);
    l2.setBounds(10,100,150,50);
    scrollPane.add(l2);

    t2 = new JTextField(20);
    t2.setBounds(165,115,140,20);
    scrollPane.add(t2);

    l3 = new JLabel(" Middle Name: ");
    l3.setFont(f1);
    l3.setBounds(15,130,120,50);
    scrollPane.add(l3);

    t3 = new JTextField(20);
    t3.setBounds(165,145,140,20);
    scrollPane.add(t3);

    l4 = new JLabel(" Confirm Middle Name: ");
    l4.setFont(f1);
    l4.setBounds(15,160,150,50);
    scrollPane.add(l4);

    t4 = new JTextField(20);
    t4.setBounds(165,175,140,20);
    scrollPane.add(t4);

    l5 = new JLabel(" * Sur Name: ");
    l5.setFont(f1);
    l5.setBounds(10,190,100,50);
    scrollPane.add(l5);

    t5 = new JTextField(20);
    t5.setBounds(165,205,140,20);
    scrollPane.add(t5);

    l6 = new JLabel(" * Confirm Sur Name: ");
    l6.setFont(f1); 
    l6.setBounds(10,220,150,50);
    scrollPane.add(l6);

    t6 = new JTextField(20);
    t6.setBounds(165,235,140,20);
    scrollPane.add(t6);

    l7 = new JLabel(" * DD / MM / YYYY" );
    Font f2 = new Font(" Comic Sans MS ",Font.ITALIC,12);
    l7.setFont(f2);
    l7.setBounds(10,260,150,50);
    scrollPane.add(l7);

    for(int j=1;j<=31;j++)
    dd.addItem(new Integer(j));
    dd.setBounds(165,275,47,20);
    scrollPane.add(dd);
    dd.addItemListener(this);

    mm.addItem("January");
    mm.addItem("February");
    mm.addItem("March");
    mm.addItem("April");
    mm.addItem("May");
    mm.addItem("June");
    mm.addItem("July");
    mm.addItem("August");
    mm.addItem("September");
    mm.addItem("October");
    mm.addItem("November");
    mm.addItem("December");
    mm.setBounds(212,275,90,20);
    scrollPane.add(mm);
    mm.addItemListener(this);

    for(int i=1990;i<=2016;i++)
    yyyy.addItem(new Integer(i));
    yyyy.setBounds(302,275,70,20);
    scrollPane.add(yyyy);
    yyyy.addItemListener(this);

    l8 = new JLabel(" Age: ");
    l8.setFont(f1);
    l8.setBounds(15,290,50,50);
    scrollPane.add(l8);

    t8 = new JTextField(10);
    t8.setBounds(165,305,50,20);
    scrollPane.add(t8);

    l9 = new JLabel(" Qualification ");
    l9.setFont(f1);
    l9.setBounds(15,320,120,50);
    scrollPane.add(l9);

    q.addItem(" B.Tech ");
    q.addItem(" M.Tech ");
    q.addItem(" MBA ");
    q.addItem(" MCA ");
    q.addItem(" Intermediate ");    
    q.addItem(" SSC "); 
    q.addItem(" Others ");
    q.setBounds(165,335,100,20);
    scrollPane.add(q);
    q.addItemListener(this);

    l10 = new JLabel(" Gender ");
    l10.setFont(f1);
    l10.setBounds(15,360,80,50);
    scrollPane.add(l10);

    rb1.setBounds(165,365,80,39);
    rb2.setBounds(250,365,80,39);

    ButtonGroup bg = new ButtonGroup();
    bg.add(rb1);
    bg.add(rb2);
    scrollPane.add(rb1);
    scrollPane.add(rb2);    

    l11 = new JLabel(" Courses Intrested: ");
    l11.setFont(f1);    
    l11.setBounds(15,450,150,50);
    scrollPane.add(l11);

    cb1.setBounds(165,390,100,50);
    scrollPane.add(cb1); 
    cb2.setBounds(285,390,100,50);
    scrollPane.add(cb2);
    cb3.setBounds(165,425,100,50);
    scrollPane.add(cb3);
    cb4.setBounds(285,425,100,50);
    scrollPane.add(cb4);
    cb5.setBounds(165,460,100,50);
    scrollPane.add(cb5);
    cb6.setBounds(285,460,100,50);
    scrollPane.add(cb6);
    cb7.setBounds(165,495,100,50);
    scrollPane.add(cb7);
    cb8.setBounds(285,495,100,50);
    scrollPane.add(cb8);
    cb9.setBounds(15,630,200,50);
    scrollPane.add(cb9);

    l12 = new JLabel(" Contact Address: ");
    l12.setFont(f1);
    l12.setBounds(15,550,150,50);
    scrollPane.add(l12);

    a1 = new JTextArea (5,20);
    a1.setBounds(165,545,250,80);
    scrollPane.add(a1);

    l13 = new JLabel(" Permenant Address: ");
    l13.setFont(f1);
    l13.setBounds(15,675,150,50);
    scrollPane.add(l13);

    a2 = new JTextArea (5,20);
    a2.setBounds(165,680,250,80);
    scrollPane.add(a2);

    cb9.addItemListener(this);


}

public void itemStateChanged(ItemEvent ie) 
{
    if(ie.getSource() == yyyy){
    int y = (Integer) ie.getItem();
    t8.setText(Integer.toString(2016-y));
    t8.setEditable(false);
    }
    if(cb9.isSelected()){
    a2.setText(a1.getText());
    a2.setEditable(false);
    }
}
public void actionPerformed(ActionEvent ae)
{   
    }

public static void main(String[] args)
{

    RegForm rf = new RegForm();
    rf.setTitle("Hai Hello");
    JScrollPane scrollPane = new JScrollPane();
       scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(10,10,100,100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(scrollPane);
rf.setContentPane(contentPane);
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}

2 个答案:

答案 0 :(得分:3)

scrollPane.add(l11);

切勿将组件直接添加到滚动窗格。

l1.setBounds(10,70,100,50);

不要使用setBounds(...)。布局管理器的工作是设置组件的大小/位置。

基本逻辑是:

JPanel panel = new JPanel(); // set your layout manager for the panel.
panel.add( someComponent );
panel.add( anotherComponent );
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );

阅读Swing Tutorial了解Swing基础知识。本教程中的每个部分都有工作示例。也许从How to Use Scroll Panes上的部分开始。

答案 1 :(得分:2)

这样可行,但您需要更好地布局组件..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RegForm extends JFrame implements ItemListener {

    JLabel l0, li, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13;
    JButton b1, b2, b3, b4, b5;
    JTextField t1, t2, t3, t4, t5, t6, t7, t8;
    JTextArea a1, a2;
    JComboBox<Integer> dd = new JComboBox<Integer>();
    JComboBox<String> mm = new JComboBox<String>();
    JComboBox<Integer> yyyy = new JComboBox<Integer>();
    JComboBox<String> q = new JComboBox<String>();
    JRadioButton rb1 = new JRadioButton(" Male ");
    JRadioButton rb2 = new JRadioButton(" Female ");
    JCheckBox cb1 = new JCheckBox(" C ");
    JCheckBox cb2 = new JCheckBox(" C++ ");
    JCheckBox cb3 = new JCheckBox(" Java ");
    JCheckBox cb4 = new JCheckBox(" Oracle ");
    JCheckBox cb5 = new JCheckBox(" Android ");
    JCheckBox cb6 = new JCheckBox(" iOS ");
    JCheckBox cb7 = new JCheckBox(" Web Designing ");
    JCheckBox cb8 = new JCheckBox(" .Net ");
    JCheckBox cb9 = new JCheckBox(" Same as Contact Address ");
    JScrollPane scrollPane;
    JPanel panel = new JPanel(new GridLayout(0,1));

    RegForm() {

        l0 = new JLabel("REGISTRATION FORM");
        Font f0 = new Font("Algerian", Font.ITALIC, 20);
        l0.setFont(f0);
        l0.setBounds(600, 10, 250, 50);
        panel.add(l0);

        li = new JLabel(" * Fields are mandatory");
        Font fi = new Font("Arabic TypeSetting", Font.PLAIN, 17);
        li.setFont(fi);
        li.setForeground(Color.RED);
        li.setBounds(10, 50, 150, 30);
        panel.add(li);

        l1 = new JLabel(" * FirstName: ");
        Font f1 = new Font("Bookman Old Style", Font.PLAIN, 12);
        l1.setFont(f1);
        l1.setBounds(10, 70, 100, 50);
        panel.add(l1);

        t1 = new JTextField(20);
        t1.setBounds(165, 85, 140, 20);
        panel.add(t1);

        l2 = new JLabel("* Confirm FirstName: ");
        l2.setFont(f1);
        l2.setBounds(10, 100, 150, 50);
        panel.add(l2);

        t2 = new JTextField(20);
        t2.setBounds(165, 115, 140, 20);
        panel.add(t2);

        l3 = new JLabel(" Middle Name: ");
        l3.setFont(f1);
        l3.setBounds(15, 130, 120, 50);
        panel.add(l3);

        t3 = new JTextField(20);
        t3.setBounds(165, 145, 140, 20);
        panel.add(t3);

        l4 = new JLabel(" Confirm Middle Name: ");
        l4.setFont(f1);
        l4.setBounds(15, 160, 150, 50);
        panel.add(l4);

        t4 = new JTextField(20);
        t4.setBounds(165, 175, 140, 20);
        panel.add(t4);

        l5 = new JLabel(" * Sur Name: ");
        l5.setFont(f1);
        l5.setBounds(10, 190, 100, 50);
        panel.add(l5);

        t5 = new JTextField(20);
        t5.setBounds(165, 205, 140, 20);
        panel.add(t5);

        l6 = new JLabel(" * Confirm Sur Name: ");
        l6.setFont(f1);
        l6.setBounds(10, 220, 150, 50);
        panel.add(l6);

        t6 = new JTextField(20);
        t6.setBounds(165, 235, 140, 20);
        panel.add(t6);

        l7 = new JLabel(" * DD / MM / YYYY");
        Font f2 = new Font(" Comic Sans MS ", Font.ITALIC, 12);
        l7.setFont(f2);
        l7.setBounds(10, 260, 150, 50);
        panel.add(l7);

        for (int j = 1; j <= 31; j++) {
            dd.addItem(new Integer(j));
        }
        dd.setBounds(165, 275, 47, 20);
        panel.add(dd);
        dd.addItemListener(this);

        mm.addItem("January");
        mm.addItem("February");
        mm.addItem("March");
        mm.addItem("April");
        mm.addItem("May");
        mm.addItem("June");
        mm.addItem("July");
        mm.addItem("August");
        mm.addItem("September");
        mm.addItem("October");
        mm.addItem("November");
        mm.addItem("December");
        mm.setBounds(212, 275, 90, 20);
        panel.add(mm);
        mm.addItemListener(this);

        for (int i = 1990; i <= 2016; i++) {
            yyyy.addItem(new Integer(i));
        }
        yyyy.setBounds(302, 275, 70, 20);
        panel.add(yyyy);
        yyyy.addItemListener(this);

        l8 = new JLabel(" Age: ");
        l8.setFont(f1);
        l8.setBounds(15, 290, 50, 50);
        panel.add(l8);

        t8 = new JTextField(10);
        t8.setBounds(165, 305, 50, 20);
        panel.add(t8);

        l9 = new JLabel(" Qualification ");
        l9.setFont(f1);
        l9.setBounds(15, 320, 120, 50);
        panel.add(l9);

        q.addItem(" B.Tech ");
        q.addItem(" M.Tech ");
        q.addItem(" MBA ");
        q.addItem(" MCA ");
        q.addItem(" Intermediate ");
        q.addItem(" SSC ");
        q.addItem(" Others ");
        q.setBounds(165, 335, 100, 20);
        panel.add(q);
        q.addItemListener(this);

        l10 = new JLabel(" Gender ");
        l10.setFont(f1);
        l10.setBounds(15, 360, 80, 50);
        panel.add(l10);

        rb1.setBounds(165, 365, 80, 39);
        rb2.setBounds(250, 365, 80, 39);

        ButtonGroup bg = new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);
        panel.add(rb1);
        panel.add(rb2);

        l11 = new JLabel(" Courses Intrested: ");
        l11.setFont(f1);
        l11.setBounds(15, 450, 150, 50);
        panel.add(l11);

        cb1.setBounds(165, 390, 100, 50);
        panel.add(cb1);
        cb2.setBounds(285, 390, 100, 50);
        panel.add(cb2);
        cb3.setBounds(165, 425, 100, 50);
        panel.add(cb3);
        cb4.setBounds(285, 425, 100, 50);
        panel.add(cb4);
        cb5.setBounds(165, 460, 100, 50);
        panel.add(cb5);
        cb6.setBounds(285, 460, 100, 50);
        panel.add(cb6);
        cb7.setBounds(165, 495, 100, 50);
        panel.add(cb7);
        cb8.setBounds(285, 495, 100, 50);
        panel.add(cb8);
        cb9.setBounds(15, 630, 200, 50);
        panel.add(cb9);

        l12 = new JLabel(" Contact Address: ");
        l12.setFont(f1);
        l12.setBounds(15, 550, 150, 50);
        panel.add(l12);

        a1 = new JTextArea(5, 20);
        a1.setBounds(165, 545, 250, 80);
        panel.add(a1);

        l13 = new JLabel(" Permenant Address: ");
        l13.setFont(f1);
        l13.setBounds(15, 675, 150, 50);
        panel.add(l13);

        a2 = new JTextArea(5, 20);
        a2.setBounds(165, 680, 250, 80);
        panel.add(a2);

        cb9.addItemListener(this);

        scrollPane = new JScrollPane(panel);
        add(scrollPane);
        //add(panel);
    }

    public void itemStateChanged(ItemEvent ie) {
        if (ie.getSource() == yyyy) {
            int y = (Integer) ie.getItem();
            t8.setText(Integer.toString(2016 - y));
            t8.setEditable(false);
        }
        if (cb9.isSelected()) {
            a2.setText(a1.getText());
            a2.setEditable(false);
        }
    }

    public void actionPerformed(ActionEvent ae) {
    }

    public static void main(String[] args) {

        RegForm rf = new RegForm();
        rf.setTitle("Hai Hello");
        /*JScrollPane panel = new JScrollPane();
        panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel.setBounds(10, 10, 100, 100);
        JPanel contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(1500, 800));
        contentPane.add(panel);
        rf.setContentPane(contentPane);*/
        rf.pack();
        rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        rf.setVisible(true);
    }
}

说明:

  1. 正如@camickr所提到的,设置组件的边界将无法可靠地工作。 Java GUI必须使用不同语言环境中的不同PLAF来处理不同的操作系统,屏幕大小,屏幕分辨率等。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them以及white space的布局填充和边框。
  2. 在你需要几十个组件之前,请尝试解决编程错误!对于具有许多组件的布局的简单测试,只需在循环中添加虚拟组件。换句话说:为了更好地提供帮助,请发布Minimal, Complete, and Verifiable exampleShort, Self Contained, Correct Example
  3. 更安全地使用特定字体的逻辑字体名称,例如Algerian仅适用于安装了该字体的系统!