Java 排列/组合计算器上的GUI问题
我能够编写这个程序,我没有看到任何错误,它甚至显示gui。我很确定我正确地分配了按钮。但是GUI是喜怒无常的,当我运行它时,它会显示出来,但有时当我输入值时,GUI的内部会消失。但它计算的是nCr,而不是pCr 我有驾驶课。非常确定它的实现是正确的。这是我的小组课。我想知道出了什么问题,为什么GUI不能正常工作 我意识到这是很多代码。我不希望有人给我重写。我只是想知道我做错了什么,以及我如何去纠正它 谢谢Java 排列/组合计算器上的GUI问题,java,swing,combinations,permutation,joptionpane,Java,Swing,Combinations,Permutation,Joptionpane,我能够编写这个程序,我没有看到任何错误,它甚至显示gui。我很确定我正确地分配了按钮。但是GUI是喜怒无常的,当我运行它时,它会显示出来,但有时当我输入值时,GUI的内部会消失。但它计算的是nCr,而不是pCr 我有驾驶课。非常确定它的实现是正确的。这是我的小组课。我想知道出了什么问题,为什么GUI不能正常工作 我意识到这是很多代码。我不希望有人给我重写。我只是想知道我做错了什么,以及我如何去纠正它 谢谢 import java.awt.*; import java.awt.event.*; i
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JOptionPane;
public class PermCombCalc extends JPanel {
JButton permButton = new JButton();
JButton combButton = new JButton();
JButton clearButton = new JButton();
JTextField npermField = new JTextField();
JTextField rperField = new JTextField();
JTextField nchooseField = new JTextField();
JTextField rchooseField = new JTextField();
JTextField pAnswerField = new JTextField();
JTextField cAnswerField = new JTextField();
public PermCombCalc() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 700));
JLabel permLabel = new JLabel("Permutation:");
permLabel.setBounds(10, 20, 100, 20);
permLabel.setForeground(Color.BLACK);
add(permLabel);
JLabel combLabel = new JLabel("Combination:");
combLabel.setBounds(215, 20, 75, 20);
combLabel.setForeground(Color.BLACK);
add(combLabel);
// Creating Permutation Button
JLabel PnrLabel = new JLabel("P (n,r)");
PnrLabel.setForeground(Color.black);
permButton.setBounds(10, 115, 100, 25);
add(permButton);
permButton.add(PnrLabel);
// Action Listener for permbutton
permButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
permButton.setActionCommand("Perm");
permButton.addActionListener(new ButtonListener());
}
});
// Creating combination button
JLabel CnrLabel = new JLabel("C(n, r)");
CnrLabel.setForeground(Color.black);
combButton.setBounds(190, 115, 100, 25);
add(combButton);
combButton.add(CnrLabel);
// ActionListener
combButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
combButton.setActionCommand("comb");
combButton.addActionListener(new ButtonListener());
}
});
// Text fields for n and r
npermField.setBounds(23, 50, 60, 20);
add(npermField);
nchooseField.setBounds(230, 50, 60, 20);
add(nchooseField);
rperField.setBounds(23, 80, 60, 20);
add(rperField);
rchooseField.setBounds(230, 80, 60, 20);
add(rchooseField);
// Input fields
JLabel npLabel = new JLabel("n:");
npLabel.setForeground(Color.black);
npLabel.setBounds(10, 55, 10, 10);
add(npLabel);
JLabel ncLabel = new JLabel("n:");
ncLabel.setForeground(Color.BLACK);
ncLabel.setBounds(217, 55, 10, 10);
add(ncLabel);
JLabel rpLabel = new JLabel("r:");
rpLabel.setForeground(Color.BLACK);
rpLabel.setBounds(10, 85, 10, 10);
add(rpLabel);
JLabel rcLabel = new JLabel("r:");
rcLabel.setForeground(Color.BLACK);
rcLabel.setBounds(217, 85, 10, 10);
add(rcLabel);
// Fields for answers
JLabel pAnswerJLabel = new JLabel("<-Answers->");
pAnswerJLabel.setForeground(Color.BLACK);
pAnswerJLabel.setBounds(115, 155, 74, 10);
add(pAnswerJLabel);
pAnswerField.setBounds(10, 150, 100, 20);
add(pAnswerField);
cAnswerField.setBounds(190, 150, 100, 20); // where is this field?!
add(cAnswerField);
// Buttons
//clearButton.setBounds(10, 210, 110, 25);
//add(clearButton);
//JLabel clearLabel = new JLabel("Clear Fields");
//clearLabel.setForeground(Color.BLACK);
//clearButton.add(clearLabel);
// clearButton.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// clearButton.setActionCommand("Clear");
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("perm")) {
// contentPane.setBackground(Color.red);
long Pnr = Permutation();
if (Pnr != 0) {
pAnswerField.setText(Pnr + "");
}
} else if (e.getActionCommand().equals("comb")) {
// contentPane.setBackground(Color.black);
long Cnr = Combination();
if (Cnr != 0) {
cAnswerField.setText(Cnr + "");
}
} else if (e.getActionCommand().equals("Clear")) {
// contentPane.setBackground(Color.lightGray);
npermField.setText(null);
rperField.setText(null);
pAnswerField.setText(null);
nchooseField.setText(null);
rchooseField.setText(null);
cAnswerField.setText(null);
}
}
public long Permutation() {
String npString = npermField.getText();
String rpString = rperField.getText();
int npint = 0;
int rpint = 0;
try {
npint = Integer.parseInt(npString);
rpint = Integer.parseInt(rpString);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"ERROR! The values for 'n' and 'r' \n must be positive integers");
return 0;
}
if (npint <= 0 || rpint <= 0) {
JOptionPane.showMessageDialog(null,"ERROR! The values for 'n' and 'r' \n must be positive integers");
return 0;
}
if (npint < rpint) {
JOptionPane.showMessageDialog(null,"ERROR! The value of 'r' must be less than \n or equal to the value of 'n.'");
return 0;
}
long Pnr = 1;
int mult = npint;
int nmr = (npint - rpint);
while (mult > nmr) {
Pnr = Pnr * mult;
mult--;
}
return Pnr;
}
public long Combination() {
String ncString = nchooseField.getText();
String rcString = rchooseField.getText();
int ncint = 0;
int rcint = 0;
try {
ncint = Integer.parseInt(ncString);
rcint = Integer.parseInt(rcString);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"ERROR! The values for 'n' and 'r' \n must be positive integers");
return 0;
}
if (ncint <= 0 || rcint <= 0) {
JOptionPane.showMessageDialog(null,"Error! The values for 'n' and 'r' \n must be positive integers");
return 0;
}
if (ncint < rcint) {
JOptionPane.showMessageDialog(null,"ERROR! The value of 'r' must be less than \n or equal to the value of 'n.'");
return 0;
}
long nfact = 1;
for (int i = 2; i <= ncint; i++) {
nfact = nfact * i;
}
long rfact = 1;
for (int i = 2; i <= rcint; i++) {
rfact = rfact * i;
}
long nmr = ncint - rcint;
int nmrfact = 1;
for (int i = 2; i <= nmr; i++) {
nmrfact = nmrfact * i;
}
long Cnr = (nfact / (rfact * nmrfact));
return Cnr;
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.Color;
导入javax.swing.JOptionPane;
公共类PermCombCalc扩展了JPanel{
JButton permButton=新JButton();
JButton combButton=新JButton();
JButton clearButton=新JButton();
JTextField npermField=新的JTextField();
JTextField rperField=新的JTextField();
JTextField nchooseField=新的JTextField();
JTextField rchooseField=新的JTextField();
JTextField pAnswerField=新的JTextField();
JTextField cAnswerField=新的JTextField();
公共PermCombCalc(){
setLayout(新的BorderLayout());
setPreferredSize(新尺寸(1000700));
JLabel permLabel=新JLabel(“置换:”);
permLabel.立根(10,20,100,20);
permLabel.setForeground(颜色:黑色);
添加(永久标签);
JLabel combLabel=新的JLabel(“组合:”);
combLabel.setBounds(215,20,75,20);
combLabel.setForeground(颜色:黑色);
添加(combLabel);
//创建置换按钮
JLabel PnrLabel=新JLabel(“P(n,r)”);
PnrLabel.setForeground(颜色:黑色);
permButton.setBounds(10、115、100、25);
添加(永久按钮);
permButton.add(PnrLabel);
//permbutton的操作侦听器
addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
permButton.setActionCommand(“Perm”);
addActionListener(newbuttonListener());
}
});
//创建组合按钮
JLabel CnrLabel=新的JLabel(“C(n,r)”;
CnrLabel.设置前景(颜色:黑色);
combButton.立根(190、115、100、25);
添加(组合按钮);
combButton.add(CnrLabel);
//ActionListener
combButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
combButton.setActionCommand(“comb”);
addActionListener(newbuttonListener());
}
});
//n和r的文本字段
npermField.setBounds(23,50,60,20);
添加(npermField);
nchooseField.立根(230,50,60,20);
添加(nchooseField);
rperField.挫折(23,80,60,20);
添加(rperField);
rchooseField.立根(230,80,60,20);
添加(rchooseField);
//输入字段
JLabel npLabel=新的JLabel(“n:”);
n标签设置前景(颜色为黑色);
nLabel.setBounds(10,55,10,10);
添加(npLabel);
JLabel ncLabel=新的JLabel(“n:”);
ncLabel.setForeground(颜色:黑色);
n标记立根(217,55,10,10);
添加(ncLabel);
JLabel rpLabel=新的JLabel(“r:”);
rpLabel.setForeground(颜色:黑色);
rpLabel.setBounds(10,85,10,10);
添加(rpLabel);
JLabel rcLabel=新的JLabel(“r:”);
rLabel.setForeground(颜色:黑色);
rLabel.立根(217,85,10,10);
添加(rcLabel);
//答案字段
JLabel pAnswerJLabel=新JLabel(“”);
pAnswerJLabel.setForeground(颜色:黑色);
pAnswerJLabel.setBounds(115,155,74,10);
添加(pAnswerJLabel);
立根(10150100,20);
添加(pAnswerField);
坎斯维尔菲尔德。挫折(19015010020);//这个领域在哪里?!
添加(坎斯维尔菲尔德);
//钮扣
//clearButton.setBounds(10210、110、25);
//添加(clearButton);
//JLabel clearLabel=新的JLabel(“清除字段”);
//clearLabel.setForeground(颜色:黑色);
//clearButton.add(clearLabel);
//clearButton.addActionListener(新ActionListener(){
//已执行的公共无效操作(操作事件e){
//clearButton.setActionCommand(“清除”);
}
私有类ButtonListener实现ActionListener{
已执行的公共无效操作(操作事件e){
如果(例如getActionCommand().equals(“perm”)){
//contentPane.setBackground(颜色:红色);
长Pnr=置换();
如果(Pnr!=0){
设置文本(Pnr+“”);
}
}else if(例如getActionCommand().equals(“comb”)){
//contentPane.setBackground(颜色:黑色);
长Cnr=组合();
如果(Cnr!=0){
cAnswerField.setText(Cnr+“”);
}
}else if(例如getActionCommand().equals(“Clear”)){
//contentPane.setBackground(颜色:浅灰色);
npermField.setText(空);
rperField.setText(空);
pAnswerField.setText(空);
nchooseField.setText(空);
rchooseField.setText(空);
cAnswerField.setText(空);
}
}
公共长排列(){
字符串npString=npermField.getText();
String rpString=rperField.getText();
int-npint=0;
int-rpint=0;
试一试{
npint=Integer.parseInt(npString);
rpint=Integer.parseInt(rpString);
}捕获(数字格式){
JOptionPane.showMessageDialog(null,“错误!n”和“r”的值\n必须是正整数
.
.
.
add(permButton);
permButton.add(PnrLabel);
// Action Listener for permbutton
permButton.setActionCommand("Perm");
permButton.addActionListener(new ButtonListener());
.
.
.
if (typeA) {
doActionA();
} else if (typeB) {
doActionB();
} else if (typeC) {
doActionC();
}
npermField.setBounds(23, 50, 60, 20);
add(npermField);
nchooseField.setBounds(230, 50, 60, 20);
// add a border to make the component easier to see during layout.
npermField.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
add(nchooseField);