所以,如果你不能通过我的标题说出来,那我对java来说真的很新。我试图编写一个applet,当你点击一个按钮(例如按钮x)时,会出现一个新的文本窗口,其中包含一些信息。我只是想知道如何将其实现到我的代码中。谢谢你能给我的任何帮助。
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Button1 extends JApplet {
private JButton b1 = new JButton("Button 1"), b2 = new JButton("Button 2"), b3 = new JButton("Button 3");
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(b3);
}
public static void main(String[] args) {
run(new Button1(), 200, 50);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
答案 0 :(得分:1)
这是一种方法:
对于此示例,b1显示了面板。 b2和b3隐藏面板。我更改了按钮标签
import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;
公共类Button1扩展JApplet实现了ActionListener {
private JButton b1 = new JButton("show"), b2 = new JButton("hide"), b3 = new JButton("hide");
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
cp.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
panel.add(new JLabel("foo"));
frame.add(panel);
frame.setSize(200,100);
frame.setLocation(200,200);
}
public static void main(String[] args) {
run(new Button1(), 200, 50);
}
public static void run(JApplet applet, int width, int height) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
frame.setVisible(true);
else
frame.setVisible(false);
}
}