日期:2014-05-20 浏览次数:20721 次
public class test extends JFrame{ private static final long serialVersionUID = 5699328341463955028L; private JScrollPane jsp = null; private JPanel jp = null; private JPanel jpMainList2 = null; private JTextArea jta = null; private JScrollPane getJsp(){ if(jsp == null){ jsp = new JScrollPane(); jsp.setLayout(null); jsp.setAutoscrolls(true); jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); jsp.add(getjp()); jsp.setSize(500,600); jsp.setBackground(Color.red); } return jsp; } private JPanel getjp(){ if(jp == null){ jp = new JPanel(); jp.setLayout(null); jp.add(getjta()); jp.setLocation(50, 50); jp.setSize(1000,1000); jp.setBackground(Color.white); } return jp; } private JTextArea getjta(){ if(jta == null){ jta = new JTextArea(); jta.setSize(1500, 1500); jta.setBackground(Color.CYAN); } return jta; } public test(){ this.setLayout(null); this.add(getJsp()); this.setExtendedState(Frame.MAXIMIZED_BOTH); this.setVisible(true); } public static void main(String[] args){ new test(); } }
import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; @SuppressWarnings("serial") public class Test extends JPanel { @Override public Dimension getPreferredSize() { return new Dimension(600, 600); // 这个值只是推荐值,并不一定会使用到 } private static void createGuiAndShow() { JFrame frame = new JFrame(""); JPanel panel = new JPanel(); panel.add(new JButton("....Button....")); panel.setPreferredSize(new Dimension(600, 600)); // 这里设置才有用 JScrollPane scroller = new JScrollPane(panel); frame.getContentPane().add(scroller); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int sw = Toolkit.getDefaultToolkit().getScreenSize().width; int sh = Toolkit.getDefaultToolkit().getScreenSize().height; int w = 400; int h = 400; int x = (sw - w) / 2; int y = (sh - h) / 2 - 40; x = x > 0 ? x : 0; y = y > 0 ? y : 0; frame.setBounds(x, y, w, h); frame.setVisible(true); } public static void main(String[] args) { createGuiAndShow(); } }