- Java code
public class TestsetLayout extends JFrame { /** * Leaning * TestsetLayout .java * TODO * 2011-9-20 */ private static final long serialVersionUID = 1L; /** * */ public TestsetLayout () { Button btn=new Button("ok"); btn.setBackground(new Color(0,0,255)); add(btn); setLayout(null); setBounds(20,20,30,30); pack(); this.setVisible(true); } /** * @param args */ public static void main(String[] args) { TestsetLayout t=new TestsetLayout (); }}
显示结果的贴图很麻烦,那我就口述吧。
去掉setLayout(null)或改为setLayout(new XXLayout())就可以正常显示了,望指教
------解决方案--------------------
这是一个在setLayout(null)情况下可以跑的例子
setBound()方法跟pack()方法是矛盾的,所以只能保留一个
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class TestsetLayout extends JFrame { /** * Leaning * TestsetLayout .java * TODO * 2011-9-20 */ private static final long serialVersionUID = 1L; /** * */ public TestsetLayout () { Button btn=new Button("ok"); btn.setBounds(0,0,200,200); btn.setBackground(new Color(0,0,255)); add(btn); setLayout(null); setBounds(20,20,300,300); this.setSize(300,300);// pack(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } /** * @param args */ public static void main(String[] args) { TestsetLayout t=new TestsetLayout (); }}