Java 如何为窗口附加滚动条?
如何为我的窗口附加滚动窗格 程序可以正确编译,但未创建窗口的滚动窗格。我真的不知道为什么会这样。我定义了Java 如何为窗口附加滚动条?,java,swing,jscrollpane,Java,Swing,Jscrollpane,如何为我的窗口附加滚动窗格 程序可以正确编译,但未创建窗口的滚动窗格。我真的不知道为什么会这样。我定义了JScrollPane,甚至用scrollPane=newjscrollpane 我的错在哪里 下面是我的代码: import java.awt.*; import java.awt.Font; import java.awt.event.*; import javax.swing.*; import javax.swing.JScrollPane; import java.awt.Dimen
JScrollPane
,甚至用scrollPane=newjscrollpane
我的错在哪里
下面是我的代码:
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);
}
}
import java.awt.*;
导入java.awt.Font;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.JScrollPane;
导入java.awt.Dimension;
导入javax.swing.JPanel;
导入javax.swing.JFrame;
导入javax.swing.JButton;
public类RegForm扩展JFrame实现items监听器{
JLabel l0、li、l1、l2、l3、l4、l5、l6、l7、l8、l9、l10、l11、l12、l13;
按钮b1、b2、b3、b4、b5;
JTextField t1、t2、t3、t4、t5、t6、t7、t8;
JTextA1、a2区;
JComboBox dd=新的JComboBox();
JComboBox mm=新的JComboBox();
JComboBox yyy=新JComboBox();
JComboBox q=新的JComboBox();
JRadioButton rb1=新的JRadioButton(“男性”);
JRadioButton rb2=新的JRadioButton(“女性”);
JCheckBox cb1=新的JCheckBox(“C”);
JCuffbox CB2=新的JCycBox(“C++”);
JCheckBox cb3=新的JCheckBox(“Java”);
JCheckBox cb4=新的JCheckBox(“Oracle”);
JCheckBox cb5=新的JCheckBox(“安卓”);
JCheckBox cb6=新的JCheckBox(“iOS”);
JCheckBox cb7=新的JCheckBox(“网页设计”);
JCheckBox cb8=新的JCheckBox(“.Net”);
JCheckBox cb9=新的JCheckBox(“与联系地址相同”);
JScrollPane scrollPane=新的JScrollPane();
RegForm()
{
l0=新的JLabel(“登记表”);
Font f0=新字体(“阿尔及利亚”,斜体,20);
l0.setFont(f0);
l0.立根(600,10250,50);
滚动窗格。添加(l0);
li=新的JLabel(“*字段是必需的”);
Font fi=新字体(“阿拉伯文排版”,普通字体,17);
li.setFont(fi);
设置前景(颜色:红色);
李.挫折(10,50150,30);;
滚动窗格。添加(li);
l1=新的JLabel(“*FirstName:”);
Font f1=新字体(“Bookman旧式”,字体:普通,12);
l1.设置字体(f1);
l1.立根(10,70100,50);
滚动窗格。添加(l1);
t1=新的JTextField(20);
t1.立根(165,85140,20);
滚动窗格。添加(t1);
l2=新的JLabel(“*确认名字:”);
l2.setFont(f1);
l2.立根(10100150,50);
滚动窗格。添加(l2);
t2=新的JTextField(20);
t2.立根(16511540,20);
滚动窗格。添加(t2);
l3=新的JLabel(“中间名:”);
l3.setFont(f1);
l3.立根(15130120,50);
滚动窗格。添加(l3);
t3=新的JTextField(20);
t3.立根(165145140,20);
滚动窗格。添加(t3);
l4=新的JLabel(“确认中间名:”);
l4.setFont(f1);
l4.立根(15160150,50);
滚动窗格。添加(l4);
t4=新的JTextField(20);
t4.立根(165175140,20);
滚动窗格。添加(t4);
l5=新的JLabel(“*Sur Name:”);
l5.设置字体(f1);
l5.立根(10190100,50);
滚动窗格。添加(l5);
t5=新的JTextField(20);
t5.立根(165205140,20);
滚动窗格。添加(t5);
l6=新的JLabel(“*确认Sur名称:”);
l6.setFont(f1);
l6.立根(10220150,50);
滚动窗格。添加(l6);
t6=新的JTextField(20);
t6.立根(165235140,20);
滚动窗格。添加(t6);
l7=新的JLabel(“*DD/MM/YYYY”);
Font f2=新字体(“Comic Sans MS”,字体斜体,12);
l7.设置字体(f2);
l7.立根(10260150,50);
滚动窗格。添加(l7);
对于(int j=1;j
切勿将组件直接添加到滚动窗格中
l1.setBounds(10,70,100,50);
不要使用收边(…)。布局管理器的工作是设置组件的大小/位置
基本逻辑是:
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基础知识。教程中的每个部分都有工作示例。可以从如何使用滚动窗格部分开始
切勿将组件直接添加到滚动窗格中
l1.setBounds(10,70,100,50);
不要使用收边(…)。布局管理器的工作是设置组件的大小/位置
基本逻辑是:
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基础知识。教程中的每个部分都有工作示例。可以从如何使用滚动窗格的部分开始。这很有效,但您需要更好地布局组件
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);
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类RegForm扩展JFrame实现ItemListener{
JLabel l0、li、l1、l2、l3、l4、l5、l6、l7、l8、l9、l10、l11、l12、l13;
按钮b1、b2、b3、b4、b5;
JTextField t1、t2、t3、t4、t5、t6、t7、t8;
JTextA1、a2区;
JComboBox dd=新的JComboBox();
JComboBox mm=新的JComboBox();
JComboBox yyy=新JComboBox();
JComboBox q=新的JComboBox();
JRadioButton rb1=新的JRadioButton(“男性”);
JRadioButton rb2=新的JRadioButton(“女性”);
JCheckBox cb1=新的JCheckBox(“C”);
JCuffbox CB2=新的JCycBox(“C++”);
JCheckBox cb3=新的JCheckBox(“Java”);
JCheckBox cb4=新的JCheckBox(“Oracle”);
JCheckBox cb5=新的JCheckBox(“安卓”);
JCheckBox cb6=新的JCheckBox(“iOS”);
JCheckBox cb7=新的JCheckBox(“网页设计”);
JCheckBox cb8=新的JCheckBox(“.Net”);
JCheckBox cb9=新的JCheckBox(“与联系地址相同”);
JScrollPane滚动窗格;
JPanel面板=新JPanel(新网格布局(0,1));
RegForm(){
l0=新的JLabel(“登记表”);
Font f0=新字体(“阿尔及利亚”,Font.ITALIC,20);
l0.setFont(f0);
l0.立根(600,10250,50);
面板。添加(l0);
li=新的JLabel(“*字段是必需的”);
Font fi=新字体(“阿拉伯语排版”,Font.PLAIN,17);
li.setFont(fi);
设置前景(颜色:红色);
立根(10,50,150,30);
加上(li);
l1=新的JLabel(“*FirstName:”);
Font f1=新字体(“Bookman旧式”,Font.PLAIN,12);
l1.设置字体(f1);
l1.立根(10,70,100,50);
面板。添加(l1);
t1=新的JTextField(20);
t1.立根(165,85,140,20);
面板。添加(t1);
l2=新的JLabel(