Java编写一个图形界面

Posted

tags:

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

要求是左边有默认的例如“IP地址”“端口”“访问网址”等文字类别说明,右边和这些文字同一行是输入框,由用户输入数据。
左边大概8行左右,最后留一行没有文字类别说明,而由用户自行输入类别文字。
最下面正中一个按钮“生成文件”,用户点了之后会在C盘根目录下生成info.dat的文件,文件内容就是“IP=****** (换行)PORT=******”这样的内容。
如果代码过长可以发到我邮箱 milan.thiago.silva.33@gmail.com
好了之后效果如下图。
我现在图形界面应该可以出来,但是不知道怎么写到文件里面去。是用PrintWrite out么?

我觉得还是我的好些。。。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TestSwing extends JFrame
private JLabel label1, label2, label3, label4,label5,label6,label7;

private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;

private JButton button;

private String filepath="c:\\info.dat";
public TestSwing()
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


public void init()
initPanel();
initLabel();
initTextField();
initButton();
initContent();


private void initContent()
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);

panel2.add(button);

panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);

getContentPane().add(panel);


private void initPanel()
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);

panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);

panel2 = new JPanel();
panel2.setLayout(new FlowLayout());

private void initLabel()
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("端口", SwingConstants.LEFT);
label3 = new JLabel("访问地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);


private void initTextField()
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);

private void initButton()
button = new JButton("完成");
button.addMouseListener(new MouseAdapter()
//左键点击
public void mouseClicked(MouseEvent e)
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");


);

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


private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv)
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
catch (Exception e)
finally
if (fos != null)
try
fos.close();
catch (Exception e)



参考技术A import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class InfoFrame extends javax.swing.JFrame

private static final long serialVersionUID = 1L;
private JPanel jPanel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextField jTextField9;
private JPanel jPanel4;
private JButton jButton1;
private JPanel jPanel3;
private JTextField jTextField8;
private JTextField jTextField7;
private JTextField jTextField6;
private JTextField jTextField5;
private JTextField jTextField4;
private JTextField jTextField3;
private JTextField jTextField2;
private JTextField jTextField1;
private JLabel jLabel7;
private JLabel jLabel6;
private JLabel jLabel2;
private JLabel jLabel1;
private JPanel jPanel2;


//Set Look & Feel
try
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch(Exception e)
e.printStackTrace();



/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args)
InfoFrame inst = new InfoFrame();
inst.setVisible(true);


public InfoFrame()
super();
initGUI();


private void initGUI()
try
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setTitle("info");

jPanel1 = new JPanel();
GridLayout jPanel1Layout = new GridLayout(8, 1);
jPanel1Layout.setColumns(1);
jPanel1Layout.setRows(8);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
getContentPane().add(jPanel1, BorderLayout.WEST);
jPanel1.setLayout(jPanel1Layout);

jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("IP地址:");


jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText("端口:");


jLabel3 = new JLabel();
jPanel1.add(jLabel3);
jLabel3.setText("访问地址: ");


jLabel4 = new JLabel();
jPanel1.add(jLabel4);
jLabel4.setText("XXXX:");


jLabel5 = new JLabel();
jPanel1.add(jLabel5);
jLabel5.setText("XXXX:");


jLabel6 = new JLabel();
jPanel1.add(jLabel6);
jLabel6.setText("XXXX:");


jLabel7 = new JLabel();
jPanel1.add(jLabel7);
jLabel7.setText("XXXX:");


jTextField9 = new JTextField();
jTextField9.setPreferredSize(new Dimension(50,21));
jPanel1.add(jTextField9);
jTextField9.setText("");



jPanel2 = new JPanel();
GridLayout jPanel2Layout = new GridLayout(8, 1);
jPanel2Layout.setColumns(1);
jPanel2Layout.setRows(8);
jPanel2Layout.setHgap(5);
jPanel2Layout.setVgap(5);
getContentPane().add(jPanel2, BorderLayout.CENTER);
jPanel2.setLayout(jPanel2Layout);

jTextField1 = new JTextField();
jPanel2.add(jTextField1);


jTextField2 = new JTextField();
jPanel2.add(jTextField2);


jTextField3 = new JTextField();
jPanel2.add(jTextField3);


jTextField4 = new JTextField();
jPanel2.add(jTextField4);


jTextField5 = new JTextField();
jPanel2.add(jTextField5);


jTextField6 = new JTextField();
jPanel2.add(jTextField6);


jTextField7 = new JTextField();
jPanel2.add(jTextField7);


jTextField8 = new JTextField();
jPanel2.add(jTextField8);



jPanel3 = new JPanel();
FlowLayout jPanel3Layout = new FlowLayout();
getContentPane().add(jPanel3, BorderLayout.SOUTH);
jPanel3.setLayout(jPanel3Layout);

jButton1 = new JButton();
jPanel3.add(jButton1);
jButton1.setText("完成");
jButton1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent evt)
jButton1ActionPerformed(evt);

);



jPanel4 = new JPanel();
getContentPane().add(jPanel4, BorderLayout.NORTH);

pack();
setSize(400, 300);
setLocationRelativeTo(null);
catch (Exception e)
e.printStackTrace();



private void jButton1ActionPerformed(ActionEvent evt)
StringBuffer sb=new StringBuffer();
sb.append("IP地址="+jTextField1.getText()+"\n");
sb.append("端口号="+jTextField2.getText()+"\n");
sb.append("访问地址="+jTextField3.getText()+"\n");
sb.append("XXXX="+jTextField4.getText()+"\n");
sb.append("XXXX="+jTextField5.getText()+"\n");
sb.append("XXXX="+jTextField6.getText()+"\n");
sb.append("XXXX="+jTextField7.getText()+"\n");
sb.append(jTextField9.getText()+"="+jTextField8.getText());
try //写入文件
FileWriter fw=new FileWriter("C:/info.dat");
fw.write(sb.toString());
fw.close();
JOptionPane.showMessageDialog(null, "保存完成!");
catch (IOException e)
e.printStackTrace();



参考技术B rcp(SWT/JFace)开发的要不要,同样是java的 参考技术C 已发送,请查收

怎样用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编写一个图形界面的主要内容,如果未能解决你的问题,请参考以下文章

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

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

CentOS设置防火墙之后,远程执行程序无图形界面

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

凯撒密码java编程实现图形界面化代码

用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算