📄 布局管理器layoutmanager.txt
字号:
if(isLongTextFields(mjc1))
{
starty+=HEIGHT+m_vGap;
}
}
}
}
}
public int getHGap()
{
return m_hGap;
}
public int getVGap()
{
return m_vGap;
}
public String toString()
{
return "javamonkey";
}
//-----------------
protected Dimension getPreferredSize(Vector v,Container parent)
{
int iSize = v.size();
//int cSize = parent.getPreferredSize();
int lbWidth = 0;
int sfWidth = 0;
int mjcWidth = 0;
int height = 0;
Insets insets = parent.getInsets();
for(int i = 0;i<iSize;i++)
{
Component mjc = (Component)v.get(i);
int count = this.find(parent,mjc);
Component label = parent.getComponent(count-1);
Component suffix = parent.getComponent(count+1);
lbWidth = Math.max(lbWidth,label.getPreferredSize().width);
sfWidth = Math.max(sfWidth,suffix.getPreferredSize().width);
mjcWidth = Math.max(mjcWidth,mjc.getPreferredSize().width);
//高度都定为label高度
//height = height+label.getPreferredSize().height+m_vGap;
height = height+HEIGHT+m_vGap;
}
int thisWidth = lbWidth+m_hGap+mjcWidth+m_hGap+sfWidth;
int thisHeight = height;
return new Dimension(thisWidth,thisHeight);
}
private Divider getDivider(Vector v,Container parent)
{
int iSize = v.size();
//int cSize = parent.getPreferredSize();
int lbWidth = 0;
int sfWidth = 0;
int mjcWidth = 0;
for(int i = 0;i<iSize;i++)
{
Component mjc = (Component)v.get(i);
int count = this.find(parent,mjc);
Component label = parent.getComponent(count-1);
Component suffix = parent.getComponent(count+1);
lbWidth = Math.max(lbWidth,label.getPreferredSize().width);
sfWidth = Math.max(sfWidth,suffix.getPreferredSize().width);
mjcWidth = Math.max(mjcWidth,mjc.getPreferredSize().width);
}
return new Divider(lbWidth,sfWidth);
}
private Vector[] getSimpleComponents(Container parent,int col)
{
Vector v = new Vector();
for (int k=1 ; k<parent.getComponentCount(); k+=3)
{
Component mjc = (Component)parent.getComponent(k);
if(!isLongTextFields(mjc))v.add(mjc);;
}
Vector[] array = new Vector[col];
for (int k=0 ; k<col; k++)
{
array[k] = new Vector();
}
for (int k=0 ; k<v.size(); k++)
{
int cols = k%col;
Component mjc = (Component)v.get(k);
array[cols].add(mjc);
}
return array;
}
private Vector getComplexComponents(Container parent)
{
Vector v = new Vector();
for (int k=1 ; k<parent.getComponentCount(); k+=3)
{
Component mjc = (Component)parent.getComponent(k);
if(isLongTextFields(mjc))v.add(mjc);;
}
return v;
}
private int getComplexDivider(Container parent)
{
Vector v = getComplexComponents(parent);
int iSize = v.size();
int lbWidth = 0;
for(int i = 0;i<iSize;i++)
{
Component mjc = (Component)v.get(i);
int count = this.find(parent,mjc);
Component label = parent.getComponent(count-1);
lbWidth = Math.max(lbWidth,label.getPreferredSize().width);
}
return lbWidth;
}
private boolean isLongTextFields(Component mjc)
{
if(mjc instanceof javax.swing.JTextArea) return true;
if(mjc instanceof javax.swing.JPanel) return true;
else
{
return false;
}
}
private int find(Container parent,Component cp)
{
Component[] cps = parent.getComponents();
for(int i=0;i<cps.length;i++)
{
if(cp.equals(cps[i]))return i;
}
return -1;
}
}
//用于表示每个列的所有preLable,suffixLabel最大值
class Divider
{
int left;
int right;
public Divider(int left,int right)
{
this.left = left;
this.right =right;
}
}[/pre]
应用布局管理器
凡是刚接触swing的,无不对布局头疼,以下是flowLayout和DialogLayout的综合使用,可以看到,只用它们俩个(有时候,还需要用BorderLayout用于宏观布局),就可以轻松做出比较美观的复杂界面
列子如下
[pre]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author javamonkey
* @version 1.0
*/
public class Frame4 extends JFrame {
private JPanel contentPane;
//Construct the frame
public Frame4() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame4.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new DialogLayout());
//第一组布局单元
contentPane.add(new JLabel("date:"));
contentPane.add( new JTextField());
contentPane.add( new suffixLabel());
//第二组布局单元
contentPane.add( new JLabel("from:"));
contentPane.add( new JTextField());
contentPane.add( new suffixLabel());
//第三组布局单元
contentPane.add( new JLabel("memo:"));
JTextArea tx = new JTextArea();
tx.setPreferredSize(new Dimension(0,50));
contentPane.add(tx );
contentPane.add( new suffixLabel());
//第四组布局单元
contentPane.add( new JLabel("cash:"));
contentPane.add(new JTextField());
contentPane.add( new suffixLabel("$"));
//第五组布局单元
JPanel pl = new JPanel();
pl.setLayout(new FlowLayout(FlowLayout.LEFT));
pl.setBorder(BorderFactory.createEtchedBorder());
pl.add(new JLabel("性别:"));
pl.add(new JRadioButton("男"));
pl.add(new JRadioButton("女"));
contentPane.add(new Label());
contentPane.add(pl);
contentPane.add(new suffixLabel());
//
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
class suffixLabel extends JLabel
{
public suffixLabel(String str)
{
super(str);
this.setPreferredSize(new Dimension(10,0));
}
public suffixLabel()
{
super();
}
}[/pre]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -