Java中frame对象_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java中frame对象

Java中frame对象

 2018/10/17 12:45:43  andrew7676  程序员俱乐部  我要评论(0)
  • 摘要:1.Java中frame对象1.Frame对象importjava.awt.Color;importjava.awt.Frame;publicclassMyFrameextendsFrame{publicMyFrame(Stringtitle){super(title);}publicstaticvoidmain(String[]args){MyFrameframe=newMyFrame("FirstGUIapp");frame.setSize(500,500);frame
  • 标签:Java
1. Java中frame对象
class="java">
1. Frame对象
import java.awt.Color;
import java.awt.Frame;
public class MyFrame extends Frame {
    public MyFrame(String title) {
        super(title);
    }
    public static void main(String[] args) {
        MyFrame frame = new MyFrame("First GUI app");
        frame.setSize(500, 500);
        frame.setBackground(Color.GREEN);
        frame.setVisible(true);
    }
}

2. Panel对象
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
public class FrameWithPanel extends Frame{
    public FrameWithPanel(String title){
        super(title);
    }
    public static void main(String[] args) {
        FrameWithPanel frame = new FrameWithPanel("frame with panel");
        Panel panel = new Panel();
        frame.setSize(200, 200);
        frame.setBackground(Color.BLACK);
        frame.setLayout(null);
        panel.setSize(100, 100);
        panel.setBackground(Color.YELLOW);
        frame.add(panel);
        frame.setVisible(true);
    }
}

3. lowLayout布局
pack()方法: 根据窗口里面的布局及组件的 preferedSize来确定frame的最佳大小

1) 默认布局
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
public class ExGui {
    private Frame frame;
    private Button button1;
    private Button button2;
    public void go(){
        frame = new Frame("gui example");
        // frame.setLayout(new FlowLayout());
        button1 = new Button("Press me");
        button2 = new Button("Don't press me");
        frame.add(button1);
        frame.add(button2);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        ExGui window = new ExGui();
        window.go();
    }
}

2) BorderLayout布局
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;

public class ExGui2 {
    private Frame frame;
    private Button bn, bs, bw, be, bc;
    public void go(){
        frame = new Frame("Border Layout");
        bn = new Button("B1");
        bs = new Button("B2");
        bw = new Button("B3");
        be = new Button("B4");
        bc = new Button("B5");
        frame.add(bn, BorderLayout.NORTH);
        frame.add(bs, BorderLayout.SOUTH);
        frame.add(bw, BorderLayout.WEST);
        frame.add(be, BorderLayout.EAST);
        frame.add(bc, BorderLayout.CENTER);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        ExGui2 gui = new ExGui2();
        gui.go();
    }
}

3) FlowLayout布局
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
public class MyFlow {
    private Frame frame;
    private Button button1, button2, button3;
    public void go(){
        frame = new Frame("Flow Layout");
        //使用FlowLayout替换掉默认的BorderLayout布局管理器
        frame.setLayout(new FlowLayout()); 
        button1 = new Button("hello");
        button2 = new Button("world");
        button3 = new Button("welcome");
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setSize(100,100);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        MyFlow flow = new MyFlow();
        flow.go();
    }
}

4. 综合使用
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
public class GridEx {
    private Frame frame;
    private Button b1, b2, b3, b4, b5, b6;
    public void go(){
        frame = new Frame("Grid Layout");
        frame.setLayout(new GridLayout(3, 2));
        b1 = new Button("1");
        b2 = new Button("2");
        b3 = new Button("3");
        b4 = new Button("4");
        b5 = new Button("5");
        b6 = new Button("6");
        frame.add(b1);
        frame.add(b2);
        frame.add(b3);
        frame.add(b4);
        frame.add(b5);
        // frame.add(b6);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        GridEx grid = new GridEx();
        grid.go();
    }
}

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
public class ExGui3 {
    private Frame frame;
    private Panel panel;
    private Button b1, b2, b3, b4;
    public void go(){
        frame = new Frame("complex layout");
        b1 = new Button("West");
        b2 = new Button("hello");
        frame.add(b1, BorderLayout.WEST);
        frame.add(b2, BorderLayout.CENTER);
        panel = new Panel();
        b3 = new Button("world");
        b4 = new Button("welcome");
        panel.add(b3);
        panel.add(b4);
        frame.add(panel, BorderLayout.NORTH);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        ExGui3 gui = new ExGui3();
        gui.go();
    }
}
上一篇: Java中awt包 下一篇: Java中exception异常
发表评论
用户名: 匿名