import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class Main extends JFrame{
JLabel imageLabel=new JLabel();
Main()
{
setTitle("메뉴로 배경 이미지 로딩하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
c.add(imageLabel);
createMenu();
setSize(300,250);
setVisible(true);
}
public void createMenu()
{
JMenuBar mb=new JMenuBar();
JMenu m=new JMenu("File");
JMenuItem mi=new JMenuItem("Open");
mi.addActionListener(new MyActionListener());
m.add(mi);
mb.add(m);
setJMenuBar(mb);
}
class MyActionListener implements ActionListener
{
private JFileChooser chooser;
public MyActionListener()
{
chooser=new JFileChooser();
}
public void actionPerformed(ActionEvent e) {
FileNameExtensionFilter filter=new FileNameExtensionFilter("JPG images","jpg");
chooser.setFileFilter(filter);
int ret=chooser.showOpenDialog(null);
if(ret!=JFileChooser.APPROVE_OPTION)
{
return;
}
String filePath=chooser.getSelectedFile().getPath();
imageLabel.setIcon(new ImageIcon(filePath));
pack();
}
}
public static void main(String[] args) {
new Main();
}
}
---------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
private JButton confirmBtn=new JButton("Confirm");
private JTextField tf=new JTextField(10);
Main()
{
setTitle("옵션 팬 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c=getContentPane();
createMenu();
setSize(500,200);
setVisible(true);
}
private void createMenu()
{
JMenuBar mb=new JMenuBar();
JMenu Menu =new JMenu("종료");
mb.add(Menu);
Menu.addMouseListener(new MyMouseListener());
setJMenuBar(mb);
}
class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e) {
int result=JOptionPane.showConfirmDialog(null, "정말 종료 하시겠습니까?","Confirm",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.CLOSED_OPTION)
{
tf.setText("Just Closed without Selection");
}
else if(result==JOptionPane.YES_OPTION)
{
tf.setText("Yes");
System.exit(0);
}
else
{
tf.setText("No");
}
}
}
public static void main(String[] args) {
new Main();
}
}
------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
JTextField tf=new JTextField();
private JButton messageBtn=new JButton("Message");
Container c=getContentPane();
Main()
{
setTitle("숫자가 아닌 키가 입력되는 경우 경고창 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
setSize(500,300);
setVisible(true);
}
private void createMenu()
{
JToolBar toolBar = new JToolBar("Kitae Menu");
toolBar.add(new JButton("학번:"));
toolBar.addSeparator();
tf.addKeyListener(new MyKeyListener());
toolBar.add(tf);
c.add(toolBar, BorderLayout.SOUTH);
}
class MyKeyListener extends KeyAdapter
{
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()=='9'||e.getKeyChar()=='8'||e.getKeyChar()=='7'||e.getKeyChar()=='6'||e.getKeyChar()=='5'||e.getKeyChar()=='4'||e.getKeyChar()=='3'||e.getKeyChar()=='2'||e.getKeyChar()=='1'||e.getKeyChar()=='0')
{
System.out.println(e.getKeyCode()+" "+e.getKeyChar());
}
else {
e.consume();
JOptionPane.showMessageDialog(null,e.getKeyChar()+"는 숫자 키가 아닙니다.\n"+"숫자를 입력하세요","경고",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new Main();
}
}
-------------------- import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyDialog extends Dialog{
JLabel h=new JLabel("두수를 더합니다.");
private static JTextField tf=new JTextField(10);
private static JTextField tf1=new JTextField(10);
private JButton okButton=new JButton("Add");
int d =0;
public MyDialog(JFrame frame,String title)
{
super(frame,title);
setLayout(new FlowLayout());
add(h);
add(tf);
add(tf1);
add(okButton);
okButton.addMouseListener(new MyMouseListener());
setSize(100,200);
}
public class MyMouseListener extends MouseAdapter
{
private int d;
public void mouseClicked(MouseEvent e) {
int a=Integer.valueOf(tf.getText());
int b=Integer.valueOf(tf1.getText());
int c = a+b;
h.setText(Integer.toString(c));
}
private void getC(int c) {
this.d=c;
}
private int setC()
{
return d;
}
}
}
public class Main extends JFrame{
public JLabel l=new JLabel("계산 결과 출력");
public MyDialog dialog;
Main()
{
super("다이얼로그 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b=new JButton("calculate");
dialog=new MyDialog(this,"Calculation Dialog");
Container c=getContentPane();
c.add(l);
c.setLayout(null);
b.setLocation(25, 10);
b.setSize(100,25);
b.setVisible(true);
b.setOpaque(true);
c.add(b);
l.setOpaque(true);
l.setSize(100,25);
l.setVisible(true);
l.setLocation(200, 10);
b.addActionListener(new MyActionListener());
setSize(350,350);
setVisible(true);
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
}
public static void main(String[] args) {
new Main();
}
}



