我是 Java Applet 编程和一般 Java 的新手,但我非常擅长 C# 和 C++。无论如何,我正在用 JTextField、JButton 和 JLabel 制作一个简单的计算器。但是 JButton 和 JTextField 占据了整个屏幕,最奇怪的是我设置了大小,它没有设置大小。那么我做错了什么?这是下面的代码,所以有人可以帮助我;我将不胜感激任何答案。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JTextField;
/**
*
* @author Danny
*/
public class Applet extends JApplet {
private JButton button1;
private JTextField textBox1;
@Override
public void paint(Graphics g)
{
g.setFont(new Font("Courier", Font.PLAIN, 12));
g.drawString("Enter a number: ", 36, 36);
return;
}
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void init() {
button1 = new JButton("Calculate");
textBox1 = new JTextField("number");
textBox1.setFont(new Font("Courier", Font.PLAIN, 12));
textBox1.setSize(100, 36);
button1.setSize(100, 36);
textBox1.setLocation(new Point(36, 76));
button1.setLocation(new Point(36, 70));
Container c = getContentPane();
c.add(button1);
c.add(textBox1);
c.setBackground(Color.gray);
}
// TODO overwrite start(), stop() and destroy() methods
}