IT/Java

자바 GUI, JFrame, BorderLayout, GridLayout, NullContainer

노마드오브 2018. 8. 6. 23:29

package java0806;


import java.awt.BorderLayout;

import java.awt.Container;


import javax.swing.JButton;

import javax.swing.JFrame;


public class BorderLayoutEx extends JFrame {


public BorderLayoutEx() {

setTitle("BorderLayout 연습");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Container c = getContentPane();

c.setLayout(new BorderLayout(30, 20));

c.add(new JButton("계산하기"), BorderLayout.CENTER);

c.add(new JButton("덧셈"), BorderLayout.NORTH);

c.add(new JButton("뺄셈"), BorderLayout.SOUTH);

c.add(new JButton("곱셈"), BorderLayout.EAST);

c.add(new JButton("나눗셈"), BorderLayout.WEST);

setSize(300, 200);

setVisible(true);

}

public static void main(String[] args) {

new BorderLayoutEx();


}


}




package java0806;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class GridLayoutEx extends JFrame {

public GridLayoutEx() {
setTitle("GridLayout 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(4, 2);
grid.setVgap(5);
Container c = getContentPane();
c.setLayout(grid);
c.add(new JLabel("이름"));
c.add(new JTextField(""));
c.add(new JLabel("이름"));
c.add(new JTextField(""));
c.add(new JLabel("이름"));
c.add(new JTextField(""));
c.add(new JLabel("이름"));
c.add(new JTextField(""));
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new GridLayoutEx();
}
}


package java0806;

import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class NullContainerEx extends JFrame {

public NullContainerEx() {
setTitle("Null Container 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
JLabel la = new JLabel("안녕, 버튼을 누르세요!");
la.setLocation(130, 50);  // 위치 
la.setSize(200, 20);  // 크기
c.add(la);
for (int i=0; i<=9; i++) {
JButton btn = new JButton(Integer.toString(i));
btn.setLocation(i*15, i*15);
btn.setSize(50, 20);
c.add(btn);
}
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new NullContainerEx();
}

}