배경색 변경
직접 타이핑
result.setOpaque(true);//불투명
class ComboboxFrame
package java12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//콤보박스 : 아이템 선택 기능 + 입력기능
public class ComboboxFrame extends JFrame {
private JPanel contentPane;
private JLabel label;
private JComboBox comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboboxFrame frame = new ComboboxFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ComboboxFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 652, 656);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
comboBox = new JComboBox();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
comboBox=(JComboBox)e.getSource();
String name=(String)comboBox.getSelectedItem();
if(name.equals("강아지")){
name="dog.png";
}else if(name.equals("사자")){
name="lion.png";
}else if(name.equals("호랑이")){
name="tiger.png";
}
ImageIcon icon =new ImageIcon(getClass().getResource("/java12/"+ name));
if(icon!=null){
label.setIcon(icon);
}else{
label.setText("이미지를 선택하세요");
}
}
});
comboBox.setModel(new DefaultComboBoxModel(new String[] {"사자", "호랑이", "강아지"}));
comboBox.setBounds(129, 125, 261, 21);
contentPane.add(comboBox);
label = new JLabel("");
label.setBounds(129, 174, 261, 223);
contentPane.add(label);
}
}
class LoginTest
package java12;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import java.awt.SystemColor;
public class LoginTest extends JFrame {
private JPanel contentPane;
private JTextField id;
private JLabel result;
private JPasswordField pwd;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginTest frame = new LoginTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("아이디");
lblNewLabel.setBounds(70, 30, 57, 15);
contentPane.add(lblNewLabel);
id = new JTextField();
id.setBounds(192, 27, 116, 21);
contentPane.add(id);
id.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("비밀번호");
lblNewLabel_1.setBounds(70, 91, 57, 15);
contentPane.add(lblNewLabel_1);
JButton btnNewButton = new JButton("로그인");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String strId=id.getText();
String strPw=String.valueOf(pwd.getPassword());
if(strId.equals("kim") && strPw.equals("1234")){
result.setText("김철수님 환영합니다.");
result.setForeground(Color.blue); //글자 색 변경
}else{
result.setText("아이디 또는 비밀 번호가 일치하지 않습니다.");
result.setForeground(Color.RED);
}
}
});
btnNewButton.setBounds(139, 143, 97, 23);
contentPane.add(btnNewButton);
result = new JLabel("로그인 결과");
result.setBackground(SystemColor.activeCaption);
result.setBounds(83, 201, 301, 51);
contentPane.add(result);
result.setOpaque(true);//불투명
pwd = new JPasswordField();
pwd.setBounds(192, 88, 116, 21);
contentPane.add(pwd);
}
}
class ProgressTest
package java12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ProgressTest extends JFrame implements Runnable {
private JPanel contentPane;
private JProgressBar pb;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProgressTest frame = new ProgressTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ProgressTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 625, 476);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
pb = new JProgressBar();
pb.setStringPainted(true);
pb.setBounds(71, 83, 401, 35);
contentPane.add(pb);
JButton btn = new JButton("확인");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//백그라운드 스레드 실행
Thread th =new Thread(ProgressTest.this);
th.start();//스레드 시작 요청 => run 메소드 호출
}
});
btn.setBounds(211, 233, 97, 23);
contentPane.add(btn);
}
@Override
public void run() {
for(int i=0; i<=100; i++){
pb.setValue(i); //프레그레스 바의 값을 변경시킴
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(this, "완료되었습니다.");
}
}
class RadioButtonFrame
package java12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.Font;
import java.awt.Color;
public class RadioButtonFrame extends JFrame {
private JPanel contentPane;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JLabel result;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RadioButtonFrame frame = new RadioButtonFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RadioButtonFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 615, 364);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel text = new JLabel("어떤 크기의 커피를 주문하시겠습니까?");
text.setBounds(54, 56, 318, 15);
contentPane.add(text);
JRadioButton small = new JRadioButton("Small Size");
small.setFont(new Font("궁서체", Font.PLAIN, 20));
small.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
result.setText("Small Size 사이즈를 주문 했습니다.");
}
});
buttonGroup.add(small);
small.setBounds(27, 129, 145, 23);
contentPane.add(small);
JRadioButton medium = new JRadioButton("Medium Size");
medium.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
result.setText("Medium Size 사이즈를 주문 했습니다.");
}
});
buttonGroup.add(medium);
medium.setBounds(205, 131, 121, 23);
contentPane.add(medium);
JRadioButton large = new JRadioButton("Large Size");
large.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
result.setText("Large Size 사이즈를 주문 했습니다.");
}
});
buttonGroup.add(large);
large.setBounds(363, 129, 121, 23);
contentPane.add(large);
result = new JLabel("주문내역");
result.setFont(new Font("08서울남산체 M", Font.PLAIN, 20));
result.setForeground(Color.RED);
result.setBounds(73, 235, 397, 45);
contentPane.add(result);
}
}
class SliderFrame2
class TextAreaFrame
package java12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TextAreaFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextAreaFrame frame = new TextAreaFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TextAreaFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 625, 446);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.append(textField.getText() +"\n");
textField.setText(""); //텍스트 필드 지우기
//textField.requestFocus();//입력 커서 설정
textArea.requestFocus();
}
});
contentPane.add(textField, BorderLayout.NORTH);
textField.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
textArea = new JTextArea();
textArea.setRows(10);
textArea.setColumns(30);
scrollPane.setViewportView(textArea);
}
}
class TextFieldFrame
package java12;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TextFieldFrame extends JFrame {
private JPanel contentPane;
private JTextField text;
private JTextField result;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TextFieldFrame frame = new TextFieldFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TextFieldFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 392, 323);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("숫자 입력");
lblNewLabel.setBounds(42, 66, 78, 15);
contentPane.add(lblNewLabel);
text = new JTextField();
text.setBounds(117, 63, 116, 21);
contentPane.add(text);
text.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("결과");
lblNewLabel_1.setBounds(42, 144, 57, 15);
contentPane.add(lblNewLabel_1);
JButton btnNewButton = new JButton("확인");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//사용자가 입력한 값
//Integer.parseInt(문자열) 문자열을 숫자로 변환
// A.equals(B) A와 B의 내용을 비교
// A==B A와 B의 주소값을 비교
if(text.getText().equals("") || text.getText()==null){//빈값이면
//if(text.getText(0=="") { //주소값 비교
//메시지 대화상자 호출
JOptionPane.showMessageDialog(null, "숫자를 입력하세요");
return; //method 를 종료시킴
}
//사용자가 입력한 갑
int num =Integer.parseInt(text.getText());
//제곱한 값 계산
result.setText(num*num+"");
}
});
btnNewButton.setBounds(118, 207, 97, 23);
contentPane.add(btnNewButton);
result = new JTextField();
result.setEditable(false);
result.setBounds(117, 141, 116, 21);
contentPane.add(result);
result.setColumns(10);
}
}
댓글 ( 4)
댓글 남기기