通过GUI制作一个简单的消息对话框互发消息
Posted 妤轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过GUI制作一个简单的消息对话框互发消息相关的知识,希望对你有一定的参考价值。
public class LTS extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; /** * Launch the application. */ private static PrintWriter pw=null; private static BufferedReader br=null; private static Socket s; static Scanner scanner=new Scanner(System.in); /** * @param args */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { LTS frame = new LTS(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public LTS() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 571, 396); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); textField = new JTextField(); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setColumns(10); JButton btnNewButton = new JButton("\u53D1\u9001"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Client端请输入:"); String str =textField_1.getText() ; pw.println(str+" "+"黄凯"+" "+df.format(new Date())); pw.flush(); String string=br.readLine(); textField.setText(string); if(string.equals("exit")){ } }catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // try { // br.close(); // pw.close(); // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } } }); JButton button = new JButton("\u53D6\u6D88"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); JButton btnIp = new JButton("IP"); btnIp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { Socket s=new Socket("175.3.15.9",8532); pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream())); br=new BufferedReader(new InputStreamReader(s.getInputStream())); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup( gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(textField, GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 545, GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(btnIp, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, 356, Short.MAX_VALUE) .addComponent(button) .addGap(18) .addComponent(btnNewButton) .addContainerGap()) ); gl_contentPane.setVerticalGroup( gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup() .addComponent(textField, GroupLayout.PREFERRED_SIZE, 218, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(btnIp, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE) .addComponent(button, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); contentPane.setLayout(gl_contentPane); } }
以上是关于通过GUI制作一个简单的消息对话框互发消息的主要内容,如果未能解决你的问题,请参考以下文章