按照你的要求编写的Java程序如下:
import javaawtFlowLayout;import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJOptionPane;
import javaxswingJTextField;
public class Calculate extends JFrame implements ActionListener{
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JLabel jl=new JLabel();
JButton jb1=new JButton("+");
JButton jb2=new JButton("");
Calculate(){
setLayout(new FlowLayout());
jb1addActionListener(this);
jb2addActionListener(this);
add(jtf1);add(jtf2);add(jl);add(jb1);add(jb2);
setResizable(false);
setSize(250, 150);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(aegetSource()==jb1){
if(jtf1getText()trim()equals("")){
JOptionPaneshowMessageDialog(this, "文本框不能为空");
jtf1requestFocus();
}else if(jtf2getText()trim()equals("")){
JOptionPaneshowMessageDialog(this, "文本框不能为空");
jtf2requestFocus();
}else{
int a=IntegerparseInt(jtf1getText()trim());
int b=IntegerparseInt(jtf2getText()trim());
jlsetText(StringvalueOf(a+b));
}
}
if(aegetSource()==jb2){
if(jtf1getText()trim()equals("")){
JOptionPaneshowMessageDialog(this, "文本框不能为空");
jtf1requestFocus();
}else if(jtf2getText()trim()equals("")){
JOptionPaneshowMessageDialog(this, "文本框不能为空");
jtf2requestFocus();
}else{
int a=IntegerparseInt(jtf1getText()trim());
int b=IntegerparseInt(jtf2getText()trim());
jlsetText(StringvalueOf(ab));
}
}
}
public static void main(String[] args) {
new Calculate();
}
}
运行结果:
import javaawtRectangle;
import javamathBigDecimal;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextArea;
import javaxswingJTextField;
public class Test {
private static JTextField jf =null;//长度值
private static JTextField jf1 =null;//宽度值
public static void main(String[] args) {
JFrame frame=new JFrame("求矩形面积");
JPanel p = new JPanel(); // 建立一个面板
JLabel chang = new JLabel("长:");
changsetBounds(new Rectangle(18, 53, 150, 38));
jf=new JTextField(20);
jfsetBounds(new Rectangle(50, 56, 150, 25));
JLabel kuan = new JLabel("宽:");
kuansetBounds(new Rectangle(18, 107, 150, 38));
jf1=new JTextField(20);
jf1setBounds(new Rectangle(50, 111, 150, 25));
JButton jb=new JButton("确认");
jbsetBounds(new Rectangle(18, 1072-53, 150, 38));
jbaddActionListener(new javaawteventActionListener() {
public void actionPerformed(javaawteventActionEvent e) {
if(jfgetText()!=null&&jf1!=null){
JFrame f=new JFrame("结果:");
JPanel p1 = new JPanel();
String cd=jfgetText();
String kd=jf1getText();
p1setLayout(null);
String str = "";
boolean flag = true;
if(cdequals("")){
str+="长不能为空,";
flag = false;
}
if(kdequals("")){
str+="宽不能为空,";
flag=false;
}
if(flag){
if(DoubleparseDouble(cd)<=0){
str+="长度需为正数!";
flag=false;
}
if(DoubleparseDouble(kd)<=0){
str+="宽度需为正数!";
flag=false;
}
if(flag){
str="矩形长:"+jfgetText()+",宽:"+jf1getText()+",面积:"+mianji(cd,kd);
}
}else{
str=strsubstring(0, strlength()-1);
}
JTextArea text=new JTextArea();
textsetBounds(new Rectangle(18, 40, 100, 40));
textsetText(str);
textsetEditable(false);
textsetLineWrap(true);
textsetWrapStyleWord(true);
p1add(text);
fadd(p1);
fsetSize(200,200);
fsetLocationRelativeTo(null);
fsetVisible(true);
fsetAlwaysOnTop(true);
}
}
});
padd(chang,null);
padd(jf);
padd(kuan,null);
padd(jf1);
psetLayout(null);
padd(jb);
frameadd(p);//将面板添加到窗口
framesetSize(300,300);//设置窗口大小
framesetAlwaysOnTop(true);//设置窗口始终在最上层
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);//设置默认的关闭 *** 作,关闭窗口后退出程序
framesetLocationRelativeTo(null);//设置窗口居中
framesetVisible(true);//设置窗口可见,尽快调用paint()方法
framesetResizable(false);
}
/计算面积的算法,为了使计算精确采用BigDecimal/
public static double mianji(String cd,String kd) {
BigDecimal bdcd=new BigDecimal(cd);
BigDecimal bdkd=new BigDecimal(kd);
return bdcdmultiply(bdkd)doubleValue();
}
}
运行了下效果如图:
JFrame frame=new JFrame("标题");
framesetSizi(30,30);//大小
framesetLocation(20,20);//位置
framesetVisible(true);//可见
framesetDefaultCloseOperation(frameEXIT_ON_CLOSE);//关闭
import javaxswing;
import javaawt;
public class test extends JFrame{
public test(){
JButton button; //按钮
JLabel label; //标签
JComboBox combobox;//下拉菜单
JCheckBox checkbox;//复选框
JRadioButton radiobutton;//单选框
JTextField textfield;//文本框
button = new JButton("按钮");
label = new JLabel("标签:");
checkbox = new JCheckBox("复选框一");
radiobutton = new JRadioButton("单选框一");
combobox = new JComboBox();
textfield = new JTextField(100);
Container c = thisgetContentPane();
csetLayout(new FlowLayout());
cadd(button);
cadd(label);
cadd(checkbox);
cadd(radiobutton);
comboboxaddItem("1");
comboboxaddItem("2");
cadd(combobox);
cadd(textfield);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
test mytest = new test();
}
}
import javaawt;
import javaawtevent;
public class TestTextField {
public static void main(String[] args) {
MyTestField mtf = new MyTestField();
}
}
class MyTestField extends Frame {
TextField tf1,tf2;
MyTestField() {
super("test");
tf1 = new TextField(10);
tf2 = new TextField(10);
Button b1 = new Button("确定");
b1addActionListener(new Monitor());
Label lb1 = new Label("输入");
Label lb2 = new Label("输出");
add(lb1);add(tf1);add(b1);add(lb2);add(tf2);
pack();
setLayout(new FlowLayout());
setSize(200,200);
setVisible(true);
}
class Monitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
tf2setText(tf1getText());
tf1setText("");
}
}
}
以上就是关于java编写一个窗体应用程序,在窗体中安排两个文本框,一个标签,两个标记为+和*的按钮。从两个文本全部的内容,包括:java编写一个窗体应用程序,在窗体中安排两个文本框,一个标签,两个标记为+和*的按钮。从两个文本、用Java程序设计知识,编写一个窗口程序。实现在文本框中输入矩形的长和宽,单击“确认”按钮,d出对、写一个java程序,实现创建Swing窗口,并改变窗口标题、窗口大小、窗口位置等。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)