按下按钮时显示另一个按钮并打印偶数 [英] Display another button and print even numbers when button pressed
本文介绍了按下按钮时显示另一个按钮并打印偶数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我下面的代码,我无法在其中添加第二个按钮.我以某种方式设法创建了一个按钮,以及如何在按下按钮时显示一组偶数
This is my code below I am not able to add a second button in it. I have somehow managed to create one button and how can display set of even numbers when a button is pressed
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example2 extends JFrame {
public Example2() {
initUI();
}
public final void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
panel.setToolTipText("A Panel container");
JButton button = new JButton("Even");
button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");
JButton button2 = new JButton("Odd");
button2.setBounds(100, 60, 100, 30);
button2.setToolTipText("A button component");
panel.add(button);
panel.add(button2);
setTitle("Tooltip");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Example2 ex = new Example2();
ex.setVisible(true);
}
}
推荐答案
放弃 null
布局并使用真正的 LayoutManager
,您的两个按钮都会出现在布局中.请参阅 Swing 布局管理器教程以获得一些示例代码
Ditch the null
layout and use a real LayoutManager
and both your buttons will appear in the layout.
Consult the Swing layout manager tutorial for some sample code
这篇关于按下按钮时显示另一个按钮并打印偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文