gpt4 book ai didi

java - 为什么我的 JButtons 在小程序调整大小之前不会出现,而我的 JCombo 框在单击之前是不可见的?

转载 作者:行者123 更新时间:2023-11-30 04:43:04 25 4
gpt4 key购买 nike

您好,首先我想说我是该网站的新手,并且对编程(主要是图形)非常陌生,对于提出任何愚蠢的问题,我想提前道歉。另外,我还要感谢所有愿意抽出时间来帮助我完成此任务的人。

我的问题是,当我尝试通过让某些 JButton 使某些按钮不可见然后其他按钮可见(使其看起来像不同的屏幕)来制作菜单时,除非我重新调整窗口大小或最小化,否则所有内容都会变得不可见。然而,执行此操作后,从一个菜单到另一个菜单的切换就可以完美地工作。另外,我还有一些出现的 JComboBox,但在单击它们之前是不可见或部分不可见的,我也不知道它们为什么这样做。

这个小程序将作为一个工具发布到网上,以帮助其他 child 学习化学,我首先开始研究的部分(JComboBoxes 部分)是一个单位转换器。我希望在正确放置所有内容之前让所有内容正常工作,我的一些变量名称可能会让其他人感到有点困惑,所以如果描述我的代码中应该做什么会让您更容易提供帮助,请询问,我我会尽力解释我在那里做什么。

再次感谢

这是我为小程序创建图形的代码:

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);
}
}

最佳答案

  1. 不要将 Swing 和 AWT 组件混合在一起。使用所有 Swing 组件。混合使用 Swing 和 AWT 组件会导致许多问题。
  2. 添加所有组件后调用validate()。这告诉 JVM 验证组件的布局。它是 pack() 对框架所做的一部分。
  3. 使用 CardLayout 在不同的 GUI 元素之间进行交换。

关于java - 为什么我的 JButtons 在小程序调整大小之前不会出现,而我的 JCombo 框在单击之前是不可见的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com