BIG
Java GUI Swing_001
JFrame
Jpanel
JButton
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javafx.scene.image.Image;
public class swing_001 extends JFrame{
public static JFrame frame = new JFrame();
public static JPanel panel1 =new JPanel();
public static JPanel panel2 =new JPanel();
public static Boolean isUp = false;
public static Boolean isDown = false;
public static Boolean isLeft = false;
public static Boolean isRight = false;
public static void main(String[] args) {
// TODO Auto-generated method stub
JButton btn1 = new JButton(); //btn1이라는 버튼 생성
btn1.setPreferredSize(new Dimension(200, 150));
// btn1.setVisible(true);
btn1.setText("버튼 1 번 입니다.");
// btn1.setSize(300, 150);
btn1.setLocation(100, 100);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("버튼 1 번을 클릭 했습니다.");
frame.dispose();
}});
JButton btn2 = new JButton(); //btn1이라는 버튼 생성
btn2.setPreferredSize(new Dimension(200, 150));
// btn2.setVisible(true);
btn2.setText("버튼 2 번 입니다.");
// btn2.setSize(300, 150);
btn2.setLocation(100, 100);
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("버튼 2 번을 클릭 했습니다.");
frame.dispose();
}});
//panel.setLayout(null);
panel1.setLocation(100, 100);
panel1.setSize(300, 300);
//panel.setPreferredSize(new Dimension(300, 300));
panel1.setBorder(
BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder()
, "Table Title"
, TitledBorder.CENTER
, TitledBorder.TOP
)
);
panel1.add(new JLabel("JPanel 에서 JLabel 출력 테스트"));
panel1.add(btn1); //btn1을 추가
//panel.setLayout(null);
panel2.setLocation(450, 100);
panel2.setSize(120, 120);
//panel.setPreferredSize(new Dimension(300, 300));
// panel2.setBorder(
// BorderFactory.createTitledBorder(
// BorderFactory.createEtchedBorder()
// , "Table Title"
// , TitledBorder.CENTER
// , TitledBorder.TOP
// )
// );
//panel2.add(new JLabel("JPanel 에서 JLabel 출력 테스트"));
String imagPath = "C:\\Users\\tjoeun\\Desktop\\111.png";
ImageIcon imageIcon = new ImageIcon(imagPath);
java.awt.Image image = imageIcon.getImage(); // transform it
java.awt.Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH);
ImageIcon imageIcon1 = new ImageIcon(newimg);
JLabel label = new JLabel("", imageIcon1, JLabel.CENTER);
panel2.add( label );
//panel2.add(btn2); //btn2을 추가
frame.setLayout(null);
frame.setFocusable(true);
class key implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println( e.getKeyCode() );
//int stepWidth = 10;
if( e.getKeyCode() == 37 )
{
isLeft = true;
System.out.println( isLeft );
// panel2.setLocation(panel2.getX()-stepWidth, panel2.getY());
}
if( e.getKeyCode() == 39 )
{
isRight = true;
// panel2.setLocation(panel2.getX()+stepWidth, panel2.getY());
}
if( e.getKeyCode() == 38 )
{
isDown = true;
// panel2.setLocation(panel2.getX(), panel2.getY()-stepWidth);
}
if( e.getKeyCode() == 40 )
{
isUp = true;
// panel2.setLocation(panel2.getX(), panel2.getY()+stepWidth);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println( e );
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println( e );
if( e.getKeyCode() == 37 )
{
isLeft = false;
}
if( e.getKeyCode() == 39 )
{
isRight = false;
}
if( e.getKeyCode() == 38 )
{
isDown = false;
}
if( e.getKeyCode() == 40 )
{
isUp = false;
}
}
}
class CharMove implements Runnable {
public void run() {
while (true) {
System.out.println(isUp);
/*
int stepWidth = 10;
if (isUp == true) {
panel2.setLocation(panel2.getX(), panel2.getY()+stepWidth);
System.out.println("UP ");
}
if (isDown == true) {
panel2.setLocation(panel2.getX(), panel2.getY()-stepWidth);
System.out.println("Down ");
}
if (isLeft == true) {
panel2.setLocation(panel2.getX()-stepWidth, panel2.getY());
System.out.println("LEFT ");
}
if (isRight == true) {
panel2.setLocation(panel2.getX()+stepWidth, panel2.getY());
System.out.println("Right ");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
}
}
}
CharMove charMove = new CharMove();
Thread cThread = new Thread(charMove);
cThread.start();
frame.addKeyListener(new key());
frame.setTitle("Java GUI Swing_001 Frame Form 출력");
frame.setSize(1200, 800);
// set JFrame in center of the screen
// frame.setLocationRelativeTo(null);
frame.setLocation(100, 100);
frame.setVisible(true);
frame.add(panel2);
frame.add(panel1);
}
}
LIST
'!!...JAVA > !!...JAVA' 카테고리의 다른 글
[java]_예제_1 ~ 100 까지의 합 (0) | 2022.04.28 |
---|---|
[java]_예제_3과 5의 공배수 입력시 "빙고" 출력 (0) | 2022.04.28 |
[java]_예제_coffee_order_checkout ( 커피주문계산 ) (0) | 2022.04.27 |
[java]_예제_Judgment_Odd_Even ( 홀수 짝수 구분하기 ) (0) | 2022.04.27 |
[java]_예제_rock_paper_scissors( 가위 바위 보 ) (0) | 2022.04.27 |