我在Java中有一个SNMP陷阱应用程序,旨在侦听SNMP代理并在JFrame窗口中的JTextArea上打印接收到的SNMP消息.
下面的第一部分是我的源代码,显示了TrapReceiver类的内容.在这个类中,listen方法是充分利用工作的地方.该类在JFrame类中进行实例化,我打算在上面提到的JTeaxtArea上显示消息.我将JTextArea对象的引用,SNMP代理URL和端口发送到TrapReceiver类的构造函数中,然后调用TrapReceiver对象的run方法以在JFrame实例以外的单独线程中开始执行.下面的第二部分展示了我如何在JFrame实例中实例化TrapReeceiver类.
当我运行应用程序时,我注意到JFrame实例(即GUI)正在冻结,并且没有在JFrame实例中提到的JTeaxtArea上打印消息,该实例实例化下面第一部分中所示的类TrapReeceiver.
我的问题是为什么JFrame实例(即GUI)是冻结的,尽管TRapReceiver本身是作为一个单独的线程执行的?此外,我想知道这个冻结问题的可能解决方案是什么.提前致谢.
PS:我已经验证了TrapReceiver工作正常并且可以在没有GUI的情况下作为独立应用程序运行时将消息打印到标准输出,但是由于某些可能的线程同步问题,这个GUI正在以某种方式冻结.我尝试运行TrapReceiver而不用线程,即使在这种情况下,GUI仍然处于冻结状态.
第一部分
package com.[Intenionally removed].snmp;
import java.io.IOException;
import javax.swing.JTextArea;
import org.snmp4j.*;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.security.Priv3DES;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.AbstractTransportMapping;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;
public class TrapReceiver implements CommandResponder, Runnable {
private String targetSnmpAgentURL;
private int targetSnmpAgentPort;
private JTextArea outConsole;
public TrapReceiver() {
}
public TrapReceiver(JTextArea outConsole) {
this.outConsole = outConsole;
}
public TrapReceiver(JTextArea outConsole, String targetSnmpAgentURL, int targetSnmpAgentPort) {
this.targetSnmpAgentURL = targetSnmpAgentURL; …Run Code Online (Sandbox Code Playgroud) 我想要获得我的系统的运行平衡.为此,我从列AMOUNT获取jtable中所有数字的总和,并将总和减去txtLoanAmount中的值.这是我的代码片段:
String LoanAmount = txtLoanAmount.getText();
float f = Float.valueOf(LoanAmount.trim()).floatValue();
float balance = 0;
float sum = 0;
for(int i=0;i<=tableLedger.getRowCount()-1;i++) {
sum = sum + Float.parseFloat(tableLedger.getModel().getValueAt(i, 2).toString());
}
balance = f - sum;
System.out.println(balance);
Run Code Online (Sandbox Code Playgroud)
现在我收到错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "20,475.00"
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?任何帮助将不胜感激.谢谢
我想取消正在运行的任务并将其替换为同一线程上的新任务。
在下面的代码中,我使用了一个单线程执行器来启动一个新任务并捕获一个Future代表它的。然后我用Future取消了任务。但是,任务并没有取消。
示例代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
class Task implements Runnable {
public void run() {
System.out.println("New task started");
int i = 0; while (true) i++;
}
}
public class TaskLauncher extends JFrame {
private JButton button = new JButton("Start new task");
private ExecutorService executor = Executors.newSingleThreadExecutor();
private Future<?> future;
public TaskLauncher() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) …Run Code Online (Sandbox Code Playgroud) 我已经创建了在eclipse中构建的项目的可执行jar文件.但是当我执行该文件时,它不会在我在项目中添加的系统托盘上显示图标.我正在使用以下简单代码.
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/ChatIcon1.jpeg");
PopupMenu Popup = new PopupMenu();
MenuItem exit = new MenuItem("Exit");
Popup.add(exit);
final TrayIcon trayIcon = new TrayIcon(image,"OfficeCommunicator",Popup);
trayIcon.setImageAutoSize(true);
Run Code Online (Sandbox Code Playgroud) 我有一个jtable.(tablesummary).其中一个专栏是EXPIRY.我想突出显示当前日期已过期的客户的行.
我已经得到了逻辑,但我不能让行变红或任何其他颜色.这是我的代码:
int count = (tableSummary.getRowCount());
NumberFormat formatter = new DecimalFormat("###,###");
String no = formatter.format(count);
txtNo.setText(no);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime()).toString();
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
String nnn= tableSummary.getModel().getValueAt(i, 6).toString();
System.out.println(nnn);
int res = nnn.compareTo(expDateString);
if(res>=0){
System.out.println("if ni " + (res>=0));
}
else{
System.out.println("else ni" + (res>=0));
rowrenderer.setBackground(Color.RED);
}
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个?因为它是我界面的主要亮点之一.提前致谢 !!:)
我有一个jtable有一个编辑按钮,当我选择一行并单击编辑按钮和编辑字段,然后单击保存按钮,该行不更新,我必须刷新我的表以更改该行!
我的代码:
if (e.getSource() == editButton) {
selectedRow = uTable.getSelectedRow();
if (selectedRow >= 0) {
editUser(selectedRow);
} else {
JOptionPane.showMessageDialog(null, "Select a row");
}
}
public void editUser(int row) {
UserInformation userInf = userModel.getSelectedMember(row);
NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf, row);
}
...
Run Code Online (Sandbox Code Playgroud)
我的NewUserFrame_Edit类:
public class NewUserFrame_Edit extends javax.swing.JFrame {
private AllUser userModel;
private int selectedrow;
private String gender;
public NewUserFrame_Edit(AllUser userModel,UserInformation userinf, int row) {
...
this.userModel = userModel;
jTextField1.setText(userinf.getFname().toString().trim());
jTextField2.setText(userinf.getLname().toString().trim());
if (userinf.getGender().equals("Male")) {
jRadioButton1.setSelected(true);
} else {
jRadioButton2.setSelected(true);
} …Run Code Online (Sandbox Code Playgroud) 有没有办法创建带有垂直标题的表格视图?我在 javafx 中没有看到任何选项可以执行此操作。
我正在攀登java学习曲线,这是我第一次需要一个JTable.我想做的是显示一个空表,所有单元格除了列标题外都是空的.然后,作为用户操作的结果,表中填充了字符串,整数和浮点的混合.
我在Web上找到的所有示例都创建了在实例化时填充的表.是否有任何简单的方法来推迟填充表,但在启动时显示它?
在此先感谢您的帮助.
我有一个JTextArea,只需要接受数字.这是我的代码:
DocumentFilter onlyNumberFilter = new AxisJTextFilter();
final JTextArea areaTextoXMin = new JTextArea(String.valueOf(xMin));
((AbstractDocument)areaTextoXMin.getDocument()).setDocumentFilter(onlyNumberFilter);
Run Code Online (Sandbox Code Playgroud)
适用于正数,但不适用于负数.我该如何解决这个问题?
编辑:对不起,AxisJTextFilter是在互联网上找到的,我忘记了.它的代码是:
import javax.swing.text.*;
import java.util.regex.*;
public class AxisJTextFilter extends DocumentFilter
{
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
{
StringBuilder sb = new StringBuilder();
sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.insert(offset, text);
if(!containsOnlyNumbers(sb.toString())) return;
fb.insertString(offset, text, attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
{
StringBuilder sb = new StringBuilder();
sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.replace(offset, offset + length, text); …Run Code Online (Sandbox Code Playgroud)