我的applet基本上是两个组合框,用于在变量及其事件中存储值(当用户选择一个选项时)。确认按钮生成将这两个值相加的事件。应用程序运行正常,但是当我尝试将其转换为applet时,文本字段不显示,并且每当我单击组合选项时似乎都会出现一些警告标志。请提出建议吗?
以下是applet的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DormPlanApplet extends JApplet
{
private JPanel selectionPanel;
private JPanel costPanel;
private JComboBox dormListBox;
private JComboBox mealPlanListBox;
private JLabel costLabel;
private JTextField costField;
private JButton confirmButton;
double dormCost;
double mealCost;
double totalCost;
int checker1;
int checker2;
String costString;
private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" };
public void init()
{
//super("College Cost Calculator");
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildSelectionPanel();
buildCostPanel();
add(selectionPanel, BorderLayout.CENTER);
add(costPanel, BorderLayout.SOUTH);
//pack();
//setVisible(true);
}
private void buildSelectionPanel()
{
selectionPanel = new JPanel();
selectionPanel.setLayout(new FlowLayout());
dormListBox = new JComboBox(dormListArray);
mealPlanListBox = new JComboBox(mealPlanListArray);
dormListBox.addActionListener(new dormCostListener());
mealPlanListBox.addActionListener(new mealCostListener());
selectionPanel.add(dormListBox);
selectionPanel.add(mealPlanListBox);
}
private void buildCostPanel()
{
costPanel = new JPanel();
costPanel.setLayout(new FlowLayout());
costLabel = new JLabel("The total cost is:");
confirmButton = new JButton("Confirm");
confirmButton.addActionListener(new calcButtonListener());
costField = new JTextField(12);
costField.setEditable(false);
costPanel.add(confirmButton);
costPanel.add(costLabel);
costPanel.add(costField);
}
private class dormCostListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
checker1 = 1;
switch (dormListBox.getSelectedIndex())
{
case 0:
dormCost = 1500;
break;
case 1:
dormCost = 1600;
break;
case 2:
dormCost = 1200;
break;
case 3:
dormCost = 1800;
break;
default:
dormCost = 0;
break;
}
}
}
private class mealCostListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
checker2 = 1;
switch (mealPlanListBox.getSelectedIndex())
{
case 0:
mealCost = 560;
break;
case 1:
mealCost = 1095;
break;
case 2:
mealCost = 1500;
break;
default:
mealCost = 0;
break;
}
}
}
private class calcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if ((checker1 == 1) && (checker2 == 1))
{
totalCost = dormCost + mealCost;
costString = Double.toString(totalCost);
costField.setText(costString);
} else {
costField.setText("It doesn't work!");
}
}
}
}
以下是oringal申请的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DormPlanApp extends JFrame
{
private JPanel selectionPanel;
private JPanel costPanel;
private JComboBox dormListBox;
private JComboBox mealPlanListBox;
private JLabel costLabel;
private JTextField costField;
private JButton confirmButton;
double dormCost;
double mealCost;
double totalCost;
int checker1;
int checker2;
String costString;
private String[] dormListArray = {"Allen Hall", "Pike Hall", "Farthing Hall", "University Suites" };
private String[] mealPlanListArray = {"7 Meals", "14 Meals", "Unlimited Meals" };
public DormPlanApp()
{
super("College Cost Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildSelectionPanel();
buildCostPanel();
add(selectionPanel, BorderLayout.CENTER);
add(costPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void buildSelectionPanel()
{
selectionPanel = new JPanel();
selectionPanel.setLayout(new FlowLayout());
dormListBox = new JComboBox(dormListArray);
mealPlanListBox = new JComboBox(mealPlanListArray);
dormListBox.addActionListener(new dormCostListener());
mealPlanListBox.addActionListener(new mealCostListener());
selectionPanel.add(dormListBox);
selectionPanel.add(mealPlanListBox);
}
private void buildCostPanel()
{
costPanel = new JPanel();
costPanel.setLayout(new FlowLayout());
costLabel = new JLabel("The total cost is:");
confirmButton = new JButton("Confirm");
confirmButton.addActionListener(new calcButtonListener());
costField = new JTextField(12);
costField.setEditable(false);
costPanel.add(confirmButton);
costPanel.add(costLabel);
costPanel.add(costField);
}
private class dormCostListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
checker1 = 1;
switch (dormListBox.getSelectedIndex())
{
case 0:
dormCost = 1500;
break;
case 1:
dormCost = 1600;
break;
case 2:
dormCost = 1200;
break;
case 3:
dormCost = 1800;
break;
default:
dormCost = 0;
break;
}
}
}
private class mealCostListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
checker2 = 1;
switch (mealPlanListBox.getSelectedIndex())
{
case 0:
mealCost = 560;
break;
case 1:
mealCost = 1095;
break;
case 2:
mealCost = 1500;
break;
default:
mealCost = 0;
break;
}
}
}
private class calcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if ((checker1 == 1) && (checker2 == 1))
{
totalCost = dormCost + mealCost;
costString = Double.toString(totalCost);
costField.setText(costString);
} else {
costField.setText("It doesn't work!");
}
}
}
public static void main(String[] args)
{
new DormPlanApp();
}
}
答案 0 :(得分:1)
您需要设置小程序的大小。在测试时,您可以使用setSize方法,如下所示:
public void init()
{
//super("College Cost Calculator");
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildSelectionPanel();
buildCostPanel();
add(selectionPanel, BorderLayout.CENTER);
add(costPanel, BorderLayout.SOUTH);
setSize(400, 100);
//pack();
//setVisible(true);
}
HTML applet语句会将宽度和高度传递给applet,因此您可以通过applet语句参数设置大小。
编辑添加:您为组合框设置默认字符串,但不是默认值。如果有人只按下计算按钮,接受组合框默认值,那么您的程序不会计算正确的值。