我是java swing的新手,我现在使用NetBeant来使用NetBeans内置的拖放设计器来构建GUI。我现在面临的问题是,我有下面发布的代码,它是用Eclipse编写的,如你所见 我有一个扩展JPanel的类,我将从WEB_CAM捕获的图像添加到JPanel.now,因为我切换到NetBeans,我使用设计器创建了JFrame,JPanel,JButtons,并且在我在NetBeans中编写的代码中,我不 知道如何将捕获的图像添加到JPanel。
在eclipse中我在另一个类中创建了下面发布的代码,但是在NetBeans中,每个东西都是自动生成的,我必须做同样的工作,我在eclipse中做了。我不知道如何在NetBeans中向JPanel添加图像。JPanel inEclipse
class FacePanel extends JPanel {
private BufferedImage image;
int count = 0;
public FacePanel() {
super();
}
public void setFace (BufferedImage img) {
this.image = img;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.image == null) {
System.out.println("image is null");
return;
}
g.drawImage(this.image, 10, 10, this.image.getWidth(), this.image.getHeight(), null);
g.setFont(new Font("arial", 2, 20));
g.setColor(Color.WHITE);
g.drawString("processing frame by frame [ frame: " + ( count++) + " ]", 150, 150);
}
}
NetBeans中的代码
private void initComponents() {
jpanel1_Preview = new javax.swing.JPanel();
btn_Play = new javax.swing.JButton();
btn_Pause = new javax.swing.JButton();
btn_Capture = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jpanel1_Preview.setBorder(javax.swing.BorderFactory.createTitledBorder("Cam_Preview"));
javax.swing.GroupLayout jpanel1_PreviewLayout = new javax.swing.GroupLayout(jpanel1_Preview);
jpanel1_Preview.setLayout(jpanel1_PreviewLayout);
jpanel1_PreviewLayout.setHorizontalGroup(
jpanel1_PreviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jpanel1_PreviewLayout.setVerticalGroup(
jpanel1_PreviewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 225, Short.MAX_VALUE)
);
btn_Play.setText("play");
btn_Play.setSelected(true);
btn_Pause.setText("pause");
btn_Pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_PauseActionPerformed(evt);
}
});
btn_Capture.setText("capture");
btn_Capture.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_CaptureActionPerformed(evt);
}
});
答案 0 :(得分:1)
class FacePanel extends JPanel {
更改为public class FacePanel extends JPanel {
,否则Netbeans将无法创建该实例。FacePanel
类从Project explorer拖到表单上......