frame------>顶级窗口
frame.setVisible(true);设置可见性
frame.setSize();设置窗口大小
frame.setBackground();设置背景颜色
frame.setLocation();弹出的初始位置
frame.setResizable();设置大小固定
监听窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);正常退出
}
});}}
panel----->无法单独显示,必须添到容器
设置相对frame坐标,
panel.setBounds();
panel.setBackground(new Color());背景颜色
布局管理
Button button = new Button("button");设置组件按钮
流式
frame.setLayout(new FlowLayout());设置为流式布局
frame.add(button);添加按钮
东西南北中
Button button= new Button("East");
frame.add(button,BorderLayout.EAST);
表格
Button button = new Button("button");
frame.setLayout(new GridLayout(2,4));2行4列
frame.add(button);
frame.pack();根据窗口里面的布局及组件来确定frame的最佳大小
练习题
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setLayout(new GridLayout(2,1));
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("East1"),BorderLayout.EAST);
p1.add(new Button("West1"),BorderLayout.WEST);
p2.add(new Button("p2btn1"));
p2.add(new Button("p2btn2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("East2"),BorderLayout.EAST);
p3.add(new Button("West2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
p4.add(new Button(i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
事件监听
Frame frame = new Frame();
Button button = new Button();按钮用于触发事件
MyActionListener myActionListener = new MyActionListener();构造一个 ActionListener
button.addActionListener(myActionListener);
.......
关闭窗体事件
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
}}事件监听
多按钮共享同一个事件
public class Demo {
public static void main(String[] args) {
Frame frame = new Frame("多按钮共享");
Button button1 = new Button("start");
Button button2 = new Button("stop");
button2.setActionCommand("button2stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
if (e.getActionCommand().equals("start")){
}
}}
输入框 TextField 监听
public class TestText01 {
public static void main(String[] args) {
//启动!
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
MyActionListener myActionListener = new MyActionListener();
textField.addActionListener(myActionListener);
textField.setEchoChar(‘*‘);
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
System.out.println(field.getText());
field.setText("");
}
}
画笔
setColor();颜色
fillOval(100,100,100,100); 圆
fillRect(150,200,200,200);矩形
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("上键");
}
}
});
}
}
窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
原文:https://www.cnblogs.com/arroa/p/11992887.html