java布局
package org.test;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class TFrame extends JFrame {
private JTextArea inputArea,outputArea;
private JButton wordAnalasys,stenceAnalasys;
private GridBagLayout layout;
private Container container;
private GridBagConstraints constraint;
public TFrame(){
super("编译器原理实现词法分析和语法分析");
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
inputArea = new JTextArea(10,10);
addComponent(inputArea, 1, 1, 2, 1);
wordAnalasys = new JButton("词法分析");
addComponent(wordAnalasys, 1, 2, 1, 1);
stenceAnalasys = new JButton("语法分析");
addComponent(stenceAnalasys, 1, 2, 2, 1);
outputArea = new JTextArea(20,10);
addComponent(outputArea, 1, 3, 2, 1);
outputArea.setEditable(false);
this.setSize(300,200);
this.setVisible(true);
}
public void addComponent(Component component, int column, int row,
int width, int height) {
constraint.gridx = column;// 放置组件的列
constraint.gridy = row;// 放置组件的行
constraint.gridwidth = width;// 组件占据的列
constraint.gridheight = height;// 组件占据的行
layout.setConstraints(component, constraint);
container.add(component);
}
public static void main(String args[]){
TFrame tf = new TFrame();
tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
为什么报空指针错误!
布局三行:第一行占两列,文本域;第二行占两列(每列一个button),第三行还是一个文本域;