📄 gridbaglayoutuse.java
字号:
package chapter11;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GridBagLayoutUse extends Frame {
Button b1 = new Button("红"), b2 = new Button("黄"), b3 = new Button("绿");
TextArea texta = new TextArea(3, 10);
public GridBagLayoutUse() {
super("GridBagLayout");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 2;
gbc.weightx = 1;
gbc.weighty = 1;
add(b1, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight = 2;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(texta, gbc);
gbc.gridx = 4;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
add(b2, gbc);
gbc.gridx = 4;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
add(b3, gbc);
setSize(240, 150);
setVisible(true);
b1.addActionListener(new useListener());
b2.addActionListener(new useListener());
b3.addActionListener(new useListener());
}
class useListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
texta.setBackground(Color.RED);
else if (e.getSource() == b2)
texta.setBackground(Color.YELLOW);
else if (e.getSource() == b3)
texta.setBackground(Color.GREEN);
}
}
public static void main(String args[]) {
GridBagLayoutUse use = new GridBagLayoutUse();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -