所以我正在制作Java,并制作了一个不错的小程序。这是代码:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class aa extends JFrame{
private JButton jb;
private JTextField jt0;
private JTextField jt1;
private JTextField jt2;
int jti1;
int jti2;
public aa(){
jb = new JButton(">> FIGHT <<");
jt0 = new JTextField("", 25);
jt1 = new JTextField("", 25);
jt2 = new JTextField("<< BATTLE VICTOR >>", 35);
jt0.setText("");
jt2.setEditable(false);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jt0.getText().length() > jt1.getText().length())
jt2.setText((jt0.getText() + " << IS VICTORIUS OVER >> " + jt1.getText()));
else if(jt1.getText().length() > jt0.getText().length())
jt2.setText((jt1.getText() + " << IS VICTORIUS OVER >> " + jt0.getText()));
else if(jt1.getText().length() == jt0.getText().length())
jt2.setText((jt1.getText() + " << TIED >> " + jt0.getText()));
};
}
);
add(jt0, BorderLayout.NORTH);
add(jt1, BorderLayout.NORTH);
add(jt2, BorderLayout.NORTH);
add(jb, BorderLayout.CENTER);
}
}
这是运行脚本:
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class a{
public static void main(String[] args){
aa b = new aa();
b.setLayout(new FlowLayout());
b.setTitle("BattleWords");
b.setSize(420, 150);
b.setVisible(true);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
到目前为止,我已经尝试了整个setIconImage()
废话,但没有奏效。我想添加 png 或 gif 或 ico 图像。我应该把它放在哪里?代码在哪里,它看起来如何?