Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JApplet添加到JFrame_Java_Swing_Jframe_Japplet - Fatal编程技术网

Java 将JApplet添加到JFrame

Java 将JApplet添加到JFrame,java,swing,jframe,japplet,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 m

我试图在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);
}
}
import javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JButton;
公务舱{
公共静态void main(字符串[]args){
对于(int i=0;i<1;i++){
FirstWindow fw=新的FirstWindow();
fw.setVisible(真);
油漆p=新油漆();
p、 setVisible(真);
}
}
}
导入javax.swing.JApplet;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.BorderLayout;
导入java.awt.Insets;
导入java.awt.GridBagLayout;
导入java.awt.event.*;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共级油漆扩展JApplet
实现MouseMotionListener{
int宽度、高度;
图像缓冲;
图形背景;
JPanel面板=新的JPanel();
公共void init(){
宽度=getSize().width;
高度=getSize()。高度;
backbuffer=createImage(宽度、高度);
backg=backbuffer.getGraphics();
背景设置颜色(颜色为白色);
backg.fillRect(0,0,宽度,高度);
backg.setColor(Color.blue);
addMouseMotionListener(此);
}
public void mouseMoved(MouseEvent e){}
公共无效鼠标标记(鼠标事件e){
int x=e.getX();
int y=e.getY();
背面呈椭圆形(x-5,y-5,10,10);
重新油漆();
e、 消费();
}
公共空间更新(图g){
g、 drawImage(backbuffer,0,0,this);
}
公共空间涂料(图g){
更新(g);
}
}
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.BorderLayout;
导入java.awt.Insets;
导入java.awt.GridBagLayout;
导入java.awt.event.*;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JButton;
导入javax.swing.JCheckBox;
导入javax.swing.JLabel;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.JOptionPane;
导入javax.swing.JRadioButton;
导入javax.swing.ButtonGroup;
公共类FirstWindow扩展了JFrame{
私有静态最终长serialVersionUID=1L;
字符串s;
JCheckBox cb,cb2;
JTextField-textField;
JLabel标签;
公共窗口(){
超级(“油漆”);
设置大小(1600800);
setDefaultCloseOperation(关闭时退出);
JPanel面板=新的JPanel();
JRadioButton b=新的JRadioButton(“蓝色”);
JRadioButton r=新的JRadioButton(“红色”);
JRadioButton y=新的JRadioButton(“黄色”);
JRadioButton g=新的JRadioButton(“绿色”);
JRadioButton p=新的JRadioButton(“紫色”);
JRadioButton o=新的JRadioButton(“橙色”);
ButtonGroup颜色=新建ButtonGroup();
增加(b);
颜色。添加(r);
颜色。添加(y);
颜色。添加(g);
颜色。添加(p);
颜色。添加(o);
添加(面板,边界布局。北);
小组委员会.添加(b);
面板。添加(r);
面板。添加(y);
小组.添加(g);
小组委员会.添加(p);
小组委员会.添加(o);
}
}
我是否需要将框架放入
StartHere
并将
JPanel
和applet从其他两个类导入其中


我还没有完成应用程序,在小程序连接到框架后,我还有很多要更新的内容,我只需要程序这一部分的帮助。

1)“我在想如何将我的JApplet添加到JFrame时遇到了问题。”如果你为小程序编写代码,我会建议完全不同的方法。将代码重构成一个
JPanel
,可以添加到applet、框架、窗口或..任何其他需要显示的容器中。2) “我看了其他问题寻求帮助,但我不认为其他人确切地回答了我的问题”哪些问题?为什么他们没有帮助?简短的回答是“没有”,一般来说,小程序有一个“生命周期”,通常由浏览器管理,长的答案是安德鲁说的