我有使用Java GUI的作业,我是一个新的java编程。任何人都可以帮我解决问题吗?
我的屏幕截图:http://cdn.kaskus.com/images/2014/03/21/2556566_20140321011737.png
如果按"+"
按钮,文本字段将显示数字0,1,2,3,4,5,6,...
如果我按"-"
按钮,文本字段将正确减少数字,但不会低于0
。这是我的源代码:
package Main.Code;
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("-");
jButton2.setText("+");
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("0");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(68, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addComponent(jButton1))
.addContainerGap(80, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JTextField jTextField1;
}
答案 0 :(得分:0)
您需要为每个按钮提供ActionListener
,这将执行所需的任务(加/减)。
请查看How to Use Buttons, Check Boxes, and Radio Buttons和How to Write an Action Listener了解详情。
每次调用actionPerformed
方法时,您都需要从文本字段(jTextField1.getText()
)中获取值并将其解析为int
值,这可以通过使用Integer#parseInt
。
然后,您需要应用修改器并将值设置回文本字段(jTextField1.setText(String)
)。不是setText
需要String
而不是int
。您需要先使用Integer.toString(int)
之类的内容进行转换,例如......
有关JTextField
答案 1 :(得分:0)
试试这个:
class NewJFrame extends javax.swing.JFrame implements ActionListener {
int total=0;
/*...
....
code ...
*/
jButton1.setText("-");
jButton2.setText("+");
jButton1.addActionListener(this);
jButton2.addActionListener(this);
}
按钮动作事件:
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jButton2)
{
total +=1;
jTextField1.setText(""+total);
}
if(e.getSource()==jButton1)
{
total -=1;
jTextField1.setText(""+total);
}
}