java图形界面设计
最上面是一个文本框主要显示结果,下面一个标签“输入一个数”下面又一个文本框存入该数最下面一个按钮名为求阶乘结果存入地一个文本框中我在线等急需...
最上面是一个文本框主要显示结果,下面一个标签“输入一个数” 下面又一个文本框存入该数 最下面一个按钮 名为 求阶乘 结果存入地一个文本框中
我在线等 急需 展开
我在线等 急需 展开
2个回答
展开全部
/**因为是用long型来存储结果,所以如果你想求更大的阶乘的话换类型或者用其它的求高精度数值类**/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestFact extends JFrame {
private JTextField jtfNum;
private JTextField jtfResult;
public TestFact() {
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel jlaInput = new JLabel("输入一个数:");
jlaInput.setBounds(12, 94, 70, 28);
panel.add(jlaInput);
jtfNum = new JTextField();
jtfNum.setBounds(121, 94, 114, 30);
panel.add(jtfNum);
jtfNum.setColumns(10);
JButton jbtCompute = new JButton("求阶乘");
jbtCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fact();
}
});
jbtCompute.setBounds(67, 151, 117, 24);
panel.add(jbtCompute);
jtfResult = new JTextField();
jtfResult.setEnabled(false);
jtfResult.setBounds(12, 34, 326, 28);
panel.add(jtfResult);
jtfResult.setColumns(10);
this.setTitle("求阶乘");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(362,300);
this.setVisible(true);
}
private void fact() {
String s = jtfNum.getText().trim();
int num;
long fact = 1;
if(s==null||s.equals("")){
JOptionPane.showMessageDialog(null, "您还没有输入呢!");
return ;
}
try{
num = Integer.parseInt(s);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "您的输入数字有误哦!");
return ;
}
if(num<0){
JOptionPane.showMessageDialog(null, "您的输入数字为负数!");
return ;
}
for(int i = 1; i<= num; i++){
fact *=i;
}
jtfResult.setText(fact+"");
}
public static void main(String[] args) {
new TestFact();
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TestFact extends JFrame {
private JTextField jtfNum;
private JTextField jtfResult;
public TestFact() {
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel jlaInput = new JLabel("输入一个数:");
jlaInput.setBounds(12, 94, 70, 28);
panel.add(jlaInput);
jtfNum = new JTextField();
jtfNum.setBounds(121, 94, 114, 30);
panel.add(jtfNum);
jtfNum.setColumns(10);
JButton jbtCompute = new JButton("求阶乘");
jbtCompute.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fact();
}
});
jbtCompute.setBounds(67, 151, 117, 24);
panel.add(jbtCompute);
jtfResult = new JTextField();
jtfResult.setEnabled(false);
jtfResult.setBounds(12, 34, 326, 28);
panel.add(jtfResult);
jtfResult.setColumns(10);
this.setTitle("求阶乘");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(362,300);
this.setVisible(true);
}
private void fact() {
String s = jtfNum.getText().trim();
int num;
long fact = 1;
if(s==null||s.equals("")){
JOptionPane.showMessageDialog(null, "您还没有输入呢!");
return ;
}
try{
num = Integer.parseInt(s);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "您的输入数字有误哦!");
return ;
}
if(num<0){
JOptionPane.showMessageDialog(null, "您的输入数字为负数!");
return ;
}
for(int i = 1; i<= num; i++){
fact *=i;
}
jtfResult.setText(fact+"");
}
public static void main(String[] args) {
new TestFact();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |