2016-09-25 3 views
0

모두들! GroupLayout을 사용하여 구성 요소를 z 순서로 그리는 데 문제가 있습니다. 내 버튼이 안정적으로 작동하지 않습니다 (마우스가 움직 인 경우 예를 들어 사라집니다). 이게 뭐야? 그림에 오류가 있습니까? z 순서를 설정하는 코드입니다.GroupLayout z-order

 javax.swing.GroupLayout jPanel2Layout = (javax.swing.GroupLayout) jLayeredPane1.getLayout();//new javax.swing.GroupLayout(getContentPane()); 
    //getContentPane().setLayout(jPanel2Layout); 
    //jPanel2.setLayout(jPanel2Layout); 
    jPanel2Layout.setHorizontalGroup(
     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
     .addGroup(jPanel2Layout.createSequentialGroup() 
      .addGap(100, 100, 100) 
      .addComponent(jPanel2) 
      .addContainerGap(211, Short.MAX_VALUE)) 
     .addGroup(jPanel2Layout.createSequentialGroup() 
      .addGap(110, 110, 110) 
      .addComponent(btn1) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 
    jPanel2Layout.setVerticalGroup(
     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
     .addGroup(jPanel2Layout.createSequentialGroup() 
      .addGap(100, 100, 100) 
      .addComponent(jPanel2) 
      .addContainerGap(33, Short.MAX_VALUE)) 
     .addGroup(jPanel2Layout.createSequentialGroup() 
      .addGap(110, 110, 110) 
      .addComponent(btn1) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 
    jLayeredPane1.setLayer(jPanel2, 1); 
    jLayeredPane1.setLayer(btn1, 2); 

내가 GroupLayout 모든 작품 사용하지 않는 경우이 코드는 프레임

package examples; 
import javafx.application.Platform; 
import javafx.embed.swing.JFXPanel; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
//import javax.swing.SpringLayout; 

public class BrowserFrame extends javax.swing.JFrame { 

    public BrowserFrame() { 
     initComponents(); 
    } 

    private void initAndShowGUI() { 
     // This method is invoked on the EDT thread 
     //JFrame frame = new JFrame("Swing and JavaFX"); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JFXPanel fxPanel = new JFXPanel(); 
     //FlowLayout fLayout = new FlowLayout(); 

     final JPanel jPanel2 = new JPanel(); 


     /*getContentPane().setLayout(fLayout);*/ 
     jPanel2.add(fxPanel); 

     final JButton btn1 = new JButton(); 
     btn1.setText("test"); 
     //btn1.setBounds(110, 140, 150, 160); 


     javax.swing.GroupLayout jPanel2Layout = (javax.swing.GroupLayout) jLayeredPane1.getLayout();//new javax.swing.GroupLayout(getContentPane()); 
     //getContentPane().setLayout(jPanel2Layout); 
     //jPanel2.setLayout(jPanel2Layout); 
     jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(100, 100, 100) 
       .addComponent(jPanel2) 
       .addContainerGap(211, Short.MAX_VALUE)) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(110, 110, 110) 
       .addComponent(btn1) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(100, 100, 100) 
       .addComponent(jPanel2) 
       .addContainerGap(33, Short.MAX_VALUE)) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(110, 110, 110) 
       .addComponent(btn1) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     jLayeredPane1.setLayer(jPanel2, 1); 
     jLayeredPane1.setLayer(btn1, 2); 


     //this.add(jPanel2); 

     //jPanel2.setBounds(100, 120, 400, 550); 
     //jLayeredPane1.add(jPanel2, new Integer(1)); 


     //jLayeredPane1.add(btn1, new Integer(2)); 
     //jLayeredPane1.setLayer(btn1, 2); 
     /*jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(100, 100, 100) 
       .addComponent(btn1) 
       .addContainerGap(211, Short.MAX_VALUE)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(100, 100, 100) 
       .addComponent(btn1) 
       .addContainerGap(33, Short.MAX_VALUE)) 
     ); */ 


     //jLayeredPane1.add(btn1, new Integer(2)); 
     //jLayeredPane1.add(btn1, new Integer(1)); 
     //jPanel2.add(btn1); 
     //jLayeredPane1.add(btn1); 

     /*jPanel2Layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(jPanel2Layout);*/ 

     /*jPanel2Layout.setHorizontalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(120, 120, 120) 
       .addComponent(btn1) 
       .addContainerGap(211, Short.MAX_VALUE)) 
     ); 
     jPanel2Layout.setVerticalGroup(
      jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel2Layout.createSequentialGroup() 
       .addGap(120, 120, 120) 
       .addComponent(btn1) 
       .addContainerGap(33, Short.MAX_VALUE)) 
     );*/ 

     /*jLayeredPane1.setComponentZOrder(jPanel2, 2); 
     jLayeredPane1.setComponentZOrder(btn1, 1);*/ 
     //jLayeredPane1.setComponentZOrder(jButton1, 2); 
     //this.add(btn1); 
     /*getContentPane().setComponentZOrder(btn1, 1); 
     getContentPane().setComponentZOrder(jPanel2, 0);*/ 
     this.pack(); 
     this.setSize(640, 480); 


     this.setLocationRelativeTo(null); 
     this.setVisible(true); 

     Platform.runLater(() -> { 
      initFX(fxPanel); 
     }); 
    } 

    private WebView webView; 

    private void initFX(JFXPanel fxPanel) { 
     // This method is invoked on the JavaFX thread 
     Scene scene = createScene(); 
     fxPanel.setScene(scene); 
    } 

    private Scene createScene() { 
     StackPane root = new StackPane(); 
     Scene scene = new Scene(root); 
     webView = new WebView(); 
     WebEngine webEngine = webView.getEngine(); 
     webEngine.load("http://www.ya.ru"); 
     root.getChildren().add(webView); 
     root.toBack(); 
     return scene; 
    }  

    /** 
    * 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() { 

     jLayeredPane1 = new javax.swing.JLayeredPane(); 
     jButton1 = new javax.swing.JButton(); 
     jPanel1 = new javax.swing.JPanel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("jButton1"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 100, Short.MAX_VALUE) 
     ); 

     jLayeredPane1.setLayer(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER); 
     jLayeredPane1.setLayer(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER); 

     javax.swing.GroupLayout jLayeredPane1Layout = new javax.swing.GroupLayout(jLayeredPane1); 
     jLayeredPane1.setLayout(jLayeredPane1Layout); 
     jLayeredPane1Layout.setHorizontalGroup(
      jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jLayeredPane1Layout.createSequentialGroup() 
       .addGap(36, 36, 36) 
       .addComponent(jButton1) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jLayeredPane1Layout.createSequentialGroup() 
       .addGap(306, 306, 306) 
       .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addGap(277, 277, 277)) 
     ); 
     jLayeredPane1Layout.setVerticalGroup(
      jLayeredPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jLayeredPane1Layout.createSequentialGroup() 
       .addGap(25, 25, 25) 
       .addComponent(jButton1) 
       .addGap(72, 72, 72) 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(280, Short.MAX_VALUE)) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLayeredPane1) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLayeredPane1) 
       .addContainerGap()) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(BrowserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(BrowserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(BrowserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(BrowserFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       //new BrowserFrame().setVisible(true); 
       new BrowserFrame().initAndShowGUI(); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JLayeredPane jLayeredPane1; 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration     
} 

의 모든 코드,하지만 난 구성 요소의 상대적 위치가 필요합니다.

+0

TL; DR 최소로 강조하여 [mcve] ...를 만들어보세요. 작은 테스트 케이스에서 문제를 재현 할 수 있다면 문제가 명백해질 수 있습니다. – AJNeufeld

+0

기다려! 왜 당신은 * swing *과 * javafx *를 동시에 사용합니까 ??? 하나를 선택. – AJNeufeld

+0

NetBeans GUI Builder를 사용하여 스윙 응용 프로그램을 만들고 싶었지만 응용 프로그램 내부에 브라우저가 필요했습니다. 스윙 브라우저 예제는 찾지 못했지만 javafx 만 기반으로했습니다. 그래서 '스윙 앱'내부에 javafx 패널을 설정하려고했습니다. – user6254358

답변

0

나는 javatalks.ru에서 대답을 얻었다. 브라우저 패널 위에 투명한 패널을 추가하기 만하면됩니다.

package name.wind.common.fx; 

import javafx.application.Platform; 
import javafx.concurrent.Worker; 
import javafx.embed.swing.JFXPanel; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.web.WebEngine; 
import javafx.scene.web.WebView; 

import javax.swing.*; 
import java.awt.*; 

public class Sample { 

    public static void main(String... args) { 
     JPanel buttonPanel = new JPanel(new FlowLayout()); 
     buttonPanel.setBackground(new Color(0, 0, 0, 0)); 
     JButton button1st = new JButton("button1st"); 
     button1st.setEnabled(false); 
     buttonPanel.add(button1st); 
     JButton button2nd = new JButton("button2nd"); 
     button2nd.setEnabled(false); 
     buttonPanel.add(button2nd); 

     JFXPanel browserPanel = new JFXPanel(); 
     Platform.runLater(() -> { 
      WebView webView = new WebView(); 
      WebEngine webEngine = webView.getEngine(); 
      webEngine.getLoadWorker().stateProperty().addListener(
       (ov, oldState, newState) -> { 
        if (newState == Worker.State.SUCCEEDED) { 
         SwingUtilities.invokeLater(() -> { 
          button1st.setEnabled(true); 
          button2nd.setEnabled(true); 
         }); 
        } 
       }); 
      webEngine.load("http://www.ya.ru"); 
      StackPane root = new StackPane(); 
      root.getChildren().add(webView); 
      browserPanel.setScene(new Scene(root)); 
     }); 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLocationByPlatform(true); 
     frame.getContentPane().setLayout(new OverlayLayout(frame.getContentPane())); 
     frame.getContentPane().add(buttonPanel); 
     frame.getContentPane().add(browserPanel); 
     frame.setSize(600, 400); 
     frame.setVisible(true); 
    } 

} 
관련 문제