"κ°μ ν: λͺ
ν JAVAESSENTIAL(ν©κΈ°ν)"κ΅μ¬μ μλ‘λ λ¬Έμ λ€μ νμ΄μ μ¬λ¦½λλ€. λ¬Έμ μ λ²νΈλ§ νκΈ°νκ³ λ΅μλ§ μ λ ννλ‘ μ λ‘λ νκ³ μμ΅λλ€. κ°μΈ νμ΄μ΄λ―λ‘ μ€λ΅μ΄ μμ μ μμΌλ©° μ€λ΅ λ°κ²¬ μ λκΈ λ¨κ²¨μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€π - #μ΄ νμλ 건 μμ§ νμ§ λͺ»ν λ¬Έμ or ν·κ°λ¦¬λ λ¬Έμ μ λλ€! μΆνμ λ€μ νμ΄μ μ¬λ¦΄ μμ μ΄μμ! -ujeyhx- |
πμ€μ΅λ¬Έμ
1) μ΄λ―Έμ§ νμ₯μκ° μ€κ°μ PNG κ° κ»΄μμ΄μ κ·Έλ₯ νλνλ μ½λ©νμ΅λλ€
νμ₯μ ν΅μΌμμΌμ λ°°μ΄λ‘ λ리면 κΉλν μ½λκ° λκ² μ§μ !!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TEST2 extends JFrame {
public TEST2 () {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridLayout(1,4));
ImageIcon img1 = new ImageIcon("images/images.jpg");
ImageIcon img2 = new ImageIcon("images/images (1).jpg");
ImageIcon img3 = new ImageIcon("images/IMG_1611.jpg");
ImageIcon img4 = new ImageIcon("images/λ€μ΄λ‘λ.png");
JLabel la1 = new JLabel(img1);
JLabel la2 = new JLabel(img2);
JLabel la3 = new JLabel(img3);
JLabel la4 = new JLabel(img4);
c.add(la1);
c.add(la2);
c.add(la3);
c.add(la4);
setSize(400,300);
setVisible(true);
}
static public void main(String[] arg) {
new TEST2(); // μ€μ νλ μ μμ±
}
}
2)
import javax.swing.*;
public class TEST2 extends JFrame {
public TEST2 () {
super("test");
createMenu();
setSize(400,300);
setVisible(true);
}
private void createMenu() {
JMenuBar mb = new JMenuBar();
JMenu sM1 = new JMenu("νμΌ");
JMenu sM2 = new JMenu("νΈμ§");
JMenu sM3 = new JMenu("보기");
JMenu sM4 = new JMenu("μ
λ ₯");
sM3.add(new JMenuItem("νλ©΄νλ"));
sM3.addSeparator();
sM3.add(new JMenuItem("μͺ½μ€κ³½"));
mb.add(sM1);
mb.add(sM2);
mb.add(sM3);
mb.add(sM4);
setJMenuBar(mb);
}
static public void main(String[] arg) {
new TEST2(); // μ€μ νλ μ μμ±
}
}
3)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TEST2 extends JFrame {
private JRadioButton red = new JRadioButton("Red");
private JRadioButton blue = new JRadioButton("Blue");
public TEST2() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
ButtonGroup g = new ButtonGroup();
g.add(red);
g.add(blue);
c.add(red);
c.add(blue);
MyItemListener listener = new MyItemListener();
red.addItemListener(listener);
blue.addItemListener(listener);
setSize(250, 100);
setVisible(true);
}
class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() != ItemEvent.SELECTED) //μ΄λ²€νΈλ°μ, μ νλμ§μμΌλ©΄!
return;
if(e.getItem() == red) {
getContentPane().setBackground(Color.RED);
}
else if(e.getItem() == blue) {
getContentPane().setBackground(Color.BLUE);
}
}
}
public static void main(String[] args) {
new TEST2();
}
}
4)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TEST2 extends JFrame {
private JCheckBox cb1 = new JCheckBox ("λ²νΌ λΉνμ±ν");
private JCheckBox cb2 = new JCheckBox ("λ²νΌ κ°μΆκΈ°");
JButton btn = new JButton ("test button");
public TEST2() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
cb1.addItemListener(new MyItemListener());
cb2.addItemListener(new MyItemListener());
c.add(cb1);
c.add(cb2);
c.add(btn);
setSize(300,200);
setVisible(true);
}
private class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED) {
if(e.getItem() == cb1) {
btn.setEnabled(false);
}
if (e.getItem() == cb2){
btn.setVisible(false);
}
}
if(e.getStateChange() != ItemEvent.SELECTED) {
if(e.getItem() == cb1) {
btn.setEnabled(true);
}
if (e.getItem() == cb2){
btn.setVisible(true);
}
}
}
}
public static void main(String[] args) {
new TEST2();
}
}
5) # νμ μ°½ κΈ°λ₯ ꡬνμ λμ€μ λ€μ ,,,, κ³΅λΆ !!!
import java.awt.*;
import java.awt.event.*;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.*;
public class TEST2 extends JFrame {
private JTextArea ta = new JTextArea();
public TEST2() {
super("test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
Container c = getContentPane();
c.add(new JScrollPane(ta)); //μ€ν¬λ‘€λ° μ½μ
setSize(300,200);
setVisible(true);
}
private void createMenu() {
JMenu fileMenu = new JMenu("νμΌ"); //2λ©λ΄
JMenuItem save = new JMenuItem("μ μ₯"); //3λ©λ΄μμ΄ν
fileMenu.add(save);
/* save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(ta.getText().length() == 0) {
JOptionPane.showMessageDialog(TEST2.this,
"μ
λ ₯λ λ΄μ©μ΄ μμ΅λλ€.",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
String fileName = JOptionPane.showInputDialog(TEST2.this,
"μ μ₯ν νμΌλͺ
μ μ
λ ₯νμΈμ");
if(fileName == null) {
JOptionPane.showMessageDialog(TEST2.this,
"νμΌμ μ μ₯νμ§ μμ΅λλ€.",
"Warning", JOptionPane.WARNING_MESSAGE);
return;
}
try {
FileWriter fout = new FileWriter(fileName); // νμΌ μ€νΈλ¦Ό μ΄κΈ°
String t = ta.getText(); // μ
λ ₯λ ν
μ€νΈλ₯Ό λ¬Έμμ΄λ‘ μ»μ΄λ΄κΈ°
StringTokenizer st = new StringTokenizer(ta.getText(), "\n"); // λΌμΈ λ¨μλ‘ λΆλ¦¬
while(st.hasMoreTokens()) {
fout.write(st.nextToken()); // ν λΌμΈ μ°κΈ°
fout.write("\r\n"); // λΌμΈ λμ λ€μ μ€λ‘ λμ΄κ°λ λ¬Έμ μ½μ
}
fout.close(); // νμΌ μ€νΈλ¦Ό λ«κΈ°
} catch (IOException e1) {}
}
}); */
JMenuBar mb = new JMenuBar(); //1λ©λ΄λ°
mb.add(fileMenu);
setJMenuBar(mb); //4νλ μ
}
public static void main(String[] args) {
new TEST2();
}
}
5)
6)