将JApplet添加到JFrame

时间:2017-02-15 17:02:19

标签: java swing jframe japplet

我正在尝试在Bluej中制作类似Microsoft Paint的绘图应用程序,并且我在确定如何将JApplet添加到JFrame时遇到问题。我已经看了其他问题寻求帮助,我不认为其他人正在回答我的问题。这是我的3个类的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
public class StartHere {
    public static void main (String [] args){
    for (int i = 0; i < 1; i++){
    FirstWindow fw = new FirstWindow();
    fw.setVisible(true);
    paint p = new paint();
    p.setVisible(true);
   }
}
}




import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class paint extends JApplet
    implements MouseMotionListener{
   int width, height;
   Image backbuffer;
   Graphics backg;

   JPanel panel = new JPanel();

   public void init() {
      width = getSize().width;
      height = getSize().height;

      backbuffer = createImage( width, height );
      backg = backbuffer.getGraphics();
      backg.setColor( Color.white );
      backg.fillRect( 0, 0, width, height );
      backg.setColor( Color.blue );

      addMouseMotionListener( this ); 
   }

   public void mouseMoved( MouseEvent e ) { }
   public void mouseDragged( MouseEvent e ) {
      int x = e.getX();
      int y = e.getY();
      backg.fillOval(x-5,y-5,10,10);
      repaint();
      e.consume();
    }

   public void update( Graphics g ) {
      g.drawImage( backbuffer, 0, 0, this );
    }

   public void paint( Graphics g ) {
      update( g );
   }
}




import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class FirstWindow extends JFrame{
private static final long serialVersionUID = 1L;

String s;
JCheckBox cb, cb2;
JTextField textField;
JLabel label;

public FirstWindow() {
    super ("PAINT");

    setSize(1600,800);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();




    JRadioButton b = new JRadioButton ("BLUE");
    JRadioButton r = new JRadioButton ("RED");
    JRadioButton y = new JRadioButton ("YELLOW");
    JRadioButton g = new JRadioButton ("GREEN");
    JRadioButton p = new JRadioButton ("PURPLE");
    JRadioButton o = new JRadioButton ("ORANGE");



    ButtonGroup colors = new ButtonGroup();
    colors.add(b);
    colors.add(r);
    colors.add(y);
    colors.add(g);
    colors.add(p);
    colors.add(o);

    add (panel, BorderLayout.NORTH);

    panel.add(b);
    panel.add(r);
    panel.add(y);
    panel.add(g);
    panel.add(p);
    panel.add(o);
}
}

我是否需要将框架放入StartHere并将其他2个类中的JPanel和applet导入其中?

注意

我没有接近完成应用程序,在applet连接到框架后我还有很多需要更新的地方,我只需要帮助解决该程序的这一部分。

0 个答案:

没有答案