명품자바 programming 실습문제 9장
2020. 12. 4. 06:06ㆍProgramming Language/JAVA
5.

import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class Ex5 extends JFrame { public Ex5() { super("4x4 Color Frame"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new GridLayout(4,4)); for (int i=0; i<16; i++) { Color[] col= {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.GRAY, Color.PINK, Color.LIGHT_GRAY, Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE}; JLabel label = new JLabel(i+""); label.setBackground(col[i]); label.setOpaque(true); c.add(label); } setSize(400,200); setVisible(true); } public static void main(String[] args) { new Ex5(); } }
6.

import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; public class Ex6 extends JFrame { public Ex6() { super("Random Labels"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(null); for (int i=0; i<20; i++) { JLabel label = new JLabel(""); String text = Integer.toString(i); label.setText(text); int x = (int)(Math.random()*200) + 50; int y = (int)(Math.random()*200) + 50; label.setBackground(Color.BLUE); label.setSize(10,10); label.setLocation(x,y); label.setOpaque(true); c.add(label); } setSize(300,300); setVisible(true); } public static void main(String[] args) { new Ex6(); } }
7.

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Ex7 extends JFrame { public Ex7() { super("계산기 프레임"); setSize(300,300); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(new NorthPanel(), BorderLayout.NORTH); c.add(new CenterPanel(), BorderLayout.CENTER); c.add(new SouthPanel(), BorderLayout.SOUTH); setVisible(true); } // JPanel에 컴포넌트 붙임. JPanel은 컨테이너이면서 --> JFrame의 컴포넌트 class NorthPanel extends JPanel{ public NorthPanel() { setBackground(Color.LIGHT_GRAY); setLayout(new FlowLayout()); JLabel text = new JLabel("수식 입력"); JTextField exp = new JTextField(15); add(text); add(exp); } } class CenterPanel extends JPanel{ public CenterPanel() { setBackground(Color.BLUE); setLayout(new GridLayout(4,4,5,5)); for (int i=0; i<10; i++) { JButton b = new JButton(Integer.toString(i)); add(b); } JButton b = new JButton("CE"); add(b); b = new JButton("계산"); add(b); JButton [] btn = { new JButton("+"), new JButton("-"), new JButton("X"), new JButton("/") }; for (int i=0; i<btn.length; i++) { btn[i].setBackground(Color.CYAN); add(btn[i]); } } } class SouthPanel extends JPanel{ public SouthPanel() { setBackground(Color.YELLOW); setLayout(new FlowLayout()); JLabel text = new JLabel("계산 결과"); JTextField res = new JTextField(15); add(text); add(res); } } public static void main(String[] args) { new Ex7(); } }
반응형
'Programming Language > JAVA' 카테고리의 다른 글
명품자바 programming 13장 실습문제 (0) | 2020.12.08 |
---|---|
명품자바 programming 11장 실습문제 (0) | 2020.12.04 |
명품자바 programming 실습문제 10장 (0) | 2020.12.04 |
\n과 \r (Escape String) (0) | 2020.12.02 |