189 8069 5689

java写图形界面的代码 java怎么写图形界面

java图形界面代码

import java.awt.*;

网站的建设成都创新互联公司专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为白乌鱼等企业提供专业服务。

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class ReadBook extends JFrame {

JTextArea jta;

JTextField jtf;

JButton jb;

public ReadBook() {

jta = new JTextArea();

jtf = new JTextField(30);

jtf.setText("文件保存路径如c:\\ab.txt");

jb = new JButton("保存文字");

JPanel jp = new JPanel();

jp.add(jtf);

jp.add(jb);

add(jta);

add(jp, BorderLayout.SOUTH);

setBounds(500, 100, 500, 380);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

jb.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

//-------------核心代码---------

String path = jtf.getText();

File f = new File(path);

String txt = jta.getText().replaceAll("\n", "\r\n");

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(f));

bw.write(txt);//写入文件中

bw.close();

} catch (Exception e1) {

e1.printStackTrace();

}

//-------------核心代码---------

}

});

}

public static void main(String[] args) {

new ReadBook();

}

}

求一个简单的java代码:(图形界面)

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

public class vv extends JDialog {

private static final long serialVersionUID = 1L;

private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);

private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);

private JTextField t_Id = new JTextField(10);

private JPasswordField t_pw = new JPasswordField(10);

private JButton btnLogin;

private JButton btnClose;

public vv() {

super();

setResizable(false);

getContentPane().setBackground(new Color(225, 225, 225));

getContentPane().setLayout(null);

initialize();

}

protected void initialize() {

setTitle("系统登录");

l_Id.setBounds(48, 43, 53, 25);

t_Id.setBounds(110, 43, 150, 25);

l_pw.setBounds(48, 93, 53, 25);

t_pw.setBounds(110, 93, 150, 25);

getContentPane().add(l_Id);

getContentPane().add(l_pw);

getContentPane().add(t_Id);

getContentPane().add(t_pw);

btnLogin = new JButton();

btnLogin.setText("登 录");

btnLogin.setBounds(70, 142, 85, 28);

btnLogin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

addBtnLoginActionListener();

}

});

getContentPane().add(btnLogin);

btnClose = new JButton();

btnClose.setText("关 闭");

btnClose.setBounds(175, 142, 85, 28);

btnClose.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

dispose();

System.exit(-1);

}

});

getContentPane().add(btnClose);

}

private void addBtnLoginActionListener() {

String user = t_Id.getText();

String password = new String(t_pw.getPassword());

if (user.equals("")) {

JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

if (password.equals("")) {

JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",

JOptionPane.WARNING_MESSAGE);

return;

}

String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"

+ password + "'";

boolean success = false;

// TODO:数据校验 success = executeQuery(sql);

if (success) {

// TODO: 如果数据校验成功 显示主界面 并关闭登录界面

JOptionPane.showMessageDialog(this, "成功登录", "提示",

JOptionPane.INFORMATION_MESSAGE);

this.dispose();

} else {

JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",

JOptionPane.WARNING_MESSAGE);

t_pw.requestFocus(); // 密码框选中

}

}

public Dimension getPreferredSize() {

return new Dimension(320, 170);

}

public void show() {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension screen = tk.getScreenSize();

Dimension d = getSize();

this.setLocation((screen.width - d.width) / 2,

(screen.height - d.height) / 2);

// 输入密码后回车相当于点击了登录按钮

getRootPane().setDefaultButton(btnLogin);

t_pw.requestFocus();

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setSize(300, 220);

super.show();

}

public static void main(String[] args) {

vv loginFrame = new vv();

loginFrame.setVisible(true);

}

}

希望对你有帮助

怎样用java编写图形界面的Application程序?

java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:

package mainFrame;

import java.awt.EventQueue;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

编写一个具有图形用户界面的Java程序

import javax.imageio.*;

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.io.*;

/**

* @author Hardneedl

*/

class ImageZoom extends JFrame {

private static final Dimension minSize = new Dimension(300, 200);

private static final Dimension maxSize = new Dimension(1024, 768);

private static final Dimension preferredSize = new Dimension(600, 400);

public Dimension getMaximumSize() {return maxSize;}

public Dimension getMinimumSize() {return minSize;}

public Dimension getPreferredSize() {return preferredSize;}

public String getTitle() {return "Frame Title";}

private class ImageCanvas extends JComponent{

private Image img;

private AffineTransform af=new AffineTransform();

private ImageCanvas(Image g,float s) {setImage(g);setScale(s);}

private void setImage(Image img){

this.img = img;

if (isVisible()) paintImmediately(getBounds());

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (img!=null) {

Graphics2D g2d = (Graphics2D)g.create();

g2d.drawImage(img,af,this);

}

}

private void setScale(float scale){

af = AffineTransform.getScaleInstance(scale,scale);

if (isVisible()) paintImmediately(getBounds());

}

};

private static ImageCanvas canvas;

private Image img;private float s;

ImageZoom(Image g,float s) throws HeadlessException {

img =g;this.s=s;

init();

doLay();

attachListeners();

}

private void init() {

canvas = new ImageCanvas(img,s);

}

private void doLay() {

Container container = getContentPane();

container.add(canvas,BorderLayout.CENTER);

pack();

}

private void attachListeners() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(final String[] args) {

SwingUtilities.invokeLater(

new Runnable(){

public void run() {

try {

new ImageZoom(ImageIO.read(new FileInputStream(args[0])),Float.parseFloat(args[1])).setVisible(true);

} catch (IOException e) {

e.printStackTrace();

}

}

}

);

}

}


网页标题:java写图形界面的代码 java怎么写图形界面
网站网址:http://cdxtjz.cn/article/ddedooc.html

其他资讯