center.setLayout(new GridLayout(3,4)); 你這個(gè)是3行4列 也就是能放12個(gè)的 但是你卻放了16個(gè)button 當(dāng)然不行啦
創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站、網(wǎng)站重做改版、十堰鄖陽(yáng)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為十堰鄖陽(yáng)等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
寫的時(shí)候仔細(xì)點(diǎn),setLyaout方法里面有錯(cuò)
lbUser=new JLabel("用戶名");
gbLayout.setConstraints(tfUser,constraints);
container.add(tfUser);
你這里把 lbUser 指向了對(duì)象,此是的tfUser還沒有,而你加的時(shí)候確是加的tfUser, 你把 tfUser改過(guò)來(lái)就好了
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("網(wǎng)格包布局");
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);
// 添加用戶名標(biāo)簽
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);
// 添加密碼標(biāo)簽
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();
}
}
出現(xiàn):“Appletviewer”不是內(nèi)部或外部命令
是因?yàn)闆]有配置運(yùn)行java的環(huán)境。
配置java運(yùn)行環(huán)境:
1.若jdk安裝在C:\jdk1.6
2.點(diǎn)擊我的電腦-屬性-高級(jí)-環(huán)境變量
3.新建系統(tǒng)變量:
變量名 變量值
JAVA_HOME C:\jdk1.6
CLASSPATH .;%JAVA_HOME%\lib
path %JAVA_HOME%\bin;
[注意:第三個(gè)變量path原先已存在值,不要覆蓋,只要在前面將 %JAVA_HOME%\bin; 添加到前面就可以了]
android 使兩個(gè)按鈕水平排列的方法是使用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="確認(rèn)對(duì)沖"
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
運(yùn)行結(jié)果如下:
null layout不是很好,所有的東西都要自己定義,一旦窗口大小變化就要重新計(jì)算。我建議使用MigLayout。上手慢,但很強(qiáng)大。
而且,就算你要用內(nèi)建的Layout Manager,還有一個(gè)GridBag Layout,比Grid Layout更強(qiáng)大一點(diǎn)。
接下來(lái),那個(gè)邊框,不是JSeparator, 而是一個(gè)邊框。我猜是
Border result = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "修改信息: ");
如果是我,使用MigLayout,以上代碼還是比較簡(jiǎn)單的:
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這個(gè)panel有5像素的邊緣,2列,3行。第一列右對(duì)齊,占40%寬度;第二列默認(rèn),左對(duì)齊,占據(jù)剩余所有空間。
first.setLayout(new?MigLayout("insets?5,?fill",?"[right,?40%]5[fill,?grow]",?"[]5[]5[]"));
first.setOpaque(false);
//這個(gè)面板的border有些特殊:createTitledBorder()方法的簽名可以有兩個(gè):前一個(gè)是線的類型,后面一個(gè)是標(biāo)題文本。
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("確認(rèn)新密碼:?");
JTextField?orig_field?=?new?JTextField();
JTextField?new_field?=?new?JTextField();
JTextField?confirm_field?=?new?JTextField();
//miglayout的核心就是網(wǎng)格排布。用坐標(biāo)來(lái)定義添加元素的位置
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像素的間距,列內(nèi)元素居中;有一行,行中上下對(duì)齊也是居中
buttons.setLayout(new?MigLayout("insets?5,?fill",?"[center]10[center]",?"[center]"));
JButton?yes?=?new?JButton("Y.?確定");
//快捷鍵設(shè)為虛擬鍵Y,得到下劃線效果
yes.setMnemonic(KeyEvent.VK_Y);
JButton?quit?=?new?JButton("Q.?退出");
//快捷鍵設(shè)為虛擬鍵Q,得到下劃線效果
quit.setMnemonic(KeyEvent.VK_Q);
//把yes按鈕加到第一列第一行,min:pref:max的大小都設(shè)為100像素
buttons.add(yes,?"cell?0?0,?w?100!");
//把quit按鈕加到第二列第一行,min:pref:max的大小都設(shè)為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還允許你使用輔助線來(lái)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 流式布局,從左到右,如果到邊界就換行再?gòu)淖蟮接摇?/p>
BorderLayout 邊界布局(默認(rèn)布局方式),按東西南北中五個(gè)方向來(lái)布局,默認(rèn)是中。后設(shè)置在同樣位置的控件會(huì)覆蓋之前的控件。
GridLayout 網(wǎng)格布局,將容器劃分成若干行列的網(wǎng)格,從左到右,然后從上到下。每個(gè)控件的大小相同。