java中 如何实现 根据JTextfield输入的个数 , 在JPanel中显示相应个数个按钮
是用循环语句么?for(inti=0;i<=number;i++){JButtonb=newJButton("");}求高手解答,谢谢...
是用循环语句么?
for(int i=0;i<=number;i++)
{
JButton b=new JButton("");
}
求高手解答,谢谢 展开
for(int i=0;i<=number;i++)
{
JButton b=new JButton("");
}
求高手解答,谢谢 展开
1个回答
展开全部
代码
--------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class BtnApp extends JFrame implements ActionListener {
int k = 0;
boolean isTree = false;
private JTextField textField;
private JPanel panel;
public BtnApp() {
getContentPane().setLayout(null);
panel = new JPanel();
panel.setBounds(10, 44, 554, 324);
getContentPane().add(panel);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(113, 10, 327, 21);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblInputNumber = new JLabel("Input number");
lblInputNumber.setBounds(10, 13, 93, 15);
getContentPane().add(lblInputNumber);
JButton btnClick = new JButton("Click");
btnClick.addActionListener(this);
btnClick.setBounds(450, 9, 93, 23);
getContentPane().add(btnClick);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(580, 410);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new BtnApp();
}
public void actionPerformed(ActionEvent e) {
int num = 0;
panel.removeAll();
try {
num = Integer.parseInt(textField.getText());
} catch (Exception ex) {
}
for (int i = 1; i <= num; i++) {
JButton button = new JButton("btn" + i);
panel.add(button);
}
panel.updateUI();
}
}
--------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class BtnApp extends JFrame implements ActionListener {
int k = 0;
boolean isTree = false;
private JTextField textField;
private JPanel panel;
public BtnApp() {
getContentPane().setLayout(null);
panel = new JPanel();
panel.setBounds(10, 44, 554, 324);
getContentPane().add(panel);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setBounds(113, 10, 327, 21);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblInputNumber = new JLabel("Input number");
lblInputNumber.setBounds(10, 13, 93, 15);
getContentPane().add(lblInputNumber);
JButton btnClick = new JButton("Click");
btnClick.addActionListener(this);
btnClick.setBounds(450, 9, 93, 23);
getContentPane().add(btnClick);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(580, 410);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new BtnApp();
}
public void actionPerformed(ActionEvent e) {
int num = 0;
panel.removeAll();
try {
num = Integer.parseInt(textField.getText());
} catch (Exception ex) {
}
for (int i = 1; i <= num; i++) {
JButton button = new JButton("btn" + i);
panel.add(button);
}
panel.updateUI();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询