java上机考试题目 在线等
.创建一个包含如下组件的GUI1)用于提示用户进行信息输入的Label2)用于进行用户信息输入的TextField3)用户能够通过点击button进行信息的输入,同时清空...
.创建一个包含如下组件的GUI
1) 用于提示用户进行信息输入的Label
2) 用于进行用户信息输入的TextField
3) 用户能够通过点击button进行信息的输入,同时清空TextField 展开
1) 用于提示用户进行信息输入的Label
2) 用于进行用户信息输入的TextField
3) 用户能够通过点击button进行信息的输入,同时清空TextField 展开
展开全部
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);
}
}
});
}
}
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);
}
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询