我正在尝试在JAVA中的类中实现一个类,比如
package test;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author jackandjill
*/
public class Summingup extends JFrame {
/**
* Creates new form Summingup
*/
public Summingup() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
input1_tb = new javax.swing.JTextField();
calculate_btn = new javax.swing.JButton();
result_lbl = new javax.swing.JLabel();
input2_tb = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Input 1");
jLabel2.setText("Input 2");
calculate_btn.setText("SUM");
result_lbl.setText("RESULT DISPLAYED HERE!");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(input1_tb))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(result_lbl))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(input2_tb)))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(calculate_btn)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(calculate_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(input1_tb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(input2_tb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(result_lbl)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Summingup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Summingup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Summingup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Summingup.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Summingup().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calculate_btn;
private javax.swing.JTextField input1_tb;
private javax.swing.JTextField input2_tb;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel result_lbl;
// End of variables declaration
}
class InvalidValueException implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource() ==calculate_btn)
{
String input_1 = input1_tb.getText();
String input_2 = input2_tb.getText();
checkBlanklnput(input_1,input_2);
validateInput(Integer.parseInt(input_1),Integer.parseInt(input_2));
result_lbl.setText(calculate(Integer.parseInt(input_1),Integer.parseInt(input_2)));
}
}
catch (NumberFormatException excep)
{
result_lbl.setText("Invalid number");
}
catch (InvalidInputException excep)
{
result_lbl.setText(excep.getMessage());
}
catch (BlankInputException excep)
{
result_lbl.setText(excep.getMessage());
}
}
public void checkBlanklnput(String input1,String input2) throws BlanklnputException
{
if (input1.length() == 0 ||input2.length() == 0) throw new BlanklnputException();
}
public void validateInput(int input1 , int input2) throws InvalidInputException
{
if (input1 + input2 > 100 )
throw new InvalidInputException();
}
public String calculate(int input1, int input2)
{
String res = "";
while (true)
{
res = Integer.toString(input1 + input2);
break;
}
return res;
}
}
class InvalidInputException extends Exception {
public InvalidInputException() {
super("Invalid number");
}
}
class BlankInputException extends Exception {
public BlankInputException() {
super("Input cannot be blank");
}
}
所以这个类也抛出很多异常并捕获它们。但唯一的问题是 InvalidValueException 类中无法识别JForm控件。 不知道如何打开这个控件到这个类。
我面临的另一个问题是我的validateInput正在检查最大总和但我想检查IF是否在文本框中输入了除数字之外的任何字母。
这就是我的GUI看起来像
答案 0 :(得分:3)
您可以将您的实施与显示的here工作示例进行比较。它使用JFormattedTextField
来强制执行数字输入,以及PropertyChangeListener
&amp; FocusListener
更新总和。
顺便说一句,在过度依赖GUI编辑器之前,你应该学习layouts。另请参阅Nested Classes。
附录:BlankInputException
拼写错误,你的(名字奇怪的)ActionListener
未被使用。您应该能够使用自定义代码属性对其进行初始化。
calculate_btn.addActionListener(new InvalidValueException());