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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎样用java编写图形界面的Application程序相关的知识,希望对你有一定的参考价值。

包括分别用于输入字符串和浮点数的两个TextField,以及两个按钮(一个是“输入”按钮,一个是“输出”按钮)和一个TextArea。用户在两个TextField中输入数据并单击“输入”按钮后,程序把这两个数据保存入一个文件file.dat中,单击“确定”按钮,则把这个文件的内容显示在TextArea中。

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;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.border.EmptyBorder;


public class Mian_login extends JFrame


private JPanel contentPane;

private JTextField text_LoginName;

private JPasswordField Login_password;


/**

* Launch the application.

*/

public static void main(String[] args)

EventQueue.invokeLater(new Runnable()

@Override

public void run()

try

Mian_login frame = new Mian_login();

frame.setVisible(true);

catch (Exception e)

e.printStackTrace();

);


/**

* Create the frame.

*/

public Mian_login()

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(500, 200, 443, 300);

setResizable(false);

setTitle("登 录");

/*获取系统按钮样式*/

String lookAndFeel = UIManager.getSystemLookAndFeelClassName();

try

UIManager.setLookAndFeel(lookAndFeel);

catch (ClassNotFoundException e1)

e1.printStackTrace();

catch (InstantiationException e1)

e1.printStackTrace();

catch (IllegalAccessException e1)

e1.printStackTrace();

catch (UnsupportedLookAndFeelException e1)

e1.printStackTrace();

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JPanel panel = new JPanel();

panel.setOpaque(false);

panel.setBounds(0, 0, 434, 272);

contentPane.add(panel);

panel.setLayout(null);

JButton btn_Login = new JButton("\\u767B\\u5F55");

btn_Login.addMouseListener(new MouseAdapter()

@Override

public void mouseClicked(MouseEvent e)

);

btn_Login.setBounds(88, 195, 70, 23);

panel.add(btn_Login);

JButton btn_cancel = new JButton("\\u53D6\\u6D88");

btn_cancel.addMouseListener(new MouseAdapter()

@Override

public void mouseClicked(MouseEvent e)

dispose();

);

btn_cancel.setBounds(268, 195, 70, 23);

panel.add(btn_cancel);

JLabel lblNewLabel_name = new JLabel("\\u7528\\u6237\\u540D");

lblNewLabel_name.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_name.setOpaque(true);

lblNewLabel_name.setBounds(88, 48, 70, 23);

panel.add(lblNewLabel_name);

JLabel lblNewLabel_passwd = new JLabel("\\u5BC6\\u7801");

lblNewLabel_passwd.setHorizontalAlignment(SwingConstants.CENTER);

lblNewLabel_passwd.setOpaque(true);

lblNewLabel_passwd.setBounds(88, 102, 70, 23);

panel.add(lblNewLabel_passwd);

JCheckBox chckbx_remember = new JCheckBox("\\u8BB0\\u4F4F\\u5BC6\\u7801");

chckbx_remember.setBounds(102, 150, 84, 23);

panel.add(chckbx_remember);

text_LoginName = new JTextField();

text_LoginName.setBounds(182, 48, 156, 23);

panel.add(text_LoginName);

text_LoginName.setColumns(10);

Login_password = new JPasswordField();

Login_password.setBounds(182, 102, 156, 23);

panel.add(Login_password);

JCheckBox chckbx_AutoLogin = new JCheckBox("\\u81EA\\u52A8\\u767B\\u5F55");

chckbx_AutoLogin.setBounds(233, 150, 84, 23);

panel.add(chckbx_AutoLogin);

JLabel Label_background = new JLabel("");

Label_background.setIcon(new ImageIcon("E:\\\\JAVA_workplace\\\\0002-\\u754C\\u9762\\u8BBE\\u8BA1\\\\images\\\\background3.jpg"));

Label_background.setBounds(0, 0, 437, 272);

contentPane.add(Label_background);


参考技术A package lea;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class liangeran extends Frame implements ActionListener

/**
* @param args
*/
TextField stringText;
TextField floatText;

TextArea textArea;

Button inputButton;
Button outputButton;

byte bytes[]=new byte[65560];

public liangeran()
setTitle("实现文本框的存取");
setSize(380,380);
setVisible(true);
setLayout(new FlowLayout());
setResizable(false);

setTextField();
setButton();
setTextArea();

addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);

validate();


private void setTextArea()
textArea=new TextArea(15,45);
add(textArea);


private void setButton()
inputButton=new Button("输入");
outputButton=new Button("输出");

inputButton.addActionListener(this);
outputButton.addActionListener(this);

add(inputButton);
add(outputButton);


private void setTextField()
stringText=new TextField("请输入字符串:",45);
floatText=new TextField("请输入浮点型数字:",45);

add(stringText);
add(floatText);



public static void main(String[] args)
new liangeran();


public void actionPerformed(ActionEvent e)
File myFile=new File("file.dat");
if(e.getSource()==inputButton)
String allText=stringText.getText()+"\n"+floatText.getText();
try

FileOutputStream fileout=new FileOutputStream(myFile);
bytes=allText.getBytes();
fileout.write(bytes, 0, bytes.length);

catch(Exception exception)
System.out.println(exception.getMessage());


if(e.getSource()==outputButton)
try
textArea.append(new String(bytes));


catch(Exception exception)
System.out.println(exception.getMessage());






注:这是从我同学那里的得到的源代码咯!
参考技术B 用netbeanszuo界面就行,那个和vb一样,组件都是现成的,直接用鼠标托到框架里界面就有了,你所要做的只是填参数和写事件处理。 参考技术C

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语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

参考技术D 采用applet,在这里不可能把程序全部贴出来,楼主若需要可联系偶

JAVA编程:编写一个图形界面的Application程序

编写一个图形界面的Application程序.包括一个文本框和一个按钮,在文本框中输入若干字符串,然后将其保存在文件中.
1.越简单越好.
2.要有说明语句.

参考技术A //:SaveContents.java
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class SaveContents extends JFrame implements ActionListener
public static void main(String[] args) new SaveContents().setVisible(true);
private JTextField cont;
private JButton save;
private String filePath="c:/cont.txt";//文件的路径
public SaveContents()
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setSize(205,130);
this.setLocationRelativeTo(null);
cont = new JTextField();
cont.setBounds(10,10,180,20);
add(cont);
save=new JButton("Save");
save.setBounds(110,50,78,22);
add(save);
save.addActionListener(this);

public void actionPerformed(ActionEvent e)
try save(); catch (Exception e1)

//保存内容
private void save() throws Exception
File f = new File(filePath);
FileWriter fw = new FileWriter(f);
fw.write(cont.getText());
fw.flush();
fw.close();

参考技术B import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class MyWindow extends JFrame implements ActionListener
JTextArea text; JButton button;
FileWriter writefile;BufferedWriter out;
MyWindow()
super("缓冲式样流的输出");
Container con=this.getContentPane();
text=new JTextArea(20,30);
text.setBackground(Color.cyan);
button=new JButton("写文件");
button.addActionListener(this);
con.setLayout(new BorderLayout());
con.setSize(400,400);con.setVisible(true);
con.add(text,"Center"); con.add(button,"South");
try
writefile =new FileWriter("line.txt");
out=new BufferedWriter(writefile);
catch(IOException e)

public void actionPerformed(ActionEvent e)
String s;
if(e.getSource()==button)
try
out.write(text.getText(),0,(text.getText()).length());
out.flush();
text.setText(null);

catch(IOException ex)

System.out.println(ex);





public class Example9_3
public static void main(String []args)
MyWindow myWin=new MyWindow();
myWin.setVisible(true);

本回答被提问者采纳

以上是关于怎样用java编写图形界面的Application程序的主要内容,如果未能解决你的问题,请参考以下文章

编写的C语言程序,怎样制作一个UI界面?用啥软件好?

Java编写一个图形界面

java中怎样在界面中显示图片

java图形界面单选按钮的内容怎样写入数据库中?谢谢…………

如何用java编写图形化显示sql查询结果

如何在Intellij IDEA用图形界面打开.java文件