JAVA基础编程题

界面设计:设计一个界面,包括一个单行文本框TextField,两个按钮显示和清除。当单击显示按钮时,在文本框中显示HelloWorld,当单击清除按钮时,清除文本框的内容

package com.qiu.swing.layoutDemo;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextField;

/**
*
* @author Qiu
*
*/
public class TextDemo extends JFrame{

final JButton button_show = new JButton("显示");
final JButton button_clear = new JButton("显示");
final JTextField text = new JTextField();
final Container con = this.getContentPane();

public TextDemo() {
this.setTitle("HelloWorld!");
this.setSize(300, 160);
// 居中
this.setLocationRelativeTo(null);
this.setUndecorated(true); // 去掉窗口的装饰
this.setResizable(false);

this.getRootPane().setWindowDecorationStyle(
JRootPane.INFORMATION_DIALOG);// 采用指定的窗口装饰风格

// 文字居中
text.setSize(100, 20);

Box vbox = Box.createVerticalBox();
Box xbox0 = Box.createHorizontalBox();
xbox0.add(text);
xbox0.add(button_show);
xbox0.add(button_clear);
vbox.add(xbox0);
vbox.add(Box.createVerticalStrut(100));
con.setLayout(new BoxLayout(con, BoxLayout.X_AXIS));
con.add(vbox);

button_show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("HelloWorld");
}
});
button_clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText("");
}
});

}
public static void main(String[] args) {
TextDemo home = new TextDemo();
home.setVisible(true);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-26
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Demo {
static final int WIDTH=300;
static final int HEIGHT=200;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame("显示与隐藏文本测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//窗体在屏幕居中显示
Toolkit kit =Toolkit.getDefaultToolkit();
Dimension dimension=kit.getScreenSize();
int width=dimension.width;
int height=dimension.height;
int x=(width-WIDTH)/2;
int y=(height-HEIGHT)/2;
frame.setLocation(x, y);

frame.setSize(WIDTH, HEIGHT);

//设置中间容器
JPanel panel=new JPanel();
frame.setContentPane(panel);

//采用FlowLayout布局管理
panel.setLayout(new FlowLayout());

JButton b1=new JButton("显示");
JButton b2=new JButton("清除");
final JTextField textField=new JTextField(10);

panel.add(textField);
panel.add(b1);
panel.add(b2);

b1.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setText("HelloWorld");

}

});

b2.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textField.setText("");

}

});

frame.setVisible(true);

}

}
第2个回答  2019-10-31
这是我的答案。希望对你有所帮助 public
class
Test
{
public
static
void
main(String[]
args)
{
double
high
=
100;//高度
double
sum
=
0;//经过路程
for
(int
i
=
1;
i
<
100;
i++)
{
if
(i
==
1)
{
sum
+=
high;//第一次落下单独计算
}
else
{
sum
+=
high
*
2;//第一次落下来还要弹上去,然后再下来。所有要乘以2
}
high
=
high
/
2;
System.out.println("第"
+
i
+
"次落地共经过"
+
sum
+
"米");
System.out.println("第"
+
i
+
"次反弹的高度是"
+
high
+
"米");
if
(high
<
0.1)
{
System.out.println("在第"
+
i
+
"次反弹的高度小于0.1米!");
return;
}
}
}
}