编写一个JApplet程序

2024-11-24 06:54:39
推荐回答(1个)
回答1:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class Compare {
private JFrame frame;
private Container contentPane;

private TextField text;
private TextField text2;
private JButton check;
private JLabel label;
private TextArea area;

public Compare() {
frame = new JFrame();
frame.setBounds(300, 300, 300, 300);
contentPane = frame.getContentPane();
init();
}

public void init(){
contentPane.setLayout(new BorderLayout());
label =new JLabel("请先输入两个待比较的整数:");
contentPane.add(label,BorderLayout.NORTH);
text = new TextField(15);
contentPane.add(text,BorderLayout.EAST);
text2 = new TextField(15);
contentPane.add(text2,BorderLayout.WEST);
check = new JButton();
contentPane.add(check,BorderLayout.SOUTH);

text.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==text){
String str = text.getText();
text.setText("");
}
}
});

text2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==text){
String str = text.getText();
text.setText("");
}
}
});
check.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(text.getText());
int b = Integer.parseInt(text2.getText());
if(a > b)
label.setText(text.getText());
else
label.setText(text2.getText());
}
});

}

public void go(){
frame.setVisible(true);
}

public static void main(String[] args) {
(new Compare()).go();
}
}