问个JAVA控件位置放置问题
来源:百度知道 编辑:UC知道 时间:2024/07/07 05:47:20
import java.awt.*;
import java.awt.event.*;
class Diary extends Frame
{
public Diary()
{
setSize(1600,800);
setTitle("尝试");
Panel a=new Panel();
a.setBackground(Color.black);
a.setBounds(50,50,30,30);
Panel b=new Panel();
b.setBackground(Color.red);
b.setBounds(50,100,30,30);
this.add(a);
this.add(b);
setVisible(true);
}
public static void main(String[] args)
{
Diary mainFrame=new Diary();
}
}
怎么达不到效果?
.setBounds(40, 40, 30, 30);
后两个30是宽和长
==================
看了你的程序,你再加一句,this.setLayout(null);就可以了,我运行了
如果你不用布局管理器来做的话,可以用VE 或者 WindowBilder 或者 swt 来做,
就是下载以上插件,直接拖动相应的控件到面板里,把主容器layout设为null以后自行设置控件坐标,大小
手写也行:
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class LocationTest extends JFrame {
private static final long serialVersionUID = 1L;
private JComponent c = new JButton("控件一");
public void frameInits() {
this.setLayout(null);//设为null
this.setBounds(150, 120, 400, 300);
this.setBackground(Color.LIGHT_GRAY);
c.setBounds(40, 40, 100, 50);//设定坐标,大小
this.add(c);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new LocationTest().frameInits();
}<