我终于在Netbeans中得到了这个计算器应用程序来正确编译并运行客户端而没有错误但由于某种原因我不能让它作为applet工作。我花了很多时间试图弄清楚并研究但无济于事。有任何专家快速查看,看看问题是什么?
我真的很感激任何建议,谢谢。
package eventhandler;
import javax.swing.JApplet;
public class CalculatorApplet extends JApplet {
@Override
public void init() {
}
@SuppressWarnings("unchecked")
private void initComponents() {
firstText = new javax.swing.JTextField();
secondText = new javax.swing.JTextField();
postLabel = new javax.swing.JLabel();
postButton = new javax.swing.JButton();
comboBox = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
postButton.setText("Solve");
postButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
postName(evt);
}
});
comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "+", "-", "*", "/" }));
jButton1.setText("Clear");
jButton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearFields(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(47, 47, 47)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, secondText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.LEADING, firstText)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(21, 21, 21)
.add(comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(postLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 51, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(postButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButton1)))
.addContainerGap(24, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(firstText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(secondText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(comboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(postButton)
.add(jButton1))
.add(5, 5, 5)
.add(postLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
.addContainerGap())
);
//pack();
}// </editor-fold>
private void postName(java.awt.event.ActionEvent evt) {
String ft = firstText.getText();
String lt = secondText.getText();
String total;
double parse1, parse2;
Object selectedCombo = comboBox.getSelectedItem();
if (selectedCombo == "+") {
parse1 = Double.parseDouble(ft);
parse2 = Double.parseDouble(lt);
total = String.valueOf(parse1 + parse2);
postLabel.setText(total);
} else if (selectedCombo == "-") {
parse1 = Double.parseDouble(ft);
parse2 = Double.parseDouble(lt);
total = String.valueOf(parse1 - parse2);
postLabel.setText(total);
} else if (selectedCombo == "*") {
parse1 = Double.parseDouble(ft);
parse2 = Double.parseDouble(lt);
total = String.valueOf(parse1 * parse2);
postLabel.setText(total);
} else if (selectedCombo == "/") {
parse1 = Double.parseDouble(ft);
parse2 = Double.parseDouble(lt);
total = String.valueOf(parse1 / parse2);
postLabel.setText(total);
}
}
private void clearFields(java.awt.event.ActionEvent evt) {
firstText.setText(null);
secondText.setText(null);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new EventGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox comboBox;
private javax.swing.JTextField firstText;
private javax.swing.JButton jButton1;
private javax.swing.JButton postButton;
private javax.swing.JLabel postLabel;
private javax.swing.JTextField secondText;
// End of variables declaration
private void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
答案 0 :(得分:2)
使用applet时,人们通常会在init()方法中添加GUI代码。你的init()方法是空的。简单的例子:
//<applet code="AppletBasic.class" width="500" height="300"></applet>
// The above line makes it easy to test the applet from the command line by using:
// appletviewer AppletBasic.java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class AppletBasic extends JApplet
{
/**
* Create the GUI. For thread safety, this method should
* be invoked from the event-dispatching thread.
*/
private void createGUI()
{
JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
appletLabel.setHorizontalAlignment( JLabel.CENTER );
appletLabel.setFont(new Font("Serif", Font.PLAIN, 36));
add( appletLabel );
}
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
}
catch (Exception e)
{
System.err.println("createGUI didn't successfully complete: " + e);
}
}
public static void main(String[] args)
{
JApplet applet = new AppletBasic();
applet.init();
JFrame frame = new JFrame("Applet in Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( applet );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
applet.start();
}
}
答案 1 :(得分:0)
我可以注意到
B)我没有注意到任何this.getContentPane().add()等 你的小程序:(但仍然扩展了JApplet
C)当然,您应该将initGUI()放在init()方法中 因为在applet中,与桌面应用中的主要方法相同:)
无论如何,它只是一个快速查看...所以如果你有一些抛出的异常,你在你的问题中添加它会更有帮助。这将有助于以更深入的方式分析问题:)
祝你好运