方块布局显示内容

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yongxiang008/article/details/80063991
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * 方块形状显示内容
 */


public class SquareLayout extends RelativeLayout {
    public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLayout(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));

        int childWidthSize = getMeasuredWidth();
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
展开阅读全文

GUI GirdBagLayout布局 显示

07-09

第二个JButton和第三个JButton重叠 rn代码如下:rnimport java.awt.*;rnimport java.awt.event.*;rnimport javax.swing.*;rnrnpublic class test2 extends JApplet //implements ActionListener,ItemListenerrnrn JButton bt1=new JButton("K"),rn bt2=new JButton("安全"),rn bt3=new JButton("设置");rn rn JFrame jf=null; rnrnpublic test2()rn rn jf=new JFrame("QQ用户登录");rn Container cp=jf.getContentPane();rn rn GridBagLayout grid=new GridBagLayout();rn GridBagConstraints c=null;rn int gridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;rn double weightx,weighty;rn Insets inset;rn rn cp.setLayout(grid);rn rn gridx=0;rn gridy=0;rn gridwidth=4;rn gridheight=1;rn weightx=0;rn weighty=0;rn anchor=GridBagConstraints.NORTH;rn fill=GridBagConstraints.NONE;rn inset = new Insets(0,0,0,0); rn ipadx=10;rn ipady=10;rn rn c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,rn weightx,weighty,anchor,fill,inset,ipadx,ipady);rn grid.setConstraints(bt3, c);rn cp.add(bt3);rn rn gridy=1;rn gridheight=2;rn c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,rn weightx,weighty,anchor,fill,inset,ipadx,ipady);rn grid.setConstraints(bt1, c);rn cp.add(bt1);rn rn gridy=2;rn gridheight=1;rn c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,rn weightx,weighty,anchor,fill,inset,ipadx,ipady);rn grid.setConstraints(bt2, c);rn cp.add(bt2);rn rn jf.setBounds(300,400,400,300);rn jf.setVisible(true);rnrnrnrnpublic static void main(String[]args)rnrn new test2(); rnrnrnrn 论坛

没有更多推荐了,返回首页