Calculator shape (JLabel and JTextField, JButton, JLabel and JTextField)
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
{
public Main()
{
setTitle("Ten Color Buttons Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(3, 1));
JPanel p = new JPanel();
p.setBackground(Color.GRAY);
p.setLayout(new FlowLayout());
p.add(new JLabel("수식입력"));
p.add(new JTextField(""));
JPanel pan = new JPanel();
pan.setBackground(Color.YELLOW);
pan.setLayout(new FlowLayout());
pan.add(new JLabel("계산결과"));
pan.add(new JTextField(""));
JPanel pa = new JPanel();
pa.setLayout(new GridLayout(4,3));
JButton z = new JButton("0");
pa.add(z);
String[] str = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "CE", "계산", "+", "-", "x", "/"};
for(int i = 0; i < 11; i++)
{
pa.add(new JButton(str[i]));
}
for(int i = 0; i < 4; i++)
{
JButton y = new JButton(str[11+i]);
y.setBackground(Color.CYAN);
pa.add(y);
}
c.add(p);
c.add(pa);
c.add(pan);
setSize(700,500);
setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}
JButton, null layout, JLabel and JTextField
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame
{
public Main()
{
setTitle("여러 개의 패널을 가진 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(3,1))
JPanel one = new JPanel();
one.setLayout(new FlowLayout());
one.setBackground(Color.GRAY);
one.add(new JButton("열기"));
one.add(new JButton("닫기"));
one.add(new JButton("나가기"));
JPanel two = new JPanel();
two.setLayout(null);
for(int i = 0; i < 10; i++)
{
JLabel hi = new JLabel("*");
hi.setForeground(Color.RED);
hi.setSize(10, 10);
hi.setLocation((int)(Math.random()*300) + 100, (int)(Math.random()*300) + 100);
two.add(hi);
}
JPanel three = new JPanel();
three.setLayout(new FlowLayout());
three.setBackground(Color.YELLOW);
three.add(new JLabel("Word Input"));
three.add(new JTextField(" "));
c.add(one);
c.add(two);
c.add(three);
setSize(1000, 800);
setVisible(true);
}
public static void main(String[] args)
{
new Main();
}
}