567
No
class Mylab
package java8;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Mylab {
public static void main(String[] args) {
JFrame f=new JFrame();
//패널 :여러개의 컴포넌트를 담는 그릇
JPanel panel=new JPanel();
f.add(panel); // 프레임에 패널을 붙임
JLabel label1=new JLabel("화씨 온도");
JLabel label2=new JLabel("섭씨 온도");
//1줄만 입력
JTextField field1=new JTextField(15);
JTextField field2=new JTextField(15);
JButton button1=new JButton("화씨->섭씨");
JButton button2=new JButton("섭씨->화씨");
button1.setSize(50, 20);
button2.setSize(50, 20);
panel.add(label1);panel.add(field1);
panel.add(label2);panel.add(field2);
panel.add(button1);panel.add(button2);
JLabel result =new JLabel("출력값");
panel.add(result);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//화씨를 섭씨로 :(화씨 -32)/1.8
// Double.parseDouble(스트링) 스트링을 double로 변환
// 텍스트필드.getText() 텍스트필드.setText("값");
double f =Double.parseDouble(field1.getText());
double c=(f-32)/1.8;
//field2.setText(Double.toString(c));
//String.format("출력형식", 출력할값들)
//%전체자리수.소수이하자리수 f
result.setText(String.format("%7.2f", c));
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//섭씨를 화씨로 : (1.8 x 섭씨) + 32;
double c2 =Double.parseDouble(field2.getText());
double f2=(1.8 * c2)+32;
//field
result.setText(String.format("%7.2f", f2));
}
});
f.setSize(300, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("온도변환기");
f.setVisible(true);
}
}
댓글 ( 4)
댓글 남기기