问题描述
代码如下JListjl=newJList(list);//创建列表框jl.setBounds(1,1,299,599);window.add(jl);
解决方案
解决方案二:
当数据多出了范围的时候自动会有滚动条。
解决方案三:
importjava.awt.AWTEvent;importjava.awt.Dimension;importjava.awt.event.WindowEvent;importjavax.swing.JFrame;importjavax.swing.JList;importjavax.swing.JPanel;importjavax.swing.JScrollPane;publicclassFrame1extendsJFrame{JPanelcontentPane;//JListjList1=newJList();String[]str={"Math","English","Physics","Chemic","Biology","Politics"};JListjList1=newJList(str);JScrollPanelistScrollPane=newJScrollPane(jList1);//ConstructtheframepublicFrame1(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{jbInit();}catch(Exceptione){e.printStackTrace();}}//ComponentinitializationprivatevoidjbInit()throwsException{contentPane=(JPanel)this.getContentPane();//contentPane.setLayout(xYLayout1);this.setSize(newDimension(30,100));this.setTitle("FrameTitle");jList1.setVisibleRowCount(4);//contentPane.add(jList1,newXYConstraints(72,86,213,73));contentPane.add(listScrollPane);}//OverriddensowecanexitwhenwindowisclosedprotectedvoidprocessWindowEvent(WindowEvente){super.processWindowEvent(e);if(e.getID()==WindowEvent.WINDOW_CLOSING){System.exit(0);}}}
importjavax.swing.UIManager;importjava.awt.*;/***@authorqzw*/publicclassApplication1{booleanpackFrame=false;//ConstructtheapplicationpublicApplication1(){Frame1frame=newFrame1();//Validateframesthathavepresetsizes//Packframesthathaveusefulpreferredsizeinfo,e.g.fromtheirlayoutif(packFrame){frame.pack();}else{frame.validate();}//CenterthewindowDimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();DimensionframeSize=frame.getSize();if(frameSize.height>screenSize.height){frameSize.height=screenSize.height;}if(frameSize.width>screenSize.width){frameSize.width=screenSize.width;}frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);frame.setVisible(true);}//Mainmethodpublicstaticvoidmain(String[]args){try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){e.printStackTrace();}newApplication1();}}