程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了显示从一类到 GUI 类的文本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决显示从一类到 GUI 类的文本?

开发过程中遇到显示从一类到 GUI 类的文本的问题如何解决?下面主要结合日常开发的经验,给出你关于显示从一类到 GUI 类的文本的解决方法建议,希望对你解决显示从一类到 GUI 类的文本有所启发或帮助;

我是 Java 新手,我有这段代码,我用它来使用套接字连接到服务器

客户端类代码

@H_675_6@public class ClIEnt { Socket mysocket; BufferedReader inFromServer; DataOutputStream outToServer; public Socket connect(String nameServer,int portServer) { try { mysocket=new Socket(nameServer,portServer); outToServer=new DataOutputStream(mysocket.getoutputStream()); inFromServer=new BufferedReader(new inputStreamReader(mysocket.geTinputStream())); } catch(UnkNownHostException E) { System.err.println("Host error"); // display Error } catch(Exception E) { System.out.print(e.getmessage()); System.out.print("Connection Error!"); // display Error System.exit(1); } return mysocket; }

界面代码

@H_675_6@package ClIEnt; import java.awt.EventQueue; import javax.swing.text.badLOCATIOnException; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.Styleddocument; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.color; import java.awt.Systemcolor; import javax.swing.JTextFIEld; import javax.swing.JLabel; import javax.swing.SwingConstants; import java.awt.Font; import javax.swing.Jbutton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextPane; public class WindowClIEnt { private JFrame frmSocketConnection; private JTextFIEld TxtIP; private JTextFIEld TxtPort; private JTextFIEld TxTinput; ClIEnt clIEnt = new ClIEnt(); public static Boolean isNumeric(String strNum) { if (strNum == null) { return false; } try { double d = Double.parseDouble(strNum); } catch (numberFormatException nfE) { return false; } return true; } public static voID main(String[] args) { EventQueue.invokelater(new Runnable() { public voID run() { try { WindowClIEnt window = new WindowClIEnt(); window.frmSocketConnection.setVisible(true); } catch (Exception E) { e.printstacktrace(); } } }); } /** * Create the application. */ public WindowClIEnt() { initialize(); } /** * Initialize the contents of the frame. */ private voID initialize() { frmSocketConnection = new JFrame(); frmSocketConnection.settitle("Socket Connection"); frmSocketConnection.getContentPane().setBACkground(color.WHITE); frmSocketConnection.setBounds(100,100,983,633); frmSocketConnection.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frmSocketConnection.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBACkground(color.WHITE); panel.setBounds(725,244,596); frmSocketConnection.getContentPane().add(panel); panel.setLayout(null); TxtIP = new JTextFIEld(); TxtIP.setFont(new Font("Tahoma",Font.PLAIN,16)); TxtIP.setBounds(10,89,224,34); panel.add(TxtIp); TxtIP.setcolumns(10); JLabel lbliP = new JLabel("IP Address"); lbliP.setFont(new Font("Tahoma",Font.bolD,18)); lbliP.setHorizontalAlignment(SwingConstants.CENTER); lbliP.setBounds(10,45,34); panel.add(lblip); TxtPort = new JTextFIEld(); TxtPort.setFont(new Font("Tahoma",16)); TxtPort.setcolumns(10); TxtPort.setBounds(10,216,34); panel.add(TxtPort); JLabel lblPort = new JLabel("IP Port"); lblPort.setHorizontalAlignment(SwingConstants.CENTER); lblPort.setFont(new Font("Tahoma",18)); lblPort.setBounds(10,172,34); panel.add(lblPort); Jbutton btnConnect = new Jbutton("Connect"); btnConnect.setFocusPainted(false); btnConnect.setBACkground(Systemcolor.controlHighlight); btnConnect.setFont(new Font("Tahoma",18)); btnConnect.setBounds(34,317,176,48); panel.add(btnConnect); Jbutton btndisconnect = new Jbutton("disconnect"); btndisconnect.setFocusPainted(false); btndisconnect.setEnabled(false); btndisconnect.setFont(new Font("Tahoma",18)); btndisconnect.setBACkground(Systemcolor.controlHighlight); btndisconnect.setBounds(34,396,48); panel.add(btndisconnect); TxTinput = new JTextFIEld(); TxTinput.setFont(new Font("Tahoma",16)); TxTinput.setForeground(color.WHITE); TxTinput.setBACkground(color.DARK_GRAY); TxTinput.setBounds(10,547,617,39); frmSocketConnection.getContentPane().add(TxTinput); TxTinput.setcolumns(10); Jbutton btnEnter = new Jbutton("Enter"); btnEnter.setFocusPainted(false); btnEnter.setBACkground(Systemcolor.controlHighlight); btnEnter.setFont(new Font("Tahoma",18)); btnEnter.setBounds(623,92,39); frmSocketConnection.getContentPane().add(btnEnter); JTextPane TxtConsole = new JTextPane(); Styleddocument doc = TxtConsole.getStyleddocument(); Style style = TxtConsole.addStyle("Stile",null); TxtConsole.setFont(new Font("Monospaced",16)); TxtConsole.setForeground(color.WHITE); TxtConsole.setBACkground(color.DARK_GRAY); TxtConsole.setBounds(10,10,705,525); frmSocketConnection.getContentPane().add(TxtConsolE); btnConnect.addActionListener(new ActionListener() { public voID actionPerformed(ActionEvent E) { btnConnect.setEnabled(false); btndisconnect.setEnabled(true); if (TxtIP.getText().length() == 0 || TxtPort.getText().length() == 0) { StyleConstants.setForeground(style,color.red); try { doc.insertString(doc.getLength(),"Error. One fIEld is empty! \n",stylE); btnConnect.setEnabled(true); btndisconnect.setEnabled(false); } catch (BadLOCATIOnException e1) { e1.printstacktrace(); } } else if (!isNumeric(TxtPort.getText())) { StyleConstants.setForeground(style,"The port fIEld has to be a number! \n",stylE); btnConnect.setEnabled(true); btndisconnect.setEnabled(false); } catch (BadLOCATIOnException e1) { e1.printstacktrace(); } } else { StyleConstants.setForeground(style,color.whitE); try { doc.insertString(doc.getLength(),"Connection... \n",stylE); } catch (BadLOCATIOnException e1) { e1.printstacktrace(); } } } }); TxTinput.addActionListener(new ActionListener() { @OverrIDe public voID actionPerformed(ActionEvent E) { TxTinput.setText(""); } }); btnEnter.addActionListener(new ActionListener() { public voID actionPerformed(ActionEvent E) { } }); btndisconnect.addActionListener(new ActionListener() { public voID actionPerformed(ActionEvent E) { TxtIP.setText(""); TxtPort.setText(""); } }); } }

我在另一个班级有一个 GUI。在该课程中,我有一个 JTextPane,我想将其用作显示任何内容的“控制台”

This is an image of the GUI that i have at the moment

我想将来自 ClIEnt 类的错误字符串显示到 JTextPane 中,它在另一个类中,但我真的不知道该怎么做

解决方法

有很多方法可以在数据之间进行通信并确保通过它。 您可以选择从 Client 类中抛出错误/异常并确保捕获它并将该错误放入 JTextPane

我可以简单地尝试一下:

@H_675_6@public static class Client { Socket mysocket; BufferedReader inFromServer; DataOutputStream outToServer; public Socket connect(String nameServer,int portServer) throws Exception { try { mysocket=new Socket(nameServer,portServer); outToServer=new DataOutputStream(mysocket.getOutputStream()); inFromServer=new BufferedReader(new InputStreamReader(mysocket.geTinputStream())); } catch(UnknownHostException E) { throw new Exception("Unknown Host Error: " + e.getmessage()); } catch(Exception E) { System.out.print("Connection Error!"); // Display Error throw new Exception("Connection Error! " + e.getmessage()); } return mysocket; } }

一个非常简单的版本是:

@H_675_6@ Client c = new Client(); String errormessage = ""; try { c.connect("localhost",8010); } catch (Exception E) { errormessage = e.getmessage(); } JTextPane pane = new JTextPane(); SimpleAttributeSet attributeSet = new SimpleAttributeSet(); StyleConstants.setBold(attributeSet,truE); // Set the attributes before adding text pane.setCharacterAttributes(attributeSet,truE); // A very simple version of that errormessage for illustrations only pane.setText(errormessage == null ? "Welcome" : errormessagE);

就您而言,如果您通过按钮操作进行连接,则:

@H_675_6@ btnConnect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent E) { btnConnect.setEnabled(false); btnDisconnect.setEnabled(true); // if-else // ... else { StyleConstants.setForeground(style,Color.whitE); try { Client c = new Client(); String errormessage = ""; try { c.connect("localhost",8010); doc.insertString(doc.getLength(),"ConnecTing...",stylE); } catch (Exception E) { // SetTing the error message doc.insertString(doc.getLength(),e.getmessage(),stylE); } } catch (BadLOCATIOnException e1) { e1.printStackTrace(); } } } });

大佬总结

以上是大佬教程为你收集整理的显示从一类到 GUI 类的文本全部内容,希望文章能够帮你解决显示从一类到 GUI 类的文本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: