import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
Main() {
setTitle("계산기 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
JPanel p = new JPanel();
JPanel p1 = new JPanel();
GridLayout grid = new GridLayout(4, 4);
String[] Str = {"CE","계산","+","-","x","/"};
grid.setVgap(1);
JPanel p2 = new JPanel();
c.setLayout(new BorderLayout());
p.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
c.add(p, BorderLayout.NORTH);
p.setBackground(Color.gray);
c.add(p1, BorderLayout.CENTER);
p1.setLayout(grid);
c.add(p2, BorderLayout.SOUTH);
p2.setBackground(Color.yellow);
JLabel la = new JLabel("수식입력");
JLabel la1 = new JLabel("계산결과");
p2.add(la1,BorderLayout.WEST);
p.add(la,BorderLayout.WEST);
JTextField a = new JTextField(17);
JTextField a1 = new JTextField(17);
p.add(a,BorderLayout.CENTER);
p2.add(a1,BorderLayout.CENTER);
for (int i = 0; i < 10; i++) {
JButton J = new JButton(Integer.toString(i));
p1.add(J);
}
for (int i = 0; i < 6; i++) {
JButton J = new JButton(Str[i]);
p1.add(J);
if(i>1)
{
J.setBackground(Color.CYAN);
}
}
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
JPanel p2=new JPanel();
Main()
{
setTitle("여러 개의 패널을 가진 프레임");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
String[] Str= {"열기","닫기","나가기"};
c.setLayout(new BorderLayout());
JPanel p=new JPanel();
JPanel p1=new JPanel();
c.setLayout(new BorderLayout());
p.setLayout(new FlowLayout());
p2.setLayout(new FlowLayout());
c.add(p,BorderLayout.NORTH);
p.setBackground(Color.gray);
c.add(p1,BorderLayout.CENTER);
p1.setBackground(Color.white);
c.add(p2,BorderLayout.SOUTH);
p2.setBackground(Color.yellow);
for(int i=0;i<3;i++)
{
JButton J=new JButton(Str[i]);
J.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
p1.setBackground(Color.black);
}
});
p.add(J);
}
JButton J1=new JButton("Word Input");
p2.add(J1);
JTextField t=new JTextField(15);
p2.add(t);
for(int i=0;i<10;i++)
{
JLabel l=new JLabel("*");
p1.setLayout(null);
int x=(int)(Math.random()*200)+50;
int y=(int)(Math.random()*200)+50;
l.setLocation(x, y);
p1.add(l);
l.setSize(10,10);
l.setForeground(Color.red);
l.setBackground(Color.white);
l.setOpaque(true);
}
setSize(300,300);
setVisible(true);
}
// //inner class로 액션리스너 클래스를 작성
// class MyActionListener implements ActionListener{
//
// public void actionPerformed(ActionEvent e) {
//
// JButton pressed = (JButton)e.getSource();
// if(pressed.getText().equals("나가기")) System.exit(0);
// else pressed.setVisible(false);
// // 패널 색상변경, 버튼 색상변경, 글자 색상변경, 글자 크기 변경, 글자 폰트 변경, 버튼이나 레이블 위치 변경
//
// }
//
// }
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame{
JPanel p=new JPanel();
JTextField t=new JTextField(17);
Main()
{
setTitle("계산기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] Str= {"0","1","2","3","4","5","6","7","8","9","CE","계산","+","-","x","/"};
Container c=getContentPane();
c.setLayout(new BorderLayout());
JPanel p1=new JPanel();
JPanel p2=new JPanel();
GridLayout G=new GridLayout(4,4);
G.setVgap(1);
p.setLayout(new FlowLayout());
p1.setLayout(G);
p2.setLayout(new FlowLayout());
c.add(p,BorderLayout.NORTH);
p.setBackground(Color.gray);
c.add(p1,BorderLayout.CENTER);
c.add(p2,BorderLayout.SOUTH);
p2.setBackground(Color.yellow);
JLabel l=new JLabel("수식입력");
p.add(l);
p.add(t);
JLabel l1=new JLabel("계산결과");
JTextField t1=new JTextField(17);
p2.add(l1);
p2.add(t1);
for(int i=0;i<Str.length;i++)
{
JButton J = new JButton(Str[i]);
J.addActionListener(new MyActionListener());
p1.add(J);
if(i>=12)
{
J.setBackground(Color.cyan);
}
}
setSize(300,300);
setVisible(true);
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton pressed = (JButton)e.getSource();
for(int i=0;i<=9;i++)
{
if(pressed.getText().equals(Integer.toString(i)))
{
t.setText(t.getText()+pressed.getText());
// JLabel lb=new JLabel(Integer.toString(i));
//t.add(lb);
//lb.setForeground(Color.BLACK);
//lb.setSize(10,10);
//lb.setOpaque(true);
}
}
}
}
public static void main(String[] args) {
new Main();
}
}
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame{
Container c = getContentPane();
Main(){
setTitle("드래깅동안 YELLOW...");
c.setBackground(Color.green);
c.addMouseListener(new MyMouseListener());
c.addMouseMotionListener(new MyMouseListener());
setVisible(true);
setSize(400, 400);
}
class MyMouseListener implements MouseListener,MouseMotionListener{
@Override
public void mouseDragged(MouseEvent e) {
c.setBackground(Color.yellow);
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
c.setBackground(Color.green);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
}
// public void mouseEntered(MouseEvent e) {
// la.setText("Love Java");
//
// }
//
// public void mouseExited(MouseEvent e) {
//
// la.setText("사랑해");
// }
}
public static void main(String[] args) {
new Main();
}
}