2014-01-29 2 views
0

간단한 계산기 프로그램에서 텍스트 필드를 확장하려고합니다. 나는 값을 늘리려고 시도했다스윙 패널에서 텍스트 필드를 확대하는 방법은 무엇입니까?

JTextField text1 = 새로운 JTextField (400); 그러나 아무 일도 일어나지 않았습니다.

답변 드리겠습니다. 도와주세요 ...

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class Carl extends JFrame{ 
    JTextField text1= new JTextField (400); 
    JLabel label1= new JLabel (" "); 
    JButton button1= new JButton ("1"); 
    JButton button2= new JButton ("2"); 
    JButton button3= new JButton ("3"); 
    JButton button4= new JButton ("4"); 
    JButton button5= new JButton ("5"); 
    JButton button6= new JButton ("6"); 
    JButton button7= new JButton ("7"); 
    JButton button8= new JButton ("8"); 
    JButton button9= new JButton ("9"); 
    JButton button10= new JButton ("0"); 
    JButton button11= new JButton ("+"); 
    JButton button12= new JButton ("-"); 
    JButton button13= new JButton ("*"); 
    JButton button14= new JButton ("/"); 
    JButton button15= new JButton ("="); 
    JButton button16= new JButton ("C"); 
    JLabel blank1 = new JLabel(""); 
    JLabel blank2 = new JLabel(""); 
    JLabel blank3 = new JLabel(""); 
    JLabel blank4 = new JLabel(""); 
    Font times= new Font ("Times New Roman", Font.BOLD, 14); 
    int operation1; 
    int operation2; 
    int operation3; 
    int operation4; 
    String n1,n2; 
    JPanel p1 = new JPanel(); 
    JPanel p2 = new JPanel(); 

public Carl(){ 
     setResizable(true); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setTitle("CARL DARWIN PAMINTUAN"); 
     setSize(300,300);   
     button1.setForeground(Color.BLACK); 
     button2.setForeground(Color.BLACK); 
     button3.setForeground(Color.BLACK); 
     button4.setForeground(Color.BLACK); 
     button5.setForeground(Color.BLACK); 
     button6.setForeground(Color.BLACK); 
     button7.setForeground(Color.BLACK); 
     button8.setForeground(Color.BLACK); 
     button9.setForeground(Color.BLACK); 
     button10.setForeground(Color.BLACK); 
     button11.setForeground(Color.BLACK); 
     button12.setForeground(Color.BLACK); 
     button13.setForeground(Color.BLACK); 
     button14.setForeground(Color.BLACK); 
     button15.setForeground(Color.BLACK); 
     button16.setForeground(Color.BLACK); 
     button1.setBackground(Color.BLUE); 
     button2.setBackground(Color.BLUE); 
     button3.setBackground(Color.BLUE); 
     button4.setBackground(Color.BLUE); 
     button5.setBackground(Color.BLUE); 
     button6.setBackground(Color.BLUE); 
     button7.setBackground(Color.BLUE); 
     button8.setBackground(Color.BLUE); 
     button9.setBackground(Color.BLUE); 
     button10.setBackground(Color.BLUE); 
     button11.setBackground(Color.BLUE); 
     button12.setBackground(Color.BLUE); 
     button13.setBackground(Color.BLUE); 
     button14.setBackground(Color.BLUE); 
     button15.setBackground(Color.BLUE); 
     button16.setBackground(Color.BLUE); 
     button1.setToolTipText ("one"); 
     button2.setToolTipText ("two"); 
     button3.setToolTipText ("three"); 
     button4.setToolTipText ("four"); 
     button5.setToolTipText ("five"); 
     button6.setToolTipText ("six"); 
     button7.setToolTipText ("seven"); 
     button8.setToolTipText ("eight"); 
     button9.setToolTipText ("nine"); 
     button10.setToolTipText ("zero"); 
     button11.setToolTipText ("addition"); 
     button12.setToolTipText ("subtraction"); 
     button13.setToolTipText ("multiplication"); 
     button14.setToolTipText ("division"); 
     button15.setToolTipText ("equal"); 
     button16.setToolTipText ("clear"); 
     button1.setFont(times); 
     button2.setFont(times); 
     button3.setFont(times); 
     button4.setFont(times); 
     button5.setFont(times); 
     button6.setFont(times); 
     button7.setFont(times); 
     button8.setFont(times); 
     button9.setFont(times); 
     button10.setFont(times); 
     button11.setFont(times); 
     button12.setFont(times); 
     button13.setFont(times); 
     button14.setFont(times); 
     button15.setFont(times); 
     button16.setFont(times); 
     label1.setFont(times); 
     setLayout(new BorderLayout()); 
     add(p1,BorderLayout.CENTER); 
     p1.setLayout(new GridLayout(6,3,2,2)); 
     p1.add(p2,BorderLayout.CENTER); 
     p1.add(label1); 
     p1.add(text1); 
     p1.add(blank3); 
     p1.add(button7); 
     p1.add(button8); 
     p1.add(button9); 
     p1.add(button11); 
     p1.add(button4); 
     p1.add(button5); 
     p1.add(button6); 
     p1.add(button12); 
     p1.add(button1); 
     p1.add(button2); 
     p1.add(button3); 
     p1.add(button13); 
     p1.add(button10); 
     p1.add(button16); 
     p1.add(button15); 
     p1.add(button14); 

button1.addActionListener(new Listener1()); 
     button2.addActionListener(new Listener2()); 
     button3.addActionListener(new Listener3()); 
     button4.addActionListener(new Listener4()); 
     button5.addActionListener(new Listener5()); 
     button6.addActionListener(new Listener6()); 
     button7.addActionListener(new Listener7()); 
     button8.addActionListener(new Listener8()); 
     button9.addActionListener(new Listener9()); 
     button10.addActionListener(new Listener0()); 
     button11.addActionListener(new ListenerAdd()); 
     button12.addActionListener(new ListenerSub()); 
     button13.addActionListener(new ListenerMul()); 
     button14.addActionListener(new ListenerDiv()); 
     button15.addActionListener(new ListenerEqual()); 
     button16.addActionListener(new ListenerClear()); 
} 

    private class Listener1 implements ActionListener{ 
     public void actionPerformed (ActionEvent a){ 
      n1= text1.getText();     
      text1.setText (n1+"1"); 
     } 
    } 


private class Listener2 implements ActionListener{ 
     public void actionPerformed (ActionEvent b){ 
      n1= text1.getText(); 
      text1.setText (n1+"2"); 
     } 
    } 
    private class Listener3 implements ActionListener{ 
     public void actionPerformed (ActionEvent c){ 
      n1= text1.getText(); 
      text1.setText (n1+"3"); 
     } 
    } 
    private class Listener4 implements ActionListener{ 
     public void actionPerformed (ActionEvent d){ 
      n1= text1.getText(); 
      text1.setText (n1+"4"); 
     } 
    } 
    private class Listener5 implements ActionListener{ 
     public void actionPerformed (ActionEvent e){ 
      n1= text1.getText(); 
      text1.setText (n1+"5"); 
     } 
    } 

private class Listener6 implements ActionListener{ 
     public void actionPerformed (ActionEvent f){ 
      n1= text1.getText(); 
      text1.setText (n1+"6"); 
     } 
    } 
    private class Listener7 implements ActionListener{ 
     public void actionPerformed (ActionEvent g){ 
      n1= text1.getText(); 
      text1.setText (n1+"7"); 
     } 
    } 
    private class Listener8 implements ActionListener{ 
     public void actionPerformed (ActionEvent h){ 
      n1= text1.getText(); 
      text1.setText (n1+"8"); 
     } 
    } 
    private class Listener9 implements ActionListener{ 
     public void actionPerformed (ActionEvent i){ 
      n1= text1.getText(); 
      text1.setText (n1+"9"); 
     } 
    } 

private class Listener0 implements ActionListener{ 
     public void actionPerformed (ActionEvent j){ 
      n1= text1.getText(); 
      text1.setText (n1+"0"); 
     } 
    } 
    private class ListenerAdd implements ActionListener{ 
     public void actionPerformed (ActionEvent k){ 
      n2= text1.getText(); 
      operation1=1; 
      text1.setText (""); 
     } 
    } 
    private class ListenerSub implements ActionListener{ 
     public void actionPerformed (ActionEvent l){ 
      n2= text1.getText(); 
      operation2=2; 
      text1.setText (""); 
     } 
    } 

private class ListenerMul implements ActionListener{ 
     public void actionPerformed (ActionEvent m){ 
      n2= text1.getText(); 
      operation3=3; 
      text1.setText (""); 
     } 
    } 

    private class ListenerDiv implements ActionListener{ 
     public void actionPerformed (ActionEvent n){ 
      n2= text1.getText(); 
      operation4=4; 
      text1.setText ("");   
     } 
    } 
    private class ListenerEqual implements ActionListener{ 
     public void actionPerformed (ActionEvent o){ 
      if(operation1==1){ 
       int a=Integer.parseInt(n2); 
       int b=Integer.parseInt(text1.getText()); 
       int sum = (a + b); 
       String sum1 = Integer.toString(sum); 
       text1.setText(sum1); 
      } 

else{ 
      text1.getText(); 
      } 
      if(operation2==2){ 
       int a=Integer.parseInt(n2); 
       int b=Integer.parseInt(text1.getText()); 
       int dif = (a - b); 
       String dif1 = Integer.toString(dif); 
       text1.setText(dif1); 
      } 
      else{ 
      text1.getText(); 
      } 
      if(operation3==3){ 
       int a=Integer.parseInt(n2); 
       int b=Integer.parseInt(text1.getText()); 
       int product = (a * b); 
       String product1 = Integer.toString(product); 
       text1.setText(product1); 
      } 

else{ 
      text1.getText(); 
      } 
      if(operation4==4){ 
       double a=Double.parseDouble(n2); 
       double b=Double.parseDouble(text1.getText()); 
       double quo = (a/b); 
       String quo1 = Double.toString(quo); 
       text1.setText(quo1); 
      } 
      else{ 
      text1.getText(); 
      }  
     } 
    } 
    private class ListenerClear implements ActionListener{ 
     public void actionPerformed (ActionEvent p){ 
      text1.setText(""); 
     } 
    } 
public static void main(String[]args){ 
    Carl frame= new Carl();  
} 
} 
+0

은'에는 setSize (300300)'호출 모습을 의심스러운 (생성자 내의 5 번째 줄). [Java Swing에서 set (Preferred | Maximum | Minimum) Size 메소드를 사용하지 않아야합니까?] (http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods) -in-java-swi) (예, 우리가해야합니다). 컴포넌트 추가를 마치면'pack();'메소드 호출이 빠져 있습니다. – dic19

+0

꽤 긴 경우 400 열 –

+0

배열이나 루프에 대해 들어 보셨습니까? –

답변

1

GridLayout의 모든 구성 요소는 동일한 크기입니다. 다른 LayoutManager를 사용해야 할 수도 있습니다. 그러나 구조 조정을 고려해야합니다 ... 어쨌든 프로그램.

+0

+1 좋은 캐치 !! – dic19

0

답 : @ dic19 답은 여러분이 setSize(300,300)을 사용할 필요가 없다는 것이 나쁜 습관이기 때문입니다. 대신 당신은 또한 나쁜 관행 당신의 프레임에 구성 요소를 추가하기 전에 당신이 setVisible(true)를 사용하는 코드에서 알 다른 Layout Managers.

한 가지 더를 사용해야합니다. 대신 프레임에 모든 구성 요소를 추가 한 후에 setVisible(true)을 사용하고 마지막으로을 사용해야합니다.

JTextField text1= new JTextField (400);이 줄을 사용하려면 다른 레이아웃을 사용해야합니다. 현재 사용중인 레이아웃 관리자로는이 레이아웃을 사용할 수 없습니다.

여기 당신이 적절하게 변경

public class Calculator extends JFrame{ 
    private JButton button1; 
    private final JPanel numPanel,labelPanel; 
    Calculator(){ 
     button1 = new JButton("1"); 
     JButton button2 = new JButton("2"); 
     JButton button3 = new JButton("3"); 
     JButton button_plus = new JButton("+"); 

     JButton button4 = new JButton("4"); 
     JButton button5 = new JButton("5"); 
     JButton button6 = new JButton("6"); 
     JButton button_minus = new JButton("-"); 

     JButton button7 = new JButton("7"); 
     JButton button8 = new JButton("8"); 
     JButton button9 = new JButton("9"); 
     JButton button_multiple = new JButton("*"); 

     JButton button0 = new JButton("0"); 
     JButton clear = new JButton("clear"); 
     JButton delete = new JButton("delete"); 
     JButton button_divide = new JButton("/"); 
     JButton button_equals = new JButton("="); 

     final TextField text = new TextField(15); 

     JFrame frame = new JFrame("CALCULATOR"); 
     frame.setSize(300, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     numPanel = new JPanel(); 
     labelPanel = new JPanel(); 

     numPanel.setLayout(new GridLayout(4, 4)); 
     labelPanel.setLayout(new FlowLayout()); 

     numPanel.add(button1); 
     button1.addActionListener(new ActionListener() { 
       public void actionPerformed(ActionEvent ae) { 
         text.setText(button1.getText()); 
       } 
     }); 

     numPanel.add(button2); 
     button2.addActionListener(null); 

     numPanel.add(button3); 
     button3.addActionListener(null); 

     numPanel.add(button_plus); 
     button_plus.addActionListener(null); 

     numPanel.add(button4); 
     button4.addActionListener(null); 

     numPanel.add(button5); 
     button5.addActionListener(null); 

     numPanel.add(button6); 
     button6.addActionListener(null); 

     numPanel.add(button_minus); 
     button_minus.addActionListener(null); 

     numPanel.add(button7); 
     button7.addActionListener(null); 

     numPanel.add(button8); 
     button8.addActionListener(null); 

     numPanel.add(button9); 
     button9.addActionListener(null); 

     numPanel.add(button_multiple); 
     button_multiple.addActionListener(null); 

     numPanel.add(button0); 
     button0.addActionListener(null); 

     numPanel.add(clear); 
     clear.addActionListener(null); 

     numPanel.add(delete); 
     delete.addActionListener(null); 

     numPanel.add(button_divide); 
     button_divide.addActionListener(null); 

     labelPanel.add(new JLabel("Answer")); 
     labelPanel.add(text); 
     labelPanel.add(button_equals); 
     button_equals.addActionListener(null); 

     frame.add(numPanel,BorderLayout.CENTER); 
     frame.add(labelPanel, BorderLayout.SOUTH); 
     frame.setVisible(true); 
    } 
    public static void main(String args[]) { 
     new Calculator(); 
    } 
} 

출력 JTextField text1= new JTextField (400);를 사용하는 방법을 볼 수있는이 샘플 예제를

봐 :

output

관련 문제