当我想将JOptionPane
放在前面时,我遇到了一个问题,因为它会在弹出消息对话框旁边显示一个窗口。
如何只弹出前面的选项窗格而不弹出任何其他内容?
这是我的代码。
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Questions1 {
static String questiontext[][]=new String[20][6];
private static int num_questions=20;
static Scanner l = new Scanner(System.in);
private int state;
public static void settriviaquestions()//constructor and using two-dimensional array
{
questiontext[0][0]="Which of the following is a renewable energy source?";
questiontext[1][0]="How full should your washing machine or dishwasher be before you run them?\r\n";
questiontext[2][0]="If you want to be really environmentally friendly, where should you buy your produce?";
questiontext[3][0]="What are the cons of Go Green?(can choose more than one)";
questiontext[4][0]="What are the ways to Go Green?(can choose more than one)";
questiontext[5][0]="What happens if we don’t Go Green?(can choose more than one)";
questiontext[6][0]="What happens to the atmosphere when fossil fuels are used?";
questiontext[7][0]="What happens if we keep using non renewable resources?";
questiontext[8][0]="What is the key difference between non renewable and renewable resources?";
questiontext[9][0]="What is not a benefit of using solar energy?";
questiontext[10][0]="Which uses less water, washing a full load of dishes by hand or in the dishwasher?";
questiontext[11][0]="Appliances that are turned off don’t use any electricity.";
questiontext[12][0]="Approximately how much global electricity output is produced from renewable sources?";
questiontext[13][0]="About how much money do you save by replacing a single incandescent light bulb with a compact fluorescent light (CFL)?";
questiontext[14][0]="Dropping the thermostat from 70 to 68 degrees(21.1 to 20 degrees celsius) saves you about how much on your heating costs?";
questiontext[15][0]="In a conventional washing machine, what percentage of energy is used to heat the water?";
questiontext[16][0]="How many times can paper be recycled before the fibers are too small to recycle again?";
questiontext[17][0]="Under the Kyoto protocol, the first international climate change treaty, how much, on average, did industrialised nations pledge to reduce their annual greenhouse gas emissions by?";
questiontext[18][0]="Last year Canada switched on the world’s first commercial carbon capture and storage plant. How much did it cost?";
questiontext[19][0]="What percentage of the world's total water supply is fresh, accessible water?";
questiontext[0][1]="1. Gasoline\n" + "2. Wind\n" + "3. Oil\n" + "4. Coal\n";
questiontext[1][1]="1. Completely\n" + "2. Mostly\n" + "3. Halfway\n" + "4. Whatever you have at the time\n";
questiontext[2][1]="1. Supermarket\n" + "2. Hannaford\n" + "3. Online\n" + "4. A farm stand in your town\n";
questiontext[3][1]="1. None of them below\n" + "2. Reduced Utility Costs\n" + "3. Require a lot of time and efforts\n" + "4. Preservation of wildlife\n";
questiontext[4][1]="1. Carpool\r\n" + "2. Use reusable bag anywhere, anytime\n" + "3. Recycle wastes\n" + "4. All of the above\n";
questiontext[5][1]="1. Global Warming\n" + "2. Disease spread\n" + "3. Animal extinction\n" + "4. All of the above\n";
questiontext[6][1]="1. The fossil fuels go into the earth and cause earthquakes.\n" + "2. The co2 released from burning these go into the atmosphere and make it thicker making the sunrays trapped with the atmosphere.\n" + "3. The fossil fuels go into our water supply which poisons us.\n" + "4. Animals eats the fossil fuels which makes them go extinct.\n";
questiontext[7][1]="1. When we use it the resources goes away.\n" + "2. Nothing happens.\n" + "3. When we run out it will replenish itself.\n" + "4. We will eventually run out of resources.\n";
questiontext[8][1]="1. There is no difference.\n" + "2. Non renewable resources eventually are harder to use, but renewable resources are easy to use.\n" + "3. Non renewable runs out and will never replenish itself or take a long time to replenish, but renewable resources constantly replenish itself.\n" + "4. Non renewable resources work faster, and renewable works slower.\n";
questiontext[9][1]="1. it costs a lot.\n" + "2. It is environment friendly.\n" + "3. The sun if the major source of energy on earth.\n" + "4. Solar energy doesn’t create CO2 that will thicken the atmosphere.\n";
questiontext[10][1]="1. by hand.\n" + "2. by dishwasher.\n";
questiontext[11][1]="1. True\n" + "2. False\n";
questiontext[12][1]="1. 10 percent\n" + "2. 5 percent\n" + "3. 20 percent\n" + "4. 1 percent\n";
questiontext[13][1]="1. $1\n" + "2. $10\n" + "3. $30\n" + "4. $5\n";
questiontext[14][1]="1. 20 percent\n" + "2. 5 percent\n" + "3. 1 percent\n" + "4. 10 percent\n";
questiontext[15][1]="1. 20%\n" + "2. 40%\n" + "3. 75%\n" + "4. 90%\n";
questiontext[16][1]="1. 5 times\n" + "2. 7 times\n" + "3. 3 times\n" + "4. 10 times\n";
questiontext[17][1]="1. 4.5% by 2015 compared to 1990\n" + "2. 5.2% by 2012 compared to 1990\n" + "3. 6.8% by 2012 compared to 1990\n" + "4. 8.6% by 2015 compared to 1990\n";
questiontext[18][1]="1. $13m\n" + "2. $130m\n" + "3. $1.3bn\n" + "4. $13bn\n";
questiontext[19][1]="1. less than 1%\n" + "2. 5% - 10%\n" + "3. 25%\n" + "4. 50%\n";
questiontext[0][2]="2";
questiontext[1][2]="1";
questiontext[2][2]="4";
questiontext[3][2]="3";
questiontext[4][2]="4";
questiontext[5][2]="4";
questiontext[6][2]="2";
questiontext[7][2]="4";
questiontext[8][2]="3";
questiontext[9][2]="1";
questiontext[10][2]="2";
questiontext[11][2]="2";
questiontext[12][2]="1";
questiontext[13][2]="3";
questiontext[14][2]="2";
questiontext[15][2]="4";
questiontext[16][2]="2";
questiontext[17][2]="2";
questiontext[18][2]="3";
questiontext[19][2]="1";
questiontext[0][3]="Gasoline, oil and coal are all non-renewable resources, and we are running out of them. Renewable energy sources include wind, solar, and geothermal energies.";
questiontext[1][3]="Whether you are washing dishes or clothes, a full load is always best. This saves water because you're not running the extra loads, and it also saves the time that extra loads take";
questiontext[2][3]="Locally grown produce is the best in terms of its effect on the environment. Food grown elsewhere must be shipped to the store, therefore causing more carbon dioxide emissions in the transportation. Traveling a few minutes has much less of an impact than a cross-country drive.";
questiontext[3][3]="The con of Go Green is require a lot of time and efforts. Adopting a green lifestyle can often be time-consuming for those who are fully committed to green living.";
questiontext[4][3]="All of the above. Carpool,using reusable and recycling wastes is very good for the environment. It will reduce air pollution and cutting trees.";
questiontext[5][3]="All of the above. If we continuously cutting down trees,burning wastes, more cars on the road and so on will result in global warming. This causes the temperature of the Earth to increase and disease become more and active. Besides that, animal will also extinction because of disease and temperature change.";
questiontext[6][3]="Fossil fuels are considered non-renewable resources and consist of coal, oil, and natural gas. They release carbon dioxide, a potent greenhouse gas, into the atmosphere when burned. Greenhouse gases trap heat from the sun in the Earth's atmosphere, causing temperatures to rise.";
questiontext[7][3]="We will eventually run out of resources because these it is not renewable.";
questiontext[8][3]="There are nine major areas of energy resources. They fall into two categories: nonrenewable and renewable. Nonrenewable energy resources, like coal, nuclear, oil, and natural gas, are available in limited supplies. This is usually due to the long time it takes for them to be replenished. Renewable resources are replenished naturally and over relatively short periods of time. The five major renewable energy resources are solar, wind, water (hydro), biomass, and geothermal.";
questiontext[9][3]="It costs a lot.The initial cost of purchasing a solar system is fairly high. Although the UK government has introduced some schemes for encouraging the adoption of renewable energy sources, for example, the Feed-in Tariff, you still have to cover the upfront costs. This includes paying for solar panels, inverter, batteries, wiring and for the installation.";
questiontext[10][3]="Hand washing dishes can use up to 50 percent more water than a water-saving, energy-efficient dishwasher. However, dishwasher made before 1994 use more water than current models. Look for units with Energy Star rating.";
questiontext[11][3]="Many appliances continue to use energy for features like clocks and remote control sensors even when they’re turned off.According to the Department of Energy, the electricity consumed by televisions that are turned off but still plugged in costs U.S households more than $750 million a year.";
questiontext[12][3]="Only about 10 percent of global energy comes from renewables. The remaining 90 percent comes from non-renewable sources like oil, coal, and natural gas.";
questiontext[13][3]="According to the U.S. Environment Protection Agency, CFLs use 2/3 less energy than standard incandescent bulbs and last up to 10 times longer. Replacing a 60-watt incandescent with a 13-watt CFL will save about $30 in energy costs over the life of the bulb.";
questiontext[14][3]="Each degree you drop the thermostat during winter saves about 5 percent on your heating bill. For air conditioning, set the thermostat to 78 degree Fahrenheit(25.6 degree Celsius) or higher.";
questiontext[15][3]="According to the Department of Energy, 90% of the energy in a conventional top-load washing machine is used to heat the water. That’s why many environmentally-conscious homeowners simply wash their clothes in cold water.";
questiontext[16][3]="Paper can be recycled up to 7 times before the fibers become too small to recycle.";
questiontext[17][3]="The Kyoto protocol was the first agreement between nations to mandate country-by-country reductions in greenhouse-gas emissions. Under Kyoto, industrialised nations pledged to cut their yearly emissions of carbon, as measured in six greenhouse gases, by varying amounts, averaging 5.2%, by 2012 as compared to 1990. That equates to a 29% cut in the values that would have otherwise occurred. However, the protocol didn't become international law until more than halfway through the 1990–2012 period.";
questiontext[18][3]="Canada has switched on the first large-scale coal-fired power plant fitted with a technology that proponents say enables the burning of fossil fuels without tipping the world into a climate catastrophe. Saskatchewan’s state-owned electricity provider is due to cut the ribbon on the $1.3 billion Canadian project on Thursday. But officials from SaskPower International Inc told guests invited to the ceremony the 110 megawatt plant went live on Tuesday night.The Boundary Dam power plant promises to cut carbon dioxide emissions by 90% by trapping C02 underground before the gas reaches the atmosphere – making its opening a milestone in the coal industry’s efforts to remain viable in a low-carbon economy.";
questiontext[19][3]="Just 0.3% of our planet's supply is fresh, accessible water. The rest is salty, frozen, or trapped deep underground.";
for(int i=0;i<num_questions;i++) {
questiontext[i][4]="0";
}
questiontext[1][5]="What are the alternative material for making paper?\n\nPanda excrement, hemp and sheepskin are all alternative material for making paper.\nPerhaps hemp, the more humane and clean option, require less land acreage to grow than timber, \nhas fewer chemical byproducts, and can be recycled more frequently than tree timber.";
questiontext[2][5]="If every household replaced one roll of regular paper with one recycled post-consumer waste rolls, how many trees would we save?\n\nIf every household replaced one roll of regular toilet paper with one recycled post-consumer waste roll, 424,000 trees would be saved.";
questiontext[3][5]="What are some consequences of making water power?\n\nThe dams required to create water power uses a lot of energy and materials, \nit can cause earthquakes, floods, and bring malaria carrying mosquitoes. \nOne way to mitigate the social effects of dams is to add doctors’ clinics around the \nreservoir area to increase treatment of mosquito related diseases such as malaria.";
questiontext[4][5]="What are the pros of Go Green?\n\nLess waste and healthier. \nRecycling will help to reduce waste and by recycling we can reduce the smoke circulating the air.";
}
public void setquestiontext(String questions){
this.questiontext=questiontext;
}
public String[][] getquestiontext(){
return questiontext;
}
public static void main (String[] args) {
int ans=0;
Random randomizer = new Random();
int r=0;
settriviaquestions();
int i=1;
int j=1;
Random A = new Random();
int a = A.nextInt(5 - 0) + 1;
Random B = new Random();
int b = B.nextInt(10 - 5) + 6;
Random C = new Random();
int c = C.nextInt(15 - 10) + 11;
Random D = new Random();
int d = D.nextInt(20 - 15) + 16;
while(i<=num_questions)
{
r = randomizer.nextInt(20)+0;
if(questiontext[r][4]=="0") {
System.out.println("Question " + i + " :" + questiontext[r][0]);
System.out.println(questiontext[r][1]);
System.out.println("Enter your answer:");
ans = l.nextInt();
if (ans == Integer.parseInt(questiontext[r][2]))
{
System.out.println("Correct!");
}
else {
System.out.println("Wrong!");
}
System.out.println("Explanation: " + questiontext[r][3] + "\n");
questiontext[r][4]="1";
if(i==a || i==b || i==c || i==d)
{
if(j<5)
{
javax.swing.JFrame jf = new javax.swing.JFrame() ;
jf.setLocation( 600, 300 );
jf.setVisible(true);
jf.setAlwaysOnTop(true);
jf.setAlwaysOnTop(false);
JOptionPane.showMessageDialog(null, "Extra Knowledge " + j + " :\n" + questiontext[j][5]);
}
j++;
}
i++;
}
}
}
}