2017-11-07 5 views
0

페이지 중앙에 3 개의 JTextField, 버튼 및 레이블이있는 JFrame을 만들었습니다. 사용자가 세 개의 텍스트 필드 모두에 'a'를 입력하고 버튼을 클릭하면 레이블의 텍스트 색상이 빨간색과 같이 다른 색상으로 바뀌는 키를 만듭니다.JTextfield 및 JButtons

내가 가진 주요 문제는 텍스트 필드와 버튼을 연결하여 함께 작동하도록하는 것입니다.

import java.awt.BorderLayout; 
import java.awt.Color; 
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.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 



public class CE203_2017_Ex1 extends JFrame 

{ 
    public static void window() 
     { 
      //Button 
      JButton but = new JButton("Submit"); 
      //JNumberTextField 
      JTextField rgb1 = new JTextField("",3); 
      JTextField rgb2 = new JTextField("",3); 
      JTextField rgb3 = new JTextField("",3); 
      //JLabel 
      JLabel text1 = new JLabel("hello my name is adam ", JLabel.CENTER); 
      text1.setForeground(Color.BLUE); 
      text1.setAlignmentX(0); 
      text1.setAlignmentY(0); 
      //JFrame 
      JFrame window1 = new JFrame("Adam"); 
      window1.setVisible(true); 
      window1.setSize(500,500); 
      window1.add(text1); 
      //JPanel 
      JPanel butPanel = new JPanel(); 
      butPanel.add(rgb1); 
      butPanel.add(rgb2); 
      butPanel.add(rgb3); 
      butPanel.add(but); 

      window1.add(butPanel, BorderLayout.SOUTH); 
      window1.add(text1); 

다음은 버튼 당신은 객체가 아닌 텍스트 필드에 텍스트 값을 테스트 할

but.addActionListener(new ActionListener(){ 

       public void actionPerformed(ActionEvent e) { 
        JTextField input = (JTextField) e.getSource(); 
        String passy = input.getText(); 
        String p = new String (passy); 

        if (rgb1.equals("a")&& rgb2.equals("a")&& rgb3.equals("a")){ 
         text1.setForeground(Color.WHITE); 
        } 
        else{ 
         JOptionPane.showMessageDialog(null, "nocorrect");}}}); 
      } 
    public static void main(String[] args) 
     { 
      window(); 

     } 
} 
+0

코드를 잘못 복사 했습니까? 당신의 주된 메소드는 ActionListener 안에 있고 window() 메소드는 불완전합니다. – user

답변

0

내의 ActionListener입니다.

if (rgb1.getText().equals("a")&& rgb2.getText().equals("a")&& rgb3.getText().equals("a")) { 
    text1.setForeground(Color.WHITE); 
}