2014-09-12 3 views
4

나는 자바 UI 프로그래밍에 익숙하지 않다. 내가 프로그램을 실행할 때 나는 GUI를로드하려면 어떻게 메인 클래스에서 Java GUI 클래스를로드하는 방법은 무엇입니까?

public static void main(String[] args) { 
    // TODO code application logic here 
    FileExtractorGUI gui = new FileExtractorGUI(); 
    gui.setVisible(true); 
} 

기본적으로

FileExtractorGUI is a gui class. 

자바

의 기본 클래스에서 UI 클래스를 호출 어떻게

. 현재 코드를 실행할 때 아무 일도 일어나지 않습니다.

주석의 요청에 따라

package fileextractor; 

import javax.swing.JOptionPane; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author 
*/ 
public class FileExtractorGUI extends javax.swing.JPanel { 

    /** 
    * Creates new form FileExtractorGUI 
    */ 
    public FileExtractorGUI() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     SourceField = new javax.swing.JTextField(); 
     jLabel1 = new javax.swing.JLabel(); 
     DestinationField = new javax.swing.JTextField(); 
     jLabel2 = new javax.swing.JLabel(); 
     Extract = new javax.swing.JButton(); 

     SourceField.setText("SourceField"); 
     SourceField.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       SourceFieldActionPerformed(evt); 
      } 
     }); 

     jLabel1.setText("SourceFolder"); 

     DestinationField.setText("DestinationField"); 

     jLabel2.setText("DestinationFolder"); 

     Extract.setText("Extract"); 
     Extract.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       ExtractActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel1) 
         .addGap(38, 38, 38) 
         .addComponent(SourceField, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel2) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE) 
         .addComponent(DestinationField, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addGap(74, 74, 74)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(Extract) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(26, 26, 26) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(SourceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel1)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(DestinationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel2)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 194, Short.MAX_VALUE) 
       .addComponent(Extract) 
       .addContainerGap()) 
     ); 
    }// </editor-fold>       

    private void SourceFieldActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
    }           

    private void ExtractActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     String src = SourceField.getText(); 
     String dest = DestinationField.getText(); 
     CopyDirectoryExample copy = new CopyDirectoryExample(); 
     copy.extract(src, dest); 
     JOptionPane.showMessageDialog(null, "Extracted!"); 
    }          


    // Variables declaration - do not modify      
    private javax.swing.JTextField DestinationField; 
    private javax.swing.JButton Extract; 
    private javax.swing.JTextField SourceField; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    // End of variables declaration     
} 

GUI 클래스는, 내가 그것을 JPanel를 확장하기 때문에 당신은 FileExtractorGUI를 표시 할 수있는 GUI 클래스 코드

+0

표시 내용을 JFrame의를 생성하고 추가 할 수 있습니다. –

+0

최상위 컨테이너를 추가하고이 최상위 컨테이너에 UI를 하위로 배치해야합니다 (http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html –

+1

). 패널을 배치해야합니다 'JFrame' 안에 넣은 다음, 그 것을 볼 수있게 설정할 수 있습니다. 또한 show [here] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)와 같이 이벤트 발송 스레드에서만 스윙 구성 요소를 만들고 액세스해야합니다. – kiheru

답변

8
public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      FileExtractorGUI gui = new FileExtractorGUI(); 
      JFrame frame = new JFrame(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().add(gui); 
      frame.pack(); 
      frame.setVisible(true); 
     } 
    }); 
} 
2

을 업로드했습니다. 모든 구성 요소 JFrame 또는 Window의 하위 항목이 될입니다.

변경 첫

public class FileExtractorGUI extends JFrame 
2

Istance JFrame의에 라인

public class FileExtractorGUI extends javax.swing.JPanel 

하고 패널을 입력합니다.

//1. Create the frame. 
JFrame frame = new JFrame("YouFrame"); 
//2. Optional: What happens when the frame closes? 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//3. Create your panel and put it in the frame. 
frame.getContentPane().add(new FileExtractorGUI(), BorderLayout.CENTER); 
//4. Size the frame. 
frame.pack(); 
//5. Show it. 
frame.setVisible(true); 
1

doc에서 봅니다 패널에게`FileExtractorGUI` 클래스의

 FileExtractorGUI gui = new FileExtractorGUI(); 

     JFrame jf=new JFrame(); 
     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jf.setSize(500, 500); 
     jf.add(gui); 
     jf.setVisible(true); 
관련 문제