如何将其添加到Java小程序中?

如何将其添加到Java小程序中?,java,swing,applet,Java,Swing,Applet,这是一个简单的类,我想添加到applet中,但是我该如何做呢 以下是正在使用的变量: int force = 1; int distance = 50/force; int x = distance*60; int WIDTH = 300 - x; 这是绘制方法: public void paint(Graphics g){ g.setColor(Color.GRAY); g.drawString("Use the buttons to control the input fo

这是一个简单的类,我想添加到applet中,但是我该如何做呢

以下是正在使用的变量:

int force = 1;
int distance = 50/force;
int x = distance*60;
int WIDTH = 300 - x;
这是绘制方法:

public void paint(Graphics g){
    g.setColor(Color.GRAY);
    g.drawString("Use the buttons to control the input force", 100, 25);
    g.fillRect(300, 250, 300, 25);
    g.fillRect(x, 250, WIDTH, 25);
如果我需要更改这些按钮的位置,请告诉我。(当前正在使用“绘制”方法)

JButton subtract=新的JButton(“-”);
JButton add=新JButton(“+”);
add.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
force++;
}
});
subtract.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
武力--;
}
});
添加.设置大小(50,25);
减去设置大小(50,25);

如果(@ MadProgrammer)会告诉你,Applet是一个不受欢迎的技术。所以你应该考虑使用其他东西。1)为什么要编写一个小程序?如果是老师指定的,请参考。2) 见和。
    JButton subtract = new JButton("-");
    JButton add = new JButton("+");
    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
                force++;
            }

        });

    subtract.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            force--;
            }
        });
    add.setSize(50, 25);
    subtract.setSize(50, 25);
    if(force<=0){
        force =1 ;
    }
    add.setLocation(0,0);
    subtract.setLocation(51, 0);
    LApplet app = new LApplet();
    app.add(add);
    app.add(subtract);
    app.setBackground(Color.BLACK);
}}