图形界面编程(C++ 图形界面编程,只能是MFC吗)

本文目录
- C++ 图形界面编程,只能是MFC吗
- JAVA程序设计,使用GUI界面
- 怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解
- java编程 完成下图所示的图形用户界面设计(窗口标题为你的真实学号及姓名,例如:11930670
- Java编写图形用户界面程序
- JAVA 编写一个java图形GUI程序,比较大小数并输出
- GUI画图板(绘图板)设计,用Java编写程序代码!!谢谢!!
- 如何用JavaGUI设计QQ那样的界面
- 用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
- java图形界面编程,要有运行的过程
C++ 图形界面编程,只能是MFC吗
C++编写图形界面程序,我们可以使用现有的成熟的图形界面库,
比如MFC、ATL、QT等。
qt库,这是一个跨平台的图形界面库,采用qt库编写的程序,在无需修改的情况下,可以 在linux、windows、mac等平台运行。可以用这个试试~~itjob助你成就开发美梦
JAVA程序设计,使用GUI界面
效果图
参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyFrame extends JFrame implements KeyListener{
JLabel jlkey;
public KeyFrame() {
jlkey = new JLabel("请输入字母或者数字,其他字符不显示");
add(jlkey);
addKeyListener(this);
setLayout(new FlowLayout());
setSize(260, 160);
setTitle("输入...");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args) {
new KeyFrame();
}
public void keyTyped(KeyEvent e) {//敲击键盘
c=e.getKeyChar();//得到键入的字符
repaint();//重绘
}
public void keyPressed(KeyEvent e) {//按下键盘
//c=e.getKeyChar();
//repaint();
}
public void keyReleased(KeyEvent e) {//释放键盘
}
char c ;
@Override
public void paint(Graphics g) {
super.paint(g);
//如果只能显示输入的是字母或者数字,那么需要if判断下
if((c《=’Z’&& c》=’A’)||(c《=’z’&&c》=’a’)||(c《=’9’&&c》=’0’)){// 注意比较的是字符 ’9’ 和字符’0’
Font font = g.getFont();
g.setColor(Color.BLUE);
g.setFont(new Font(font.getName(),font.getStyle(),20));
g.drawString(c+"",100, 100);//绘制
}
}
}
怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解
//正好以前注释过一个
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
import java.util.Random;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Timer;
public class CharacterRain extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
private Random random = new Random();
private Dimension screenSize;
private JPanel graphicsPanel;
private final static int gap = 20;
private int posArr;
private int lines;
private int columns;
public static void main(String args) {
new CharacterRain();
}
public CharacterRain() {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
graphicsPanel = new GraphicsPanel();
add(graphicsPanel, BorderLayout.CENTER);
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Image image = defaultToolkit.createImage(new MemoryImageSource(0, 0,
null, 0, 0));
Cursor invisibleCursor = defaultToolkit.createCustomCursor(image,
new Point(0, 0), "cursor");
setCursor(invisibleCursor);
KeyPressListener keyPressListener = new KeyPressListener();
this.addKeyListener(keyPressListener);
this.setAlwaysOnTop(true);
this.setUndecorated(true);
this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
lines = screenSize.height / gap; //用gap来分行
columns = screenSize.width / gap; //用gap来分列
posArr = new int;
random = new Random();
for (int i = 0; i 《 posArr.length; i++) {
posArr = random.nextInt(lines); //随机行数
}
new Timer(100, this).start();
}
private char getChr() {
return (char) (random.nextInt(94) + 33); //返回随机字符
}
public void actionPerformed(ActionEvent e) {
graphicsPanel. repaint(); // Timer事件,graphicsPanel重绘
}
@SuppressWarnings("serial")
private class GraphicsPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d. setFont (getFont (). deriveFont (Font. BOLD));
g2d. setColor(Color. BLACK);
g2d. fillRect (0, 0, screenSize. width, screenSize. height); // 设置背景色为黑色
int currentColumn = 0;
for (int x = 0; x 《 screenSize.width; x += gap) {
int endPos = posArr; //获得开始行位置
g2d. setColor(Color. GREEN);
g2d.drawString(String.valueOf(getChr()), x, endPos * gap); //画出随机开始行的字符
int cg = 0; //初始黑色
int length = 25; //字符列的长度
for (int j = endPos -length; j 《 endPos; j++) { //随机行逐列向上length行,为循环开始行
cg += 255/(length + 1); //渐变色递增,0,255,0为green
if (cg 》 255) {
cg = 255;
}
g2d.setColor(new Color(0, cg, 0));
g2d.drawString(String.valueOf(getChr()), x, j * gap); //画出随机行后的15行字符,颜色从黑色渐变成绿色,逐行增加
}
posArr += random.nextInt(5); //下落距离最快为4, 会产生加速下落的感觉
if ((posArr -5)* gap 》 getHeight()) { //如果行数位置大于屏幕高度,从新获取合适的行
posArr = random.nextInt(lines);
}
currentColumn++; //下一列获取随机行
}
}
}
private class KeyPressListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() ==KeyEvent.VK_ESCAPE) { //监听键盘事件,按ESC按键退出
System.exit(0);
}
}
}
}
java编程 完成下图所示的图形用户界面设计(窗口标题为你的真实学号及姓名,例如:11930670
好像是作业, 源代码发给你参考, 注意学习下代码. 了解下布局 和按钮的工作原理
参考代码
import java.awt.*;
import java.awt.event.*;
public class MyRandomFrame extends Frame implements ActionListener {
private final TextField tf1;
private final TextField tf2;
private final TextField tf3;
public static void main(String args) {
new MyRandomFrame().setVisible(true);
}
public MyRandomFrame() {
setSize(600, 100);
setLocationRelativeTo(null);
setTitle("学号:XXXXX 姓名:张三");
Button but1 = new Button("获取1~100之间的随机数");
but1.setActionCommand("随即");
but1.addActionListener(this);
Label label1 = new Label("第一个数:");
tf1 = new TextField(5);
Label label2 = new Label("第二个数:");
tf2 = new TextField(5);
Button but2 = new Button("求和");
but2.setActionCommand("求和");
but2.addActionListener(this);
tf3 = new TextField(5);
setLayout(new FlowLayout());
add(but1);
add(label1);
add(tf1);
add(label2);
add(tf2);
add(but2);
add(tf3);
addWindowListener(new WindowAdapter() {// 关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("随即")) {
int a = (int) (Math.random() * 100);
int b = (int) (Math.random() * 100);
tf1.setText(String.valueOf(a));
tf2.setText(String.valueOf(b));
} else if (command.equals("求和")) {
int x = Integer.parseInt(tf1.getText());
int y = Integer.parseInt(tf2.getText());
tf3.setText(String.valueOf((x + y)));
}
}
}
Java编写图形用户界面程序
运行如图
参考代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpf.setEchoChar(’*’);//用*号来隐藏密码的显示
jp1.add(jl1);jp1.add(jtf);
jp1.add(jl2);jp1.add(jpf);
jpc.add(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1.addActionListener(this);
jb1.setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2.addActionListener(this);
jb2.setActionCommand("no");
JPanel jp2 = new JPanel();
jp2.add(jb1);jp2.add(jb2);
add(jp2,BorderLayout.SOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim();
String pwd = new String(jpf.getPassword());
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtf.setText("");
jpf.setText("");
}
}
}
JAVA 编写一个java图形GUI程序,比较大小数并输出
1.首先定义三个输入框
JTextField firstNumber;
JTextField secontdNumber;
JTextField maxNumber;
2.然后怎样生成并把它们放到界面上我就不多说了。
3.接下来生成最大数按钮maxNumberButton和关闭按钮closeButton,并放在界面上。
然后让maxnNumberButton监听鼠标单击事件
maxNumberButton.addMouseListener(new MouseAdapter() {
if (SwingUtilities.isLeftMouseButton(e)) {//判断是否鼠标左键按下
//在这里获得第一个和第二个数并比较获得最大的数,当然,你还可以首先判断是否已经输入了两个数,判断输入的两个值是否是数值等等的。你也可以在外边实现一个方法,直接返回最大值。然后是把最大值显示出来。
//从输入框直接获得的是string,你要自己转换成数值,具体怎样做我就不说了。默认你已经获得了两个值,并得到最大值max,顺便说一下float和double是不能直接大于小于这样比较的。
maxNumber.setText("");//首先把之前显示的清除
maxNumber.setText(max);
}
});
关闭按钮处理类似
closeButton.addMouseListener(new MouseAdapter() {
if (SwingUtilities.isLeftMouseButton(e)) {
System.exit(0);
}
});
GUI画图板(绘图板)设计,用Java编写程序代码!!谢谢!!
只有矩形有圆形能移动,其它实现起来麻烦点,办法有的只是代码太多。
画圆弧改成了画曲线,圆弧稍麻烦,当然方法是很简单的,你可以自己思考一下。
双击13个颜色中的任意一个都会弹出颜色选择器。
有保存与打开功能。扩展名请用 .jdr
基本满足条件,细节可能不是很好,另,代码比较乱,怕不好看懂咯,呼呼。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JDraw {
public static void main(String args) {
JFrame f=new JFrame();
f.setDefaultCloseOperation(3);
f.setSize(880,600);
f.setLocationRelativeTo(null);
f.getContentPane().add(M.c);
f.getContentPane().add(M.m,"South");
f.setVisible(true);
}
}
class CVS extends Component implements ComponentListener,MouseListener,MouseMotionListener{
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {resized();}
public void componentShown(ComponentEvent e) {}
private void resized() {
int w=this.getWidth();
int h=this.getHeight();
tbuff=new BufferedImage(w,h,3);
makeBuff(w,h);
}
private void makeBuff(int w,int h) {
Graphics g = tbuff.getGraphics();
g.drawImage(buff,0,0,null);
g.dispose();
buff=new BufferedImage(w,h,3);
g=buff.getGraphics();
g.drawImage(tbuff,0,0,null);
g.dispose();
}
BufferedImage buff,tbuff;
CVS(){
this.addComponentListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
buff=new BufferedImage(1,1,3);
}
public void paint(Graphics gr){
Graphics2D g = buff.createGraphics();
g.setBackground(new Color(0xff000000,true));
g.clearRect(0,0,getWidth(),getHeight());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
M.sa.drawAll(g);
if(M.ts!=null)
M.ts.draw(g);
g.dispose();
gr.drawImage(buff,0,0,this);
gr.dispose();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
M.mp(e.getPoint());
}
public void mouseReleased(MouseEvent e) {
M.mr(e.getPoint());
}
public void mouseDragged(MouseEvent e) {
M.md(e.getPoint());
}
public void mouseMoved(MouseEvent e) {}
}
class Menu extends JComponent implements MouseListener,ActionListener{
JComboBox sbox,method;
CLabel cl;
JCheckBox fillC,drawB;
JRadioButton fc,bc;
ButtonGroup bg;
JButton clear,up,down,save,load;
Menu(){
this.setLayout(new FlowLayout());
method=new JComboBox(new Object{"draw","move","erase",});
add(method);
sbox=new JComboBox(new Object{"Pt","Ln","Rect","Cir","Arc",});
add(sbox);
cl=new CLabel;
for(int i=0; i《cl.length; i++){
cl=new CLabel();
cl.addMouseListener(this);
add(cl);
}
fc=new JRadioButton("fc",true);
bc=new JRadioButton("bc");
bg=new ButtonGroup();
bg.add(fc);bg.add(bc);
add(fc);add(bc);
fc.setOpaque(true);
bc.setOpaque(true);
fc.setBackground(Color.white);
bc.setBackground(Color.blue);
fillC=new JCheckBox("Fill",true);
drawB=new JCheckBox("Draw",true);
fillC.addActionListener(this);
drawB.addActionListener(this);
add(fillC);add(drawB);
clear=new JButton("clear");
clear.addActionListener(this);
add(clear);
up=new JButton("zUp");
up.addActionListener(this);
add(up);
down=new JButton("zDown");
down.addActionListener(this);
add(down);
save=new JButton("Save");
save.addActionListener(this);
add(save);
load=new JButton("Load");
load.addActionListener(this);
add(load);
}
public void mouseClicked(MouseEvent e) {
JLabel l=(JLabel)e.getSource();
if(e.getClickCount()==2){
Color sc=JColorChooser.showDialog(null, "Color chooser", l.getBackground());
l.setBackground(sc);
mousePressed(e);
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Color c=((JLabel)e.getSource()).getBackground();
if(fc.isSelected())
fc.setBackground(c);
else if(bc.isSelected())
bc.setBackground(c);
M.cp();
}
public void mouseReleased(MouseEvent e) {}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==clear)M.clear();
else if(e.getSource()==up)M.up();
else if(e.getSource()==down)M.down();
else if(e.getSource()==save)M.save();
else if(e.getSource()==load)M.load();
else if(e.getSource()==fillC||e.getSource()==drawB)M.cp();
}
}
class CLabel extends JLabel{
static Color cs={Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,
Color.blue,Color.magenta,Color.magenta.brighter(),
Color.white,Color.black,Color.gray,Color.LIGHT_GRAY,Color.DARK_GRAY,};
static int i;
CLabel(){
this.setOpaque(true);
this.setBackground(cs);
this.setBorder(BorderFactory.createLineBorder(Color.black));
this.setPreferredSize(new Dimension(10,20));
}
}
class M{
static JFileChooser jfc=new JFileChooser();
static Menu m=new Menu();
static CVS c=new CVS();
static SA sa=new SA();
static S ts=null,selected=null;
static Color fc,bc;
static void clear(){
sa.ss.clear();
c.repaint();
}
public static void cp() {
System.out.println(selected);
if(selected!=null){
selected.fillColor=m.fc.getBackground();
selected.borderColor=m.bc.getBackground();
selected.fc=m.fillC.isSelected();
selected.db=m.drawB.isSelected();
c.repaint();
}
}
public static void up() {
if(selected!=null){
sa.upZ(selected);
c.repaint();
}
}
public static void down(){
if(selected!=null){
sa.downZ(selected);
c.repaint();
}
}
static{
jfc.setFileFilter(new FileNameExtensionFilter("JDraw file (*.jdraw,*.jdr)","jdr","jdraw"));
}
static void save(){
int x=jfc.showSaveDialog(c);
if(x==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
try{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(sa);
oos.flush();
oos.close();
}catch(Exception e){}
}
}
static void load(){
int x=jfc.showOpenDialog(c);
if(x==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
try{
ObjectInputStream oos=new ObjectInputStream(new FileInputStream(f));
Object r=oos.readObject();
if(r!=null){
sa=(SA)r;
c.repaint();
}
oos.close();
}catch(Exception e){e.printStackTrace();}
}
}
static int mx,my,tx,ty,ox,oy;
static int pc=0,pmax;
static int sm;
static boolean ne=true;
static int mid;
static void mp(Point p){
mid=m.method.getSelectedIndex();
if(mid==0){
if(ne){
mx=p.x;my=p.y;
pc=0;
sm=m.sbox.getSelectedIndex();
pmax=sm==4?2:1;
ne=false;
}
++pc;
}
else if(mid==1){
checkSel(p);
mx=p.x;my=p.y;
}
else if(mid==2){
checkSel(p);
if(selected!=null){
sa.ss.remove(selected);
c.repaint();
}
}
}
private static void checkSel(Point p) {
selected=null;
for(int i=sa.ss.size();i》0; i--)
if(sa.ss.get(i-1).shape.contains(p)){
selected=sa.ss.get(i-1);break;
}
sa.select(selected);
c.repaint();
}
static void mt(){
Shape s=null;
int cx,cy,cw,ch;
switch(sm){
case 0:
case 2:
cx=Math.min(mx,tx);
cy=Math.min(my,ty);
cw=Math.abs(mx-tx);
ch=Math.abs(my-ty);
if(sm==0)
s=new Ellipse2D.Double(cx,cy,cw,ch);
else
s=new Rectangle(cx,cy,cw,ch);
break;
case 1:
s=new Line2D.Float(mx,my,tx,ty);
break;
case 3:
cw=Math.abs(mx-tx);
ch=Math.abs(my-ty);
int cd=(int)Math.sqrt(Math.pow(mx-tx,2)+Math.pow(my-ty,2))*2;
cx=mx-cd/2;
cy=my-cd/2;
s=new Ellipse2D.Double(cx,cy,cd,cd);
break;
case 4:
Path3D p=new Path3D.Double();
p.moveTo(mx,my);
if(pc==1){
p.lineTo(tx, ty);
}
else{
p.quadTo(ox,oy,tx,ty);
}
s=p;
break;
}
ts=new S(s,m.fc.getBackground(),m.bc.getBackground(),m.fillC.isSelected(),m.drawB.isSelected(),null);
c.repaint();
}
static void md(Point p){
if(mid==0){
if(!sa.ss.isEmpty()){
sa.ss.get(sa.ss.size()-1).sel=false;
}
if(pc》1){
ox=p.x;oy=p.y;
}
else{
tx=p.x;ty=p.y;
}
mt();
}
else if(mid==1){
if(selected!=null){
moveTo(selected,p);
c.repaint();
}
}
else if(mid==2){
checkSel(p);
if(selected!=null){
sa.ss.remove(selected);
c.repaint();
}
}
}
static void moveTo(S s, Point p) {
if(s.shape instanceof Rectangle){
Rectangle r=(Rectangle)s.shape;
r.setLocation(r.x+p.x-mx,r.y+p.y-my);
mx=p.x;my=p.y;
}
else if(s.shape instanceof Ellipse2D){
Ellipse2D e=(Ellipse2D)s.shape;
e.setFrame(e.getX()+p.x-mx,e.getY()+p.y-my,e.getWidth(),e.getHeight());
mx=p.x;my=p.y;
}
}
static void mr(Point p) {
if(pc==pmax){
pc=0;
ne=true;
sa.add(ts);
selected=ts;
ts=null;
}
}
}
class S implements Serializable{
boolean fc,db,sel=true;
Shape shape;
Color fillColor,borderColor;
Stroke stroke;
static Stroke bstroke=new MyBasicStroke();
static Stroke selectStroke=new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,1,new float{5,2},1);
S(Shape s,Color c,Color b,boolean f,boolean d,Stroke k){
this.shape=s;this.fillColor=c;this.borderColor=b;this.fc=f;this.db=d;this.stroke=k==null?bstroke:k;
}
void draw(Graphics2D g){
if(fc){
g.setColor(fillColor);
g.fill(shape);
}
if(db){
g.setColor(borderColor);
g.setStroke(stroke);
g.draw(shape);
}
if(sel){
g.setColor(Color.green);
g.setStroke(selectStroke);
g.draw(shape.getBounds());
}
}
}
class MyBasicStroke extends BasicStroke implements Serializable{}
class SA implements Serializable{
ArrayList《S》 ss=new ArrayList《S》();
void add(S s){
if(s!=null){
for(S sx:ss)
sx.sel=false;
ss.add(s);
}
}
S remove(int i){
return ss.remove(i);
}
void remove(S s){
ss.remove(s);
}
void upZ(S s){
indexZ(s,1);
}
void downZ(S s){
indexZ(s,-1);
}
void indexZ(S s, int i) {
int si=ss.indexOf(s);
if(si+i《0||si+i》ss.size()-1)return;
swap(s,ss.get(si+i));
}
void swap(S a,S b){
int ai=ss.indexOf(a);
int bi=ss.indexOf(b);
ss.set(ai,b);
ss.set(bi,a);
}
void select(S s){
for(S x:ss)
x.sel=false;
if(s!=null)
s.sel=true;
}
void drawAll(Graphics2D g){
for(S s:ss)
s.draw(g);
}
}
如何用JavaGUI设计QQ那样的界面
代码没法给你写,但是道理是很简单的.其实QQ的列表原理非常简单,其界面就是一颗JTree,设置树根不可见,树根的没给子节点就是每个分组.而列表内容的实现就更简单了,自己写一个实现了TableCellRenderer的渲染器,然后给设置为分组下每个节点的渲染器就行了.数据结构也超简单,就是三层树,第一层是根root,设置为不可见,所以只能看到他的几个节点;第二层是根root的节点,也就是分组,有几个分组就有几个节点,新建一个分组就是在root上添加一个新的子节点;第三层就是每个分组的内容了,这就是QQ列表的实现原理.别告诉我你不知道渲染器是什么,如果还没学到,等学到了你就能做出来.
用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算
下午发
五分钟搞定,先给你发个简单的,照例子自己去做你想要的计算器
package aaa;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YunSuan implements ActionListener,ItemListener
{
public static JFrame jf;
public static Container c;
public static JTextField jtf1,jtf2,jtf3;
public static JButton jb1;
public static Choice c2;
public static String fuhao="";
public YunSuan()
{
jf=new JFrame("加减乘除运算");
c=jf.getContentPane();
c.setLayout(new FlowLayout());
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
c2=new Choice();
jb1=new JButton("=");
}
public void Jiemian()
{
c.add(jtf1);
c2.addItemListener(this);
c2.add("");
c2.add("+");
c2.add("-");
c2.add("*");
c2.add("/");
c.add(c2);
c.add(jtf2);
jb1.addActionListener(this);
c.add(jb1);
c.add(jtf3);
jf.setLocation(200,100);
jf.setVisible(true);
jf.pack();
}
public static void main(String args)
{
YunSuan ys=new YunSuan();
ys.Jiemian();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double s1=Integer.parseInt(jtf1.getText());
double s2=Integer.parseInt(jtf2.getText());
double result=0;
if(fuhao.equals("+"))
{
result=s1+s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("-"))
{
result=s1-s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("*"))
{
result=s1*s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
if(fuhao.equals("/"))
{
result=s1/s2;
String result2=String.valueOf(result);
jtf3.setText(result2);
}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==c2)
{
String str1=c2.getSelectedItem();
fanhui(str1);
}
}
public String fanhui(String str2)
{
return fuhao=str2;
}
}
java图形界面编程,要有运行的过程
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class BorrowerInfoPanel {
private JLabel oldKeyLabel, newKeyLabel;
private JPasswordField oldKeyField, newKeyField, confirmKeyField;
private JPanel infoPanel;
private JLabel name1, name2;
private JLabel position1, position2;
private JLabel id1, id2;
private JLabel mail1, mail2;
private JLabel maxBorrowNum1, maxBorrowNum2;
private JLabel maxBorrowTime1, maxBorrowTime2;
private JLabel maxContinueBorrowTime1, maxContinueBorrowTime2;
private JButton change_bt;
private Borrower borrower;
private JFrame frame = new JFrame();;
private JDialog jd = new JDialog(frame, "修改密码", true);
public BorrowerInfoPanel() {
infoPanel = new JPanel();
borrower = new Borrower("1", "dg", "123456");
name1 = new JLabel("姓名");
name2 = new JLabel(borrower.getBorrowerName());
position1 = new JLabel("身份");
position2 = new JLabel("本科生");
id1 = new JLabel("编号");
id2 = new JLabel(borrower.getBorrowerNum());
mail1 = new JLabel("邮箱");
mail2 = new JLabel("453636@qq.com");
maxBorrowNum1 = new JLabel("最大借阅册数");
maxBorrowNum2 = new JLabel("borrower.maxBorrowedNumber");
maxBorrowTime1 = new JLabel("最长借阅时间");
maxBorrowTime2 = new JLabel("31天");
maxContinueBorrowTime1 = new JLabel("最大续借次数");
maxContinueBorrowTime2 = new JLabel("borrower.maxContinueBorrowTime");
change_bt = new JButton("修改密码");
infoPanel.setLayout(new GridLayout(8, 2));
infoPanel.setBorder(new TitledBorder(borrower.getBorrowerName()
+ ":你好!"));
infoPanel.add(id1);
infoPanel.add(id2);
infoPanel.add(name1);
infoPanel.add(name2);
infoPanel.add(position1);
infoPanel.add(position2);
infoPanel.add(mail1);
infoPanel.add(mail2);
infoPanel.add(maxBorrowNum1);
infoPanel.add(maxBorrowNum2);
infoPanel.add(maxBorrowTime1);
infoPanel.add(maxBorrowTime2);
infoPanel.add(maxContinueBorrowTime1);
infoPanel.add(maxContinueBorrowTime2);
infoPanel.add(new JLabel());
infoPanel.add(change_bt);
oldKeyLabel = new JLabel(" 旧密码");
newKeyLabel = new JLabel(" 新密码");
oldKeyField = new JPasswordField(10);
newKeyField = new JPasswordField(10);
confirmKeyField = new JPasswordField(10);
oldKeyField.setEditable(true);
newKeyField.setEditable(true);
confirmKeyField.setEditable(true);
}
public void init() {
change_bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jd.setVisible(true);// TODO Auto-generated method stub
}
});
jd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
jd.setVisible(false);
}
});
jd.setLocation(620, 340);
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
jp.add(oldKeyField);
jp.add(oldKeyLabel);
jp2.add(newKeyField);
jp2.add(newKeyLabel);
Box top = Box.createVerticalBox();
top.add(jp);
top.add(jp2);
jd.add(top);
jd.pack();
jd.setResizable(false);
frame.setLocation(450, 240);
frame.add(infoPanel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args) {
new BorrowerInfoPanel().init();
}
class Borrower {
private String borrowerName;
public String getBorrowerName() {
return borrowerName;
}
public void setBorrowerName(String borrowerName) {
this.borrowerName = borrowerName;
}
public String getBorrowerNum() {
return borrowerNum;
}
public void setBorrowerNum(String borrowerNum) {
this.borrowerNum = borrowerNum;
}
public String getPassword() {
return password;
}
public void setId(String id) {
this.password = id;
}
private String borrowerNum;
private String password;
public Borrower(String borrowerNum, String borrowerName, String password) {
this.password = password;
this.borrowerName = borrowerName;
this.borrowerNum = borrowerNum;
}
}
}

更多文章:
avantage怎么设置为中文(Avantage安装丢失GIT)
2026年10月15日 03:00
activex控件未注册怎么办(登陆视频监控网页时提示activex控件未注册,怎么解决)
2026年10月14日 20:30
excel连接sql数据库教程(excel怎么连接sql数据库)
2026年10月14日 19:40






