十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
center.setLayout(new GridLayout(3,4)); 你这个是3行4列 也就是能放12个的 但是你却放了16个button 当然不行啦
创新互联专注于企业营销型网站、网站重做改版、十堰郧阳网站定制设计、自适应品牌网站建设、H5建站、成都做商城网站、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为十堰郧阳等各大城市提供网站开发制作服务。
写的时候仔细点,setLyaout方法里面有错
lbUser=new JLabel("用户名");
gbLayout.setConstraints(tfUser,constraints);
container.add(tfUser);
你这里把 lbUser 指向了对象,此是的tfUser还没有,而你加的时候确是加的tfUser, 你把 tfUser改过来就好了
import javax.swing.*;
import java.awt.*;
public class GridBagLayoutDemo extends JFrame {
private GridBagLayout gbLayout = new GridBagLayout();
private GridBagConstraints constraints = new GridBagConstraints();
private JLabel lbUser, lbPassword;
private JTextField tfUser, tfPassword;
private JButton btnLog;
private Container container;
public GridBagLayoutDemo() {
super("网格包布局");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setConstraints(GridBagConstraints gbc, int row, int column,
int numRows, int numColumns, int Weightx, int Weighty) {
gbc.gridx = row;
gbc.gridy = column;
gbc.gridwidth = numRows;
gbc.gridheight = numColumns;
gbc.weightx = Weightx;
gbc.weighty = Weighty;
}
public void setLyaout() {
container = this.getContentPane();
container.setLayout(gbLayout);
// 添加用户名标签
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
setConstraints(constraints, 0, 0, 1, 1, 0, 0);
lbUser = new JLabel("用户名");
gbLayout.setConstraints(lbUser, constraints);
container.add(lbUser);
// 添加用户名文本框
constraints.fill = GridBagConstraints.HORIZONTAL;
setConstraints(constraints, 1, 0, 1, 1, 100, 100);
tfUser = new JTextField();
gbLayout.setConstraints(tfUser, constraints);
container.add(tfUser);
// 添加密码标签
constraints.fill = GridBagConstraints.NONE;
setConstraints(constraints, 0, 1, 1, 1, 0, 0);
lbPassword = new JLabel("密码");
gbLayout.setConstraints(lbPassword, constraints);
container.add(lbPassword);
// 添加密码文本框
constraints.fill = GridBagConstraints.HORIZONTAL;
setConstraints(constraints, 1, 1, 1, 1, 100, 100);
tfPassword = new JTextField();
gbLayout.setConstraints(tfPassword, constraints);
container.add(tfPassword);
// 添加登录按钮
constraints.fill = GridBagConstraints.CENTER;
setConstraints(constraints, 0, 2, 2, 1, 0, 0);
btnLog = new JButton("登录");
gbLayout.setConstraints(btnLog, constraints);
container.add(btnLog);
}
public static void main(String[] args) {
GridBagLayoutDemo frame = new GridBagLayoutDemo();
frame.setLyaout();
frame.show();
}
}
出现:“Appletviewer”不是内部或外部命令
是因为没有配置运行java的环境。
配置java运行环境:
1.若jdk安装在C:\jdk1.6
2.点击我的电脑-属性-高级-环境变量
3.新建系统变量:
变量名 变量值
JAVA_HOME C:\jdk1.6
CLASSPATH .;%JAVA_HOME%\lib
path %JAVA_HOME%\bin;
[注意:第三个变量path原先已存在值,不要覆盖,只要在前面将 %JAVA_HOME%\bin; 添加到前面就可以了]
android 使两个按钮水平排列的方法是使用lineLayout线性布局,如下代码:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
View
android:layout_width="wrap_content"
android:layout_height="1.2px"
android:layout_marginBottom="7dp"
android:background="@color/white" /
LinearLayout
android:layout_width="fill_parent"
android:layout_height="79dp"
android:layout_weight="2"
android:orientation="horizontal"
android:layout_margin="10dp"
Button
android:id="@+id/bt1"
android:layout_width="fill_parent"
android:layout_height="26dp"
android:background="@drawable/shape"
android:layout_weight="1"
android:text="确认对冲"
android:textColor="@color/white"
android:textSize="15dp" /
Button
android:layout_width="fill_parent"
android:layout_height="26dp"
android:background="@drawable/shapeyuanjiao"
android:layout_weight="1"
android:text="取消"
android:textColor="@color/white"
android:textSize="15dp" /
/LinearLayout
/LinearLayout
运行结果如下:
null layout不是很好,所有的东西都要自己定义,一旦窗口大小变化就要重新计算。我建议使用MigLayout。上手慢,但很强大。
而且,就算你要用内建的Layout Manager,还有一个GridBag Layout,比Grid Layout更强大一点。
接下来,那个边框,不是JSeparator, 而是一个边框。我猜是
Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ");
如果是我,使用MigLayout,以上代码还是比较简单的:
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import net.miginfocom.swing.MigLayout;
public class MyFrame extends JFrame {
public MyFrame() {
begin();
}
private void begin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new MigLayout("insets 10, fill", "[]", "[]5[]"));
//first panel
JPanel first = new JPanel();
//first这个panel有5像素的边缘,2列,3行。第一列右对齐,占40%宽度;第二列默认,左对齐,占据剩余所有空间。
first.setLayout(new MigLayout("insets 5, fill", "[right, 40%]5[fill, grow]", "[]5[]5[]"));
first.setOpaque(false);
//这个面板的border有些特殊:createTitledBorder()方法的签名可以有两个:前一个是线的类型,后面一个是标题文本。
Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ");
first.setBorder(result);
//把first加到contentPane里面
add(first, "cell 0 0, grow");
JLabel original = new JLabel("输入原密码: ");
JLabel newPass = new JLabel("输入新密码: ");
JLabel confirm = new JLabel("确认新密码: ");
JTextField orig_field = new JTextField();
JTextField new_field = new JTextField();
JTextField confirm_field = new JTextField();
//miglayout的核心就是网格排布。用坐标来定义添加元素的位置
first.add(original, "cell 0 0, w 100!"); //add to col 0, line 0, min:pref:max width all set to 100
first.add(newPass, "cell 0 1, w 100!"); //add to col 0, line 1
first.add(confirm, "cell 0 2, w 100!"); //add to col 0, line 2
first.add(orig_field, "cell 1 0, w 150!"); //add to col 1, line 0, min:pref:max width all set to 150
first.add(new_field, "cell 1 1, w 150!"); //add to col 1, line 1
first.add(confirm_field, "cell 1 2, w 150!"); //add to col 1, line 2
//按钮面板
JPanel buttons = new JPanel();
buttons.setOpaque(false);
//边缘为5像素;有两列,中间是10像素的间距,列内元素居中;有一行,行中上下对齐也是居中
buttons.setLayout(new MigLayout("insets 5, fill", "[center]10[center]", "[center]"));
JButton yes = new JButton("Y. 确定");
//快捷键设为虚拟键Y,得到下划线效果
yes.setMnemonic(KeyEvent.VK_Y);
JButton quit = new JButton("Q. 退出");
//快捷键设为虚拟键Q,得到下划线效果
quit.setMnemonic(KeyEvent.VK_Q);
//把yes按钮加到第一列第一行,min:pref:max的大小都设为100像素
buttons.add(yes, "cell 0 0, w 100!");
//把quit按钮加到第二列第一行,min:pref:max的大小都设为100像素
buttons.add(quit, "cell 1 0, w 100!");
//把按钮面板加到contentPane里面
add(buttons, "cell 0 1, grow");
pack();
setBounds(0, 0, 500, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame frame = new MyFrame();
}
});
}
}
效果是这样:
MigLayout还允许你使用辅助线来debug。把这一行:
first.setLayout(new MigLayout("insets 5, fill", "[right, 40%]5[fill, grow]", "[]5[]5[]"));
改成:
first.setLayout(new MigLayout("insets 5, fill,debug", "[right, 40%]5[fill, grow]", "[]5[]5[]"));
你就可以看到first面板里面的辅助线了。
FlowLayout 流式布局,从左到右,如果到边界就换行再从左到右。
BorderLayout 边界布局(默认布局方式),按东西南北中五个方向来布局,默认是中。后设置在同样位置的控件会覆盖之前的控件。
GridLayout 网格布局,将容器划分成若干行列的网格,从左到右,然后从上到下。每个控件的大小相同。