I'm trying to output these components but they won't appear. I can't figure out what I did wrong.
import java.awt.*;
import javax.swing.*;
public class Buttons extends JApplet {
Container con;
JPanel form;
JButton oneB, twoB, threeB;
public void init() {
con = new Container();
form = new JPanel();
form.setLayout(new GridLayout(2, 2));
oneB = new JButton("1B");
form.add(oneB);
twoB = new JButton("2B");
form.add(twoB);
threeB = new JButton("3B");
form.add(threeB);
con.add(form);
}
}
You never added con
to anything
In fact, it's not really needed, just add form
to the applet...
public void init() {
form = new JPanel();
form.setLayout(new GridLayout(2, 2));
oneB = new JButton("1B");
form.add(oneB);
twoB = new JButton("2B");
form.add(twoB);
threeB = new JButton("3B");
form.add(threeB);
add(form);
}
If you're just learning, I would highly encourage you NOT to use applets, they have their own issues which can make learning a real pain. Instead, try starting with window based components, like JFrame
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.