2013-01-17 6 views
0

기본적으로 저는 약간 혼란 스럽습니다. 저는 이클립스 용 WindowsPro Builder 플러그인을 사용하고 있으며, 사용자 정의 initialize() 클래스의 모든 JFrame 구성 요소를 만듭니다. 이것은 저를위한 질문을 창조합니다, 일반적으로 나는 처음에 구성 요소를 정의하므로 공개적으로 저의 프로그램에 액세스 할 수 있습니다. 아니요. 두 번째 수업이 있지만 내 구성 요소에 액세스 할 수 없습니다. 예를 들어 전체 초기화 클래스에 대해 통합 된 ActionListener를 만드는 방법을 이해할 수 없습니다.JFrame을 적절하게 사용하는 방법

또한 입력란을 텍스트 영역에서 가져오고 싶지만 어떻게 할 수 있습니까? 모든 것이 범위를 벗어 났을 때? 당신은 클래스 SaveToFile을 호출 할 수 있습니다, 그 클래스에서 나는 textarea에서 입력을 얻고 싶습니다,하지만 어떻게해야합니까? 일식에 대한

import javax.swing.*; 


public class FunctionsGUI { 

private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       FunctionsGUI window = new FunctionsGUI(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public FunctionsGUI() { 
    initialize(); 

} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 

    try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch(Exception e) { 
      System.out.println("Error setting native LAF: " + e); 
     } 

    frame = new JFrame(); 
    frame.setBounds(100, 100, 571, 531); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    SpringLayout springLayout = new SpringLayout(); 
    frame.getContentPane().setLayout(springLayout); 

    JTextPane textPane = new JTextPane(); 
    springLayout.putConstraint(SpringLayout.NORTH, textPane, 10, SpringLayout.NORTH, frame.getContentPane()); 
    springLayout.putConstraint(SpringLayout.WEST, textPane, 10, SpringLayout.WEST, frame.getContentPane()); 
    springLayout.putConstraint(SpringLayout.SOUTH, textPane, 462, SpringLayout.NORTH, frame.getContentPane()); 
    springLayout.putConstraint(SpringLayout.EAST, textPane, 545, SpringLayout.WEST, frame.getContentPane()); 
    frame.getContentPane().add(textPane); 
    frame.setLocationRelativeTo(null); 
    frame.setTitle("Calcolo"); 


    JMenuBar menuBar = new JMenuBar(); 
    frame.setJMenuBar(menuBar); 

    JMenu mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    final JMenuItem mntmSave = new JMenuItem("Save"); 
    mntmSave.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      SaveToFile sv = new SaveToFile(); 
     } 
    }); 
    mnFile.add(mntmSave); 

    JMenu mnOptions = new JMenu("Options"); 
    menuBar.add(mnOptions); 

    JMenu mnHelp = new JMenu("Help"); 
    menuBar.add(mnHelp); 

    final JMenuItem AboutMenu = new JMenuItem("About"); 
    AboutMenu.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      if (e.getSource().equals(AboutMenu)) { 
       JDialog dialog = new JDialog(); 
        dialog.setTitle("Search Dialog"); 
        dialog.getContentPane().add(new JLabel("Just a test")); 
        dialog.setSize(300,300); 
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
        dialog.setLocationRelativeTo(frame); 
        dialog.setVisible(true); 

      if (e.getSource().equals(mntmSave)); 
       SaveToFile sv = new SaveToFile(); 
      } 
     } 
    }); 
    mnHelp.add(AboutMenu); 

} 
} 

답변

2

나는 당신에게 방법을 보여주고 싶습니다. 아래처럼 String을 받아 들일 수있는 SaveToFile 클래스에 생성자가 있습니다. textpane의 텍스트를이 생성자 또는 메서드로 String으로 전달합니다. 또는 방법조차 좋다. 그러나이 두 가지 중 하나를 선택하십시오.

public class SaveToFile { 

    public SaveToFile(String textinput) { 
     System.out.println(textinput); 
    } 

    // if you prefer to have a differrent method do like below. 

    public void doSomething(String textinput) { 
      System.out.println(textinput); 
    } 
} 

지금 내가 뭐하는 거지 것은 나는를 보내고있다

final JMenuItem mntmSave = new JMenuItem("Save"); 
     mntmSave.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
        SaveToFile sv = new SaveToFile(); 
        sv.doSomething(textPane.getText()); 
      } 
}); 

, 당신은 다음 생성자를 원하지 않는 경우

final JMenuItem mntmSave = new JMenuItem("Save"); 
    mntmSave.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      SaveToFile sv = new SaveToFile(textPane.getText()); 
     } 
}); 

, 아래와 같이 좋아하는 리스너 변경 Textpanel의 텍스트를 SaveToFile 클래스에 복사합니다. 거기에서이 문자열을 사용할 수 있습니다. 당신은 당신이 SaveToFile 클래스와 다른 클래스에 whcih textpane에서 텍스트를 가지고 지금

final JTextPane textPane = new JTextPane(); 

아래처럼 마지막으로 JTextPane가 선언되었는지 확인합니다. 내가 말했듯이 "그 길은 길이 아님".

+0

코드에 감사드립니다. 제 경우에는 완벽합니다. :) – chriskvik

+0

당신을 환영합니다 ... – Vinay

4

WindowsPro 빌더 플러그인, 그리고 정의의 모든 JFrame의 구성 요소() 클래스를 초기화 할 수 있습니다.

initialize()은 클래스가 아닌 방법 일뿐입니다. 여기 수업은 FunctionsGUI입니다.

이 일반적으로 나는 내 구성 요소가 그래서 그들이 공개적으로 내 프로그램 물마루에 액세스 할 수 있습니다 시작 정의, 나를 위해 질문을 만듭니다.

당신이하고있는 일에 따라 다르지만 이것은 좋지 않은 디자인 일 수 있습니다.

아니요. 두 번째 클래스가 있지만 구성 요소에 액세스 할 수 없습니다.

getter를 구현하여 필요한 구성 요소 (또는 모든 구성 요소)를 반환합니다.

당신은 내가 에게 텍스트 영역의 입력을 얻기 위해 원하는 클래스, 클래스 SaveToFile 전화 자체 바와 같이,하지만 어떻게 이런 짓을 했을까?

예를 들어 생성자의 JTextField ~ SaveToFile 클래스에 대한 참조를 전달할 수 있습니다.

+0

감사합니다. 오프라인이라고하는 방법입니다. 내 나쁜 .. 지금은 Understnad가되었지만, 어떻게 행동 청취자에 대해 알아볼 수 있습니까? 지금처럼 모든 버튼 하나를 구현해야합니까? 또는 전체 메서드에 대한 ActionListener를 초기화 할 수 있습니까? 예를 들어 방금 함수가없는 FunctionGUI가 있다면 일반적으로 여기에 ActionListener가 구현되어 있습니다. 어떻게 초기화 할 수 있습니까? :) – chriskvik

관련 문제