我想知道arraylist列表是否能够在JOptionPane窗口中运行。我试图从Windows中使用命令控制台分支出来,所以我想了解如何使用JOptionPane。
例如psudocode:
import javax.swing.*;
import java.util.*;
import java.io.*;
public class try1
{
private static JPanel panel = new JPanel();
private static try2 testing = new try2 ();
public static Integer testnum;
public static void main (String[] args)
{
testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
tryMe ();
}
public static void tryMe ()
{
int userInput = 0;
Object[] options1 = { " ENTER " , " GET AVERAGE " };
panel.add(new JLabel(" PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE "));
JTextField textField = new JTextField(10);
panel.add(textField);
if (userInput == JOptionPane.YES_OPTION)
{
for ( int count = 1; count <= testnum; count++)
{
userInput = JOptionPane.showOptionDialog(null, panel, " TEST AVERAGE PROGRAM " ,JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null, options1, null);
try2 testing = new try2 (userInput); // sending this to my class.
}
}
if (userInput == JOptionPane.NO_OPTION)
{
testing.setAvg ();
JOptionPane.showMessageDialog(null,"You average is" + (testing.getAvg()));
}
}
}
class try2
{
public static ArrayList<Integer>userInput=new ArrayList<Integer>();
public static double avg;
public try2()
{
}
public try2(int i)
{
userInput.add(i);
}
public static void setAvg ()
{
try
{
int sum = 0;
for ( int x = 0 ; x < userInput.size(); x++)
{
sum += userInput.size() ;
}
avg = sum / userInput.size();
if ( avg < 0 || avg > 100)
{
IllegalArgumentException ex;
}
}
catch ( IllegalArgumentException ex)
{
}
}
public static double getAvg ()
{
return avg;
}
}
我从这个例子开始,看看它是如何工作的,谁能告诉我我做错了什么。所以这就是我被困在Jpanel上来,所以它正在通过我的声明。但是,jpanel并不清楚。如何让Jpanel清除,以便输入另一个输入?
答案 0 :(得分:0)
“我试图在Windows中使用命令控制台进行分支,所以我试图了解如何使用JOptionPane。”
所以看起来JOptionPane
是您第一次遇到任何 GUI。我建议的是尝试最简单的任务。一些注意事项
();
声明结尾处遗漏的ArrauList
之外,您的循环已关闭。
example.size()
进行迭代,最初为0.循环将起作用,但它将是循环内显式条件if/break
内的无限循环。count >
而是count <
并使用硬编码。按照上述要点后,尝试像
这样简单的事情完成10次显示JOptionPane
并将输入数字添加到列表中
for (int count = 0; count < 10; count++) {
Integer num = ...
example.add(num);
}
然后在完成循环后,只需添加ArrayList
中的所有数字并将其打印出来。
只需做一些这样简单的事情就可以了解它。除了JOptionPane
inputDialog
对话框的How to Make Dialogs
更新
import javax.swing.*;
import java.util.*;
import java.io.*;
public class try1 {
private static JPanel panel = new JPanel();
private static try2 testing = new try2();
public static Integer testnum;
public static void main(String[] args) {
testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
tryMe();
}
public static void tryMe() {
int userInput = 0;
if (userInput == JOptionPane.YES_OPTION) {
for (int count = 1; count <= testnum; count++) {
String userInputString = JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
int value = Integer.parseInt(userInputString);
testing.addInput(value); // sending this to my class.
}
JOptionPane.showMessageDialog(null, String.valueOf(testing.getAvg()));
}
if (userInput == JOptionPane.NO_OPTION) {
System.exit(0);
}
}
}
class try2 {
public static ArrayList<Integer> userInput = new ArrayList<Integer>();
public static double avg;
public try2() {
}
public void addInput(int value) {
userInput.add(value);
}
public try2(int i) {
userInput.add(i);
}
public double getAvg() {
double sum = 0;
for (Integer value : userInput) {
sum += value;
}
return sum / userInput.size();
}
}
答案 1 :(得分:0)
这是我使用数组列表和Jopt创建的测试程序。我将不得不在我的主要部分调整我的声明计数器,但这将是一块蛋糕。谢谢@peeskillet的帮助
// Import Libraries
import javax.swing.*;
import java.util.*;
public class Try1
{
public static ArrayList<Integer>user=new ArrayList<Integer>();
private static JPanel panel = new JPanel();
public static Integer testnum;
public static void main (String[] args)
{
testnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Please Enter The Amount Of Test To Be Calculated Below "));
classes ();
}
public static void classes()
{
int userInput = 0;
if (userInput == JOptionPane.YES_OPTION)
{
user = new ArrayList<Integer>();
for (int count = 1; count <= testnum; count++)
{
String userInputString = JOptionPane.showInputDialog(null, " PLEASE ENTER ALL THE FOLLOWING TEST GRADES TO CALCULATE ");
int value = Integer.parseInt(userInputString);
user.add(value);
new Try2(user);
}
}
if (userInput == JOptionPane.NO_OPTION)
{
Try2.setAvg();
JOptionPane.showMessageDialog(null, "You average is" + (Try2.getAvg()));
}
classesExtended();
}
public static void classesExtended()
{
JFrame frame = new JFrame();
String[] options = new String[3];
options[0] = new String ( " GET AVERAGE ");
options[1] = new String ( " OOPS I FORGOT!! ADD MORE TESTS ");
options[2] = new String ( " Exit ");
int result = JOptionPane.showOptionDialog(frame.getContentPane(), " OK WHAT WOULD YOU LIKE TO DO NOW ","Title", 0,JOptionPane.INFORMATION_MESSAGE,null,options,null);
if ( result == 0 )
{
Try2.setAvg ();
JOptionPane.showMessageDialog(null,"You average is" + (Try2.getAvg()));
}
if ( result == 1 )
{
classes ();
}
if ( result == 2)
{
System.exit(0);
}
}
}
class Try2
{
public static ArrayList<Integer>userInput=new ArrayList<Integer>();
public static double avg;
public Try2()
{
}
public Try2(ArrayList<Integer> test)
{
try
{
for (int x = 0; x <= test.size()-1; x++)
{
if (test.get(x) < 0 || test.get(x) > 100)
{
throw new IllegalArgumentException();
}
else
{
this.userInput = test;
}
}
}
catch (IllegalArgumentException ex)
{
JOptionPane.showMessageDialog(null, " NO NEGETIVES ALLOWED ");
}
}
public static void setAvg ()
{
int sum = 0;
for ( int x = 0 ; x < userInput.size(); x++)
{
sum += userInput.get(x) ;
}
avg = sum / userInput.size();
}
public static double getAvg ()
{
return avg;
}
}