JAVA_eclipse插件绘制GUI界面过程
- 安装eclipse插件
安装WindowBuilder插件 选择相应的版本install
等待下载安装完成重新启动eclipse
- 插件的使用
新建项目,选择其他
新建Application Window
生成代码界面和design界面
选择design进行gui界面编辑
-
例如创建一个简易的计算器4*4界面布局 首先需要是使用布局 其次创建一个文本框TextField
添加一个面板Panel
同时面板布局采用GridLayout,自动排列,往面板添加按钮
左下角设置按钮参数
依次添加全部按钮最后结果
自动生成的代码块如下(部分)
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
caculatetest window = new caculatetest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public caculatetest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 522, 504);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField();
textField.setColumns(10);
JPanel panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 484, GroupLayout.PREFERRED_SIZE)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 485, GroupLayout.PREFERRED_SIZE))
.addContainerGap(13, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 349, GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
panel.setLayout(new GridLayout(4, 4, 0, 0));
btnNewButton_1 = new JButton("+");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_1);
btnNewButton = new JButton("-");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton);
btnNewButton_2 = new JButton("x");
btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_2);
btnNewButton_3 = new JButton("/");
btnNewButton_3.setEnabled(true);
btnNewButton_3.setFont(new Font("Dialog", Font.PLAIN, 17));
panel.add(btnNewButton_3);
btnNewButton_4 = new JButton("1");
btnNewButton_4.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_4);
btnNewButton_5 = new JButton("2");
btnNewButton_5.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_5);
btnNewButton_6 = new JButton("3");
btnNewButton_6.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_6);
btnNewButton_7 = new JButton("0");
btnNewButton_7.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_7);
btnNewButton_8 = new JButton("4");
btnNewButton_8.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_8);
btnNewButton_9 = new JButton("5");
btnNewButton_9.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_9);
btnNewButton_10 = new JButton("6");
btnNewButton_10.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_10);
btnNewButton_11 = new JButton("c");
btnNewButton_11.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_11);
btnNewButton_12 = new JButton("7");
btnNewButton_12.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_12);
btnNewButton_13 = new JButton("8");
btnNewButton_13.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_13);
btnNewButton_14 = new JButton("9");
btnNewButton_14.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_14);
btnNewButton_15 = new JButton("=");
btnNewButton_15.setFont(new Font("宋体", Font.PLAIN, 17));
panel.add(btnNewButton_15);
frame.getContentPane().setLayout(groupLayout);
|