- import javax.swing.*;
- import java.awt.*;
- public class GuiJava extends JFrame {
- Container container;
- //container size
- public GuiJava (int width, int height, String name) {
- super(name);
- setBasic(width, height);
- }
- public GuiJava (String name) {
- super(name);
- //default
- setBasic(200, 200);
- }
- public void setBasic(int width, int height)
- {
- container = this.getContentPane();
- container.setLayout(null);
- this.setResizable(false);
- this.setVisible(true);
- this.setSize(width, height);
- this.setLocation(500, 300);
- this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
- }
- //component location
- public void addComponentSet(Component compo0, int x, int y)
- {
- compo0.setLocation(x, y);
- //default
- compo0.setSize(50, 50);
- container.add(compo0);
- }
- //component location and size
- public void addComponentSet(Component compo0, int x, int y, int width, int height)
- {
- compo0.setSize(width, height);
- compo0.setLocation(x, y);
- container.add(compo0);
- }
- }
看看是如何使用
- import javax.swing.*;
- public class Sample {
- public static void main (String[] args) {
- GuiJava com = new GuiJava("java");
- JLabel label = new JLabel("hello");
- com.addComponentSet(label, 10, 10);
- }
- }