はじめに、私はこのサイトに不慣れで、プログラミング (主にグラフィックス) にかなり慣れていません。また、時間を割いてこの作業を手伝ってくれた親切な人にも感謝したいと思います。
私の問題は、特定の JButtons で特定のボタンを非表示にし、他のボタンを表示して (別の画面のように見せるために) メニューを作成しようとすると、ウィンドウのサイズを変更するか最小化しない限り、すべてが非表示になることです。ただし、これを一度行った後、メニューからメニューへの切り替えは完全に機能します。また、表示される JComboBox がいくつかありますが、クリックされるまでは非表示または部分的に非表示であり、なぜそうしているのかわかりません。
このアプレットは、化学で他の子供たちを助けるためのツールとして完成したときにオンラインで公開される予定です. すべてを正しく配置する前にすべてを機能させたかったのですが、私の変数名のいくつかは他の人にとって少し混乱するかもしれません。私がそこで何をしているのかを説明するために最善を尽くします。
再度、感謝します
アプレットのグラフィックを作成するためのコードは次のとおりです。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends Applet implements ActionListener
{
private int screen = -1;//-1 is value for startup screen
private JComboBox box1, box2, box3, box4, box5, box11, box22, box33, box44, box55;//drop downs for unit converter
private static String[] cVolumeO = {"Ml","L"}; //Unit options for converter
private static String[] cAreaO = {"sqFt","sqMile"};
private static String[] cLengthO = {"ft","Mile"};
private static String[] cTempO = {"C","F","K"};
private static String[] cChemO = {"Moles","Grams"}; //end of unit options for converter
private static String[] filename;//store pic filenames
private Icon[] pics;//stores applet pics
private JLabel picture;
private Button MainMenu;
private Button periodicTable, element, solvers, balancer, fChart, converter; //main menu buttons
private Button Continue; //start up screen
private TextField dialog;
private TextField cVolume, cArea, cLength, cTemp, cChem; //converter entry fields
private TextField volumeAnswer, areaAnswer, lengthAnswer, tempAnswer, chemAnswer;//converter answer fields
public void init(){
setBackground(new Color(10,10,205));
setSize(500, 500);
setLayout(new FlowLayout());
instanceVarAssigner();//sets all variables to their correct values (it's basically a constructor)
createStartScreen_();//sets up default screen by making all invisible except continue button + default words and picture
assignLocation();//positions all of the items to their proper locations
add(Continue); //adds all buttons to the applet screen
add(periodicTable);
add(element);
add(solvers);
add(balancer);
add(fChart);
add(converter);
add(MainMenu);
add(box1);
add(box11);
add(box2);
add(box22);
add(box3);
add(box33);
add(box4);
add(box44);
add(box5);
add(box55);
add(cVolume);
add(cArea);
add(cLength);
add(cTemp);
add(cChem);
add(volumeAnswer);
add(areaAnswer);
add(lengthAnswer);
add(tempAnswer);
add(chemAnswer);
// adds functionality to all the buttons
periodicTable.addActionListener(this);
element.addActionListener(this);
solvers.addActionListener(this);
balancer.addActionListener(this);
fChart.addActionListener(this);
converter.addActionListener(this);
MainMenu.addActionListener(this);
Continue.addActionListener(this);
box1.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
box4.addActionListener(this);
box5.addActionListener(this);
repaint();
}//end init();
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawString(dialog.getText(),20,100);
if(screen==0){//Main Menu
mainSetup();
}
else if(screen==1){//PeriodicTable
ptSetup();
}
else if(screen==2){//Element Finder/Info
elementSetup();
}
else if(screen==3){//Solvers
solverSetup();
}
else if(screen==4){//Balancer
balancerSetup();
}
else if(screen==5){//fChart
fChartSetup();
}
else if(screen==6){//Converter
converterSetup();
}
}//end paint();
private void solverSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void elementSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void balancerSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void fChartSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void converterSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
box1.setVisible(true);
box2.setVisible(true);
box3.setVisible(true);
box4.setVisible(true);
box5.setVisible(true);
box11.setVisible(true);
box22.setVisible(true);
box33.setVisible(true);
box44.setVisible(true);
box55.setVisible(true);
cVolume.setVisible(true);
cArea.setVisible(true);
cLength.setVisible(true);
cTemp.setVisible(true);
cChem.setVisible(true);
volumeAnswer.setVisible(true);
areaAnswer.setVisible(true);
lengthAnswer.setVisible(true);
tempAnswer.setVisible(true);
chemAnswer.setVisible(true);
}
private void ptSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void mainSetup() {
periodicTable.setVisible(true);
element.setVisible(true);
solvers.setVisible(true);
balancer.setVisible(true);
fChart.setVisible(true);
converter.setVisible(true);
MainMenu.setVisible(false);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == element){
dialog.setText("element was pushed.");
screen=2;
repaint();
}
else if(evt.getSource() == periodicTable){
dialog.setText("pt was pushed.");
screen=1;
repaint();
}
else if(evt.getSource() == solvers){
dialog.setText("s was pushed.");
screen=3;
repaint();
}
else if(evt.getSource() == balancer){
dialog.setText("b was pushed.");
screen=4;
repaint();
}
else if(evt.getSource() == fChart){
dialog.setText("fC was pushed.");
screen=5;
repaint();
}
else if(evt.getSource() == converter){
dialog.setText("c was pushed.");
screen=6;
repaint();
}
else if(evt.getSource() == MainMenu){
createStartScreen_();//refreshes shutting off of all things so that the code doesnt need to be repeated.
dialog.setText("Main Menu");
Continue.setVisible(false);
screen=0;
repaint();
}
else if(evt.getSource() == Continue){
dialog.setText("Main Menu");
screen=0;
Continue.setVisible(false);
repaint();
}
}//end actionPreformed();
private void instanceVarAssigner(){
MainMenu=new Button("Main Menu");
periodicTable = new Button("Peoridic Table");
element = new Button("Element Finder/Info");
solvers = new Button("Solvers");
balancer = new Button("Equation Balancer");
fChart = new Button("Formula Chart");
converter = new Button("Unit Converter");
Continue = new Button("Continue");
box1 = new JComboBox(cVolumeO);//converter items
box11 = new JComboBox(cVolumeO);
box2 = new JComboBox(cAreaO);
box22 = new JComboBox(cAreaO);
box3 = new JComboBox(cLengthO);
box33 = new JComboBox(cLengthO);
box4 = new JComboBox(cTempO);
box44 = new JComboBox(cTempO);
box5 = new JComboBox(cChemO);
box55 = new JComboBox(cChemO);
volumeAnswer = new TextField("0");
areaAnswer = new TextField("0");
lengthAnswer = new TextField("0");
tempAnswer = new TextField("0");
chemAnswer = new TextField("0");
cVolume = new TextField("0");
cArea = new TextField("0");
cLength = new TextField("0");
cTemp = new TextField("0");
cChem = new TextField("0"); //end of converter items
dialog = new TextField("Homework Helper: Chemistry Edition V1.0 by BMCA Tech. Press Continue to begin...",35);
preventChanges();//makes all answer fields in app uneditable
}
private void createStartScreen_(){//sets all buttons to false other than continue which is set to true and displays default pic and text.
Continue.setVisible(true);
dialog.setText("Homework Helper: Chemistry Edition V1.0 by BMCA Tech. Press Continue to begin...");
periodicTable.setVisible(false);
MainMenu.setVisible(false);
converter.setVisible(false);
fChart.setVisible(false);
balancer.setVisible(false);
solvers.setVisible(false);
element.setVisible(false);
box1.setVisible(false);
box2.setVisible(false);
box3.setVisible(false);
box4.setVisible(false);
box5.setVisible(false);
box11.setVisible(false);
box22.setVisible(false);
box33.setVisible(false);
box44.setVisible(false);
box55.setVisible(false);
cVolume.setVisible(false);
cArea.setVisible(false);
cLength.setVisible(false);
cTemp.setVisible(false);
cChem.setVisible(false);
volumeAnswer.setVisible(false);
areaAnswer.setVisible(false);
lengthAnswer.setVisible(false);
tempAnswer.setVisible(false);
chemAnswer.setVisible(false);
}//end createStartScreen
private void assignLocation(){
MainMenu.setLocation(10, 10);
box1.setLocation(10, 50);
}
private void preventChanges(){
volumeAnswer.setEditable(false);
areaAnswer.setEditable(false);
lengthAnswer.setEditable(false);
tempAnswer.setEditable(false);
chemAnswer.setEditable(false);
}
}