我如何实施一个;如果/那么;Java中带按钮的语句?
所以,如果你还不能从我的头衔看出,我对java真的很陌生。我正在尝试编写一个小程序,当你点击一个按钮(例如按钮x)时,一个新的文本窗口将出现,其中包含一些信息。我只是想知道如何在代码中实现它。谢谢你能给我的任何帮助我如何实施一个;如果/那么;Java中带按钮的语句?,java,button,Java,Button,所以,如果你还不能从我的头衔看出,我对java真的很陌生。我正在尝试编写一个小程序,当你点击一个按钮(例如按钮x)时,一个新的文本窗口将出现,其中包含一些信息。我只是想知道如何在代码中实现它。谢谢你能给我的任何帮助 import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; pu
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);
}
} ///:~
这里有一种方法:
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);
}
}好的,您需要处理按钮上的单击事件,然后在该处理程序中,创建一个新的
JDialog
或JFrame
。我建议您查看教程GUI,它并不简单。java2s在很多方面都有很好的例子。给JButton一个Action或ActionListener。请考虑Google和阅读教程,因为这只是他们擅长回答的这些类型的问题。我如何在上面的代码中实现这一点?