public static void main(String[] args){
JFrame f = new JFrame(); // JFrame 객체를 만든다;
JButton b= new JButton("click"); // JButton객체 만든다;
b.setBounds(130,100,100,40);// x축, y축 , 너비 , 높이
f.add(b); // 프레임에 버튼 추가하기
f.setSize(400, 500); // 너비 , 높이
f.setLayout(null); // 레이아웃 관리자를 사용치 않겠다.
f.setVisible(true); // 프레임 시각화 설정
}
JButton 클래스는 플랫폼 독립적 구현이 있는 레이블이 지정된 버튼을 만드는 데 사용됩니다. 버튼을 누르면 응용 프로그램에서 일부 작업이 수행됩니다. AbstractButton 클래스를 상속합니다
public class JButton extends AbstractButton implements Accessible
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
public static void main(String[] args){
JFrame f=new JFrame("Button Example");
final JTextField tf = new JTextField();
tf.setBounds(50,50,150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tf.setText("Welcome to JavatPoint");
}
});
f.add(tf);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
JButton b= new JButton(new ImageIcon(${IMAGE_PATH}));
public class JLabel extends JComponent implements SwingConstants, Accessible
public static void main(String[] args){
JFrame f = new JFrame("Label Example");
JLabel l1, l2;
l1 = new JLabel("First Label");
l1.setBounds(50,50,100,30);
l2 = new JLabel("Second Label");
l2.setBounds(50,100,100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public class LabelExample extends Frame implements ActionListener {
JTextField tf; JLabel l ; JButton b;
LabelExample(){
tf = new JTextField();
tf.setBounds(50,50,150,20);
l = new JLabel();
l.setBounds(50,100,250,20);
b= new JButton("Find IP");
b.setBounds(50,150,95,30);
b.addActionListener(this);
add(b); add(tf); add(l);
setSize(400,400);
setLayout(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String host = tf.getText();
try {
String ip=java.net.InetAddress.getByName(host).getHostAddress();
l.setText("Ip of " + host + " is "+ ip);
} catch (UnknownHostException unknownHostException) {
unknownHostException.printStackTrace();
System.out.println(unknownHostException);
}
}
public static void main(String[] args){
new LabelExample();
}
}
public class JTextField extends JTextComponent implements SwingConstants
public static void main(String[] args){
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public class TextFieldExample implements ActionListener {
JTextField tf1, tf2, tf3;
JButton b1, b2;
TextFieldExample(){
JFrame f= new JFrame();
tf1 = new JTextField();
tf1.setBounds(50,50,150,20);
tf2 = new JTextField();
tf2.setBounds(50,100,150,20);
tf3 = new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEnabled(false); // tf3 임의 변경 불가능
b1 = new JButton("+");
b1.setBounds(50,200,50,50);
b2 = new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String s1 = tf1.getText();
String s2 = tf2.getText();
String minus = b2.getText();
int x = Integer.parseInt(s1);
int y = Integer.parseInt(s2);
int result = 0;
if(e.getSource()== b1){
result = x + y;
}
else if(e.getActionCommand().equals(minus)){
result = x-y;
}
String stringResult = String.valueOf(result);
tf3.setText(stringResult);
}
public static void main(String[] args){
new TextFieldExample();
}
}