package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class project extends JFrame {
Container contentPane;
static int sum = 0;
project(){
setTitle("프로젝트 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
setSize(500,200);
c.add(new MyPanel(),BorderLayout.NORTH);
c.add(new SumPanel(),BorderLayout.CENTER);
setVisible(true);
}
class MyPanel extends Panel
{
String Name[] = {"사과","바나나","딸기"};
JButton inputBtn = new JButton("구매");
JTextField tf = new JTextField(10);
JButton inputtn = new JButton("판매");
JButton messageBtn = new JButton("종료");
MyPanel(){
setBackground(Color.LIGHT_GRAY);
add(inputBtn);
add(inputtn);
add(messageBtn);
add(tf);
inputBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name = JOptionPane.showInputDialog("이름을 입력하세요");
if(name != null)
{
tf.setText(name);
if(name.equals("사과"))
sum += 100;
else if(name.equals(Name[1]))
sum += 200;
else if(name.equals(Name[2]))
sum += 300;
}
}
});
inputtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String result = JOptionPane.showInputDialog("이름을 입력하세요");
if(result != null)
tf.setText(result);
{if(result.equals(Name[0]))
sum -= 100;
else if(result.equals(Name[1]))
sum -= 200;
else if(result.equals(Name[2]))
sum -= 300;}
}
});
messageBtn.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
}
class SumPanel extends Panel{
JLabel sumLabel = new JLabel("결과");
JTextField tq = new JTextField(10);
SumPanel()
{
add(sumLabel);
add(tq);
tq.setText(Integer.toString(sum));
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new project();
}
}
다음과 같은 코드인데요
MyPanel 클래스에서 사과나 바나나 딸기를 입력하면 그걸 비교해서
sum 값을 증감시키는 코딩입니다.
근데 아무리 봐도 sum값이 계속 0이네요;;
뭐가 문제인지 혹시 아시는분 계신가요...