/*文章中用到的代码只是一部分,需要源码的可通过邮箱联系我 1978702969@qq.com*/
在上篇博客中提到了JAVA图形界面开发时的两种布局,流式布局和边框布局。
在实际使用中可能会发现,往容器中添加组件往往并不能得到想要的结果。比如想上下对齐两个组件,而流式布局是从左到右的,此时就很难实现上下对齐,此篇文章将介绍两个方法。
1.直接使用坐标贴图
如下面这个计算器的制作
package Graphic; import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField; public class Calculator { public static void main(String[] args) {
Calculator login = new Calculator();
login.Init();
} public void Init()
{
String arr[] = {"1","2","3","4","5","6","7","8","9","0","+","-","*","/","="}; JFrame frame = new JFrame();
frame.setTitle("计算器");
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setResizable(false); frame.setLayout(null); JTextField text1 = new JTextField();
text1.setBounds(40, 20, 400, 40);
frame.add(text1); for(int i=0;i<9;i++)
{
JButton button = new JButton();
button.setText(arr[i]);
button.setBounds((i%3)*100+40, (i/3)*60+80, 80, 40);
frame.add(button);
} JButton button = new JButton();
button.setText("0");
button.setBounds(40, 260, 180, 40);
frame.add(button); JButton button1 = new JButton();
button1.setText(".");
button1.setBounds(240, 260, 80, 40);
frame.add(button1); for(int i=10;i<arr.length - 1;i++)
{
JButton button2 = new JButton();
button2.setText(arr[i]);
button2.setBounds(360, (i-10)*60+80, 80, 40);
frame.add(button2);
} JButton button2 = new JButton();
button2.setText("x^2");
button2.setBounds(460, 80, 80, 40);
frame.add(button2); JButton button3 = new JButton();
button3.setText("√x");
button3.setBounds(460, 140, 80, 40);
frame.add(button3); JButton button4 = new JButton();
button4.setText("=");
button4.setBounds(460, 200, 80, 100);
frame.add(button4); frame.setVisible(true);
}
}
计算器
2.设置组件大小
下面这个界面的制作
package Graphic; import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout; import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField; public class Login1 { public static void main(String[] args) {
// TODO Auto-generated method stub
Login1 login = new Login1();
login.Init();
} public void Init()
{
JFrame frame = new JFrame();
frame.setTitle("QQ");
frame.setSize(430,330);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(3);
frame.setResizable(false); BorderLayout b1 = new BorderLayout();
frame.setLayout(b1);
//添加图片 北
ImageIcon icon = new ImageIcon("C:\\Users\\long452a\\Desktop\\a1.jpg");
JLabel labIcon = new JLabel(icon);
frame.add(labIcon,BorderLayout.NORTH); //添加面板容器:账号、密码
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,5)); JLabel labName = new JLabel("账号:");
labName.setHorizontalAlignment(JLabel.RIGHT);//这步很关键,通过设置右对齐的方式在前面补空
labName.setPreferredSize(new Dimension(110,30));
centerPanel.add(labName); JTextField textName = new JTextField();
textName.setPreferredSize(new Dimension(180,30));
centerPanel.add(textName); JLabel labPassowrd = new JLabel("密码 :");
labPassowrd.setHorizontalAlignment(JLabel.RIGHT);
labPassowrd.setPreferredSize(new Dimension(110,30));
centerPanel.add(labPassowrd); JPasswordField textPassword = new JPasswordField();
textPassword.setPreferredSize(new Dimension(180,30));
centerPanel.add(textPassword,labPassowrd); JPanel centerPanel1 = new JPanel();
centerPanel1.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
JCheckBox rememberBox = new JCheckBox();
rememberBox.setText("记住密码");
rememberBox.setHorizontalAlignment(JLabel.RIGHT);
rememberBox.setPreferredSize(new Dimension(140,30));
centerPanel.add(rememberBox); JCheckBox rememberBox2 = new JCheckBox();
rememberBox2.setText("自动登录");
rememberBox2.setHorizontalAlignment(JLabel.RIGHT);
rememberBox2.setPreferredSize(new Dimension(100,30));
centerPanel.add(rememberBox2); JButton southButton = new JButton();
southButton.setPreferredSize(new Dimension(120,40));
southButton.setText("登录");
centerPanel1.add(southButton); frame.add(centerPanel1, BorderLayout.SOUTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.setVisible(true);
}
}
QQ界面