大家好!使用GroupLayout按z顺序绘制零部件时遇到问题。我的按钮工作不稳定(例如,如果鼠标被移动,就会消失(。这是什么?绘图错误?这是设置z顺序的代码。
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);
这是帧的所有代码
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
}
如果我不使用GroupLayout
,一切都可以,但我需要组件的相对位置。
我在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);
}
}