每个由 GridBagLayout 管理的组件都与 GridBagConstraints 的实例相关联。Constraints 对象指定组件的显示区域在网格中的具体放置位置,以及组件在其显示区域中的放置方式。除了 Constraints 对象之外,GridBagLayout 还考虑每个组件的最小大小和首选大小,以确定组件的大小new GridBagConstraints();创建了GridBagConstraints实例,并给赋各属性默认值; gridx=0;gridy=1;c.ipadx=110;就是改变各属性的值。 其中gridx代表行坐标;gridx=0;gridy=0;就是第一行第一个, 当设置第二行第一个的时候就只用设置gridy=1;就行了,不用设置gridx=0;就是因为设置第一行第一个的时候设置了。------------------------------------------------------------------------------------ public GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady)GridBagConstraints还有这个构造方法,它可以在创建的时候直接设置各属性,这些属性代表什么API里都用 你要创建更复杂的界面都要看一下,因为登录界面比较简单所以我用的比较少。 你的另外一个界面用GridBagLayout再镶套一次GridBagLayout就可以了。 你还有什么不懂么?加我QQ:368279707 我怎么感觉在讲课啊
首先使用两个jsplitpanel1上下方向
再在jsplitpanel1的上面放一个jsplitpanel2左右方向
在jsplitpanel1的下面放一个jsplitpanel3左右方向
整体使用BorderLayout布局,两个JPanel,上面放姓名和密码,下面的JPanel放按钮,可以使用流布局,左、中、右对齐都可以
netbeans 有布局的功能 部件直接往上托就行不过练习的话就别用了
我总体上就是这样布局的,但上面部分,我再使用BorderLayout布局,文字放西边,输入框放东边,但是这样的话就跟模板的有点出入,文字会靠向左边,输入框会靠向右边。但模板是整体居中的。怎样能用最简单的方法解决得了。
另外标题栏那里,怎样才能学模板一样,设置成没有最小最大化按钮?
我自己做的是这样的:
去查一下API
提供的方法可以满足你所有要求
正解啊
setUndecorated(true); // 去掉窗口的装饰
getRootPane().setWindowDecorationStyle(JRootPane.NONE); //采用指定的窗口装饰 风格参数分别为:
NONE 无装饰(即去掉标题栏)
FRAME 普通窗口风格
PLAIN_DIALOG 简单对话框风格
INFORMATION_DIALOG 信息对话框风格
ERROR_DIALOG 错误对话框风格
COLOR_CHOOSER_DIALOG 拾色器对话框风格
FILE_CHOOSER_DIALOG 文件选择对话框风格
QUESTION_DIALOG 问题对话框风格
WARNING_DIALOG 警告对话框风格
我当初学的是清华大学的教材,讲布局管理器的时候有一节专门讲这个布局管理器
教材在清华的网站上可以找到
或者你自己查GridBagLayout的API
思路的话:先把主界面分成2*3的网格,再把右上角的格子分成4*7的网格,其他格子都比较简单
如果还不会的话我写个例子给你~
早上没空
夜里发上来哈
import java.awt.*;
import java.util.*;
import java.applet.Applet; public class GridBagEx1 extends Applet { protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
} public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints(); setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(gridbag); c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c); c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c); c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c); c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c); setSize(300, 100);
} public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1(); ex1.init(); f.add("Center", ex1);
f.pack();
f.setSize(f.getPreferredSize());
f.show();
}
}你先拿这个去参考下
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class login extends JDialog implements ActionListener
{
public login()
{
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setTitle("操作员登陆");
setSize(250,120);
setResizable(false);
setLayout(gridbag);
JLabel lb1 =new JLabel("操作员姓名:");
c.gridx=0;
c.gridy=0;
gridbag.setConstraints(lb1, c);
add(lb1); JLabel lb2 =new JLabel("口令:");
c.gridy=1;
gridbag.setConstraints(lb2, c);
add(lb2); JTextField tf2=new JTextField();
c.gridx=1;
c.ipadx=110;
gridbag.setConstraints(tf2, c);
add(tf2); JTextField tf1=new JTextField();
c.gridy=0;
gridbag.setConstraints(tf1, c);
add(tf1); JButton button =new JButton("登录");
c.gridx=0;
c.gridy=2;
c.gridwidth=2;
c.ipadx=0;
gridbag.setConstraints(button, c);
add(button);
setVisible( true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
}
public static void main( String args[])
{
new login();
}
}
gridx=0;gridy=1;c.ipadx=110;就是改变各属性的值。
其中gridx代表行坐标;gridx=0;gridy=0;就是第一行第一个,
当设置第二行第一个的时候就只用设置gridy=1;就行了,不用设置gridx=0;就是因为设置第一行第一个的时候设置了。------------------------------------------------------------------------------------
public GridBagConstraints(int gridx,
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int anchor,
int fill,
Insets insets,
int ipadx,
int ipady)GridBagConstraints还有这个构造方法,它可以在创建的时候直接设置各属性,这些属性代表什么API里都用
你要创建更复杂的界面都要看一下,因为登录界面比较简单所以我用的比较少。
你的另外一个界面用GridBagLayout再镶套一次GridBagLayout就可以了。
你还有什么不懂么?加我QQ:368279707
我怎么感觉在讲课啊
那这个c.ipadx=110;和c.ipadx=0是为了什么? 其实也没什么,只是程序的习惯有点不同,自己刚学所以看上去比较别扭