boxui

时间:2024-10-28 11:52:56编辑:思创君

求JAVA大神用swing组件编写如图一小程序

import javax.swing.*;

public class Swing {
JFrame frame;
JPanel panel;
JLabel jla1,jla2,jla3;
JButton jbu;
JTextField jtf1,jtf2;
JComboBox box;
static final String[] email={"263.net","163.com","qq.com"};

public Swing(){
frame=new JFrame();
frame.setSize(600,300);
frame.setLocation(200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

jla1=new JLabel("邮箱");
jla2=new JLabel("@");
jla3=new JLabel("密码");

jtf1=new JTextField("用户名",10);
jtf2=new JTextField("密码",10);

box=new JComboBox(email);

jbu=new JButton("登陆");

panel=new JPanel();

panel.add(jla1);
panel.add(jtf1);
panel.add(jla2);
panel.add(box);
panel.add(jla3);
panel.add(jtf2);
panel.add(jbu);

frame.add(panel);

}

public static void main(String args[]){
new Swing();
}
}


Java语言,用Swing组件编写程序,要求输入圆的半径r,求圆的面积?

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DecimalFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Test {

JFrame frame = new JFrame("求圆的面积");
JLabel info = new JLabel("请输入圆的半径:");
JTextField rField = new JTextField();
JLabel result = new JLabel();

public void init() {
frame.setLayout(null);
frame.setSize(280, 100);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

info.setBounds(10, 10, 100, 30);
rField.setBounds(110, 15, 150, 20);
result.setBounds(30, 40, 220, 30);
frame.add(info);
frame.add(rField);
frame.add(result);
rField.addKeyListener(new KeyListener() {

public void keyPressed(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
double r = 0;
try {
r = Double.parseDouble(rField.getText());
} catch (NumberFormatException n) {
String txt = rField.getText();
if (!txt.equals("")) {
rField.setText(changeTxt(txt));
} else {
result.setText("");
}
return;
}
DecimalFormat format = new DecimalFormat("#0.00");
String area = format.format(Math.pow(Math.PI * r, 2));
result.setText("圆的面积: " + area);
}

public void keyTyped(KeyEvent e) {
}

});
}

public String changeTxt(String txt) {
StringBuffer s = new StringBuffer();
for (int i = 0; i < txt.length(); i++) {
int n = (int) txt.charAt(i);
if (n >= 48 && n <= 57 || n == 46) {
s.append(txt.charAt(i));
}
}
return s.toString();
}

public static void main(String[] args) {
new Test().init();
}
}


上一篇:百度阿波罗

下一篇:没有了