gpt4 book ai didi

java - 将 Swing 应用程序转换为 Applet 的复杂性

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

我终于在 Netbeans 中正确编译了这个计算器应用程序并运行客户端,没有错误,但由于某种原因我无法让它作为小程序工作。我花了很多时间试图解决这个问题并进行研究,但毫无结果。有高手帮忙看一下,看看是什么问题吗?

我非常感谢任何建议,谢谢。

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

最佳答案

当使用小程序时,人们通常在 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();
}
}

关于java - 将 Swing 应用程序转换为 Applet 的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139011/

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