「java编写gui」java编写gui扑克牌程序

博主:adminadmin 2022-12-05 19:21:06 66

本篇文章给大家谈谈java编写gui,以及java编写gui扑克牌程序对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何创建Java GUI项目?(文字说明)输出“大家好,我是XXX”

java的GUI项目通常要用到swing库,代码如下若干行:

import javax.swing.*;

public class HelloWorldSwing {

private static void createAndShowGUI() {

/* 确保一个漂亮的外观风格*/

JFrame.setDefaultLookAndFeelDecorated(true);

/*创建及设置窗口*/

JFrame frame = new JFrame("HelloWorldSwing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/*添加 "大家好" 标签*/

JLabel label = new JLabel("大家好,我是xxx");

frame.getContentPane().add(label);

/*显示窗口*/

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

/* 显示应用 GUI*/

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

Java写一个GUI

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class BankAccountViewer {

public static void main(String[] args) {

new BankAccountFrame().setVisible(true);

}

}

class BankAccountFrame extends JFrame implements ActionListener{

BankAccount ba;

JTextField tf = new JTextField(6);

JButton bw = new JButton("Withdraw");

JButton bd = new JButton("Deposit");

JLabel la = new JLabel("Amount");

JLabel lb = new JLabel();

public BankAccountFrame() {

ba = new BankAccount();

JPanel p = new JPanel();

p.add(la);

p.add(tf);

p.add(bw);

p.add(bd);

add(p);

lb.setHorizontalAlignment(JLabel.CENTER);

add(lb, BorderLayout.SOUTH);

lb.setText("Balance=" + ba.getBalance());

this.setDefaultCloseOperation(3);

this.pack();

bd.addActionListener(this);

bw.addActionListener(this);

}

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == bw) {

double wi = Double.parseDouble(tf.getText());

ba.withdraw(wi);

lb.setText("Balance=" + ba.getBalance());

lb.setText("Balance=" + ba.getBalance());

lb.setText("Balance=" + ba.getBalance());

}

if (e.getSource() == bd) {

double de = Double.parseDouble(tf.getText());

ba.deposit(de);;

lb.setText("Balance=" + ba.getBalance());

}

}

}

class BankAccount {

double balance = 1000;

public double getBalance() {

return balance;

}

public void deposit(double de) {

balance += de;

}

public void withdraw(double wi) {

if (balance = wi) {

balance -= wi;

}

}

}

用JAVA编写一个GUI记事本程序,实现文本的输入,保存,修改,打开操作

代码如下:

import java.io.*;

import java.awt.*;

import java.awt.event.*;

public class jtxtfm{

public static void main(String args[]){

jtxtfrm fm=new jtxtfrm();

}

}

class jtxtfrm extends Frame implements ActionListener{

FileDialog op,sv;

Button btn1,btn2,btn3;

TextArea tarea;

jtxtfrm(){

super("读写文件");

setLayout(null);

setBackground(Color.cyan);

setSize(600,300);

setVisible(true);

btn1=new Button("打开");

btn2=new Button("保存");

btn3=new Button("关闭");

tarea=new TextArea("");

add(btn1);add(btn2);add(btn3);add(tarea);

tarea.setBounds(30,50,460,220);

btn1.setBounds(520,60,50,30);

btn2.setBounds(520,120,50,30);

btn3.setBounds(520,180,50,30);

op=new FileDialog(this,"打开",FileDialog.LOAD);

sv=new FileDialog(this,"保存",FileDialog.SAVE);

btn1.addActionListener(this);

btn2.addActionListener(this);

btn3.addActionListener(this);

    addWindowListener(

     new WindowAdapter(){

      public void windowClosing(WindowEvent e){

       setVisible(false);

       System.exit(0);

      }

     }    

    );

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1){

String str;

op.setVisible(true);

try{

File f1=new File(op.getDirectory(),op.getFile());

FileReader fr=new FileReader(f1);

BufferedReader br=new BufferedReader(fr);

tarea.setText("");

while((str=br.readLine())!=null)tarea.append(str+'\n');

fr.close();

}

catch(Exception e1)

{}

}

if(e.getSource()==btn2){

sv.setVisible(true);

try{

File f1=new File(sv.getDirectory(),sv.getFile());

FileWriter fw=new FileWriter(f1);

BufferedWriter bw=new BufferedWriter(fw);

String gt=tarea.getText();

bw.write(gt,0,gt.length());

bw.flush();

fw.close();

}

catch ( Exception e2)

{}

}

if(e.getSource()==btn3){

System.exit(0);

}

}

}

效果图:

关于java编写gui和java编写gui扑克牌程序的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-05,除非注明,否则均为首码项目网原创文章,转载请注明出处。