重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
看了一下 你这个题应该是相当于一个写一个带有TCP UDP和排序功能的计算器,用到的知识蛮多的 写出来要花很多时间 期末了要考试了没时间了。。
创新互联专注于衡阳县企业网站建设,成都响应式网站建设,商城开发。衡阳县网站建设公司,为衡阳县等地区提供建站服务。全流程按需设计网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
新建一个窗口,然后实现一个关闭按钮”窗口的功能
import java.awt.*;
import java.awt.event.*;
public class TestWindowEvent {
public static void main (String[] args) {
new Frame88 ("WindowAdapter");
}
}
class Frame88 extends Frame {
Frame88 (String s) {
super(s);
setBounds (300,300,200,70);
setLayout (null);
setVisible (true);
addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible (false);
System.exit(0);
}
} );
}
}
我觉得还是我的好些。。。
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) {
}
}
}
}
可以用下面的代码放在界面所在的构造函数内,便出现美丽的Nimbus界面风格.
至于具体的界面呢.就看你的天分了...
try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Nimbus风格,新出来的外观,JDK 6 Update10 版本开始出现
} catch (Exception e) {
}