java上机考试题目 在线等

2024-12-01 03:31:49
推荐回答(1个)
回答1:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

public class TestFrame extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton jButton1;
private JTextField jTextField1;
private JLabel jLabel1;
private JPanel jPanel3;
private JPanel jPanel2;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestFrame inst = new TestFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public TestFrame() {
super();
initGUI();
this.addListener();
}

private void initGUI() {
try {
FlowLayout thisLayout = new FlowLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1);
jPanel1.setPreferredSize(new java.awt.Dimension(375, 66));
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("\u8bf7\u8f93\u5165\u4fe1\u606f:");
jLabel1.setPreferredSize(new java.awt.Dimension(73, 24));
}
}
{
jPanel2 = new JPanel();
getContentPane().add(jPanel2);
jPanel2.setPreferredSize(new java.awt.Dimension(375, 66));
{
jTextField1 = new JTextField();
jPanel2.add(jTextField1);
jTextField1.setPreferredSize(new java.awt.Dimension(260, 29));
}
}
{
jPanel3 = new JPanel();
getContentPane().add(jPanel3);
jPanel3.setPreferredSize(new java.awt.Dimension(375, 66));
{
jButton1 = new JButton();
jPanel3.add(jButton1);
jButton1.setText("清空");
jButton1.setPreferredSize(new java.awt.Dimension(92, 24));
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}

public void addListener(){
jButton1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if("清空".equals(command)){
TestFrame.this.jTextField1.setText(null);
}
}
});
}
}