Java编写一个图形界面

发布网友 发布时间:2022-04-22 22:52

我来回答

4个回答

热心网友 时间:2023-06-22 13:49

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

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) {
}
}
}
}

热心网友 时间:2023-06-22 13:49

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();
}
}

}

热心网友 时间:2023-06-22 13:50

rcp(SWT/JFace)开发的要不要,同样是java的

热心网友 时间:2023-06-22 13:50

已发送,请查收

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com