我想让我的电脑和服务器兼容.这是我的代码
import java.net.*;
class tester {
static int pos=0;
static byte buffer[]=new byte[100];
static void Client() throws Exception {
InetAddress address=InetAddress.getLocalHost();
DatagramSocket ds=new DatagramSocket(3000,address);
while(true) {
int c=System.in.read();
buffer[pos++]=(byte)c;
if((char)c=='\n')
break;
}
ds.send(new DatagramPacket(buffer,pos,address,3000));
Server();
}
static void Server() throws Exception {
InetAddress address=InetAddress.getLocalHost();
DatagramSocket ds=new DatagramSocket(3001,address);
DatagramPacket dp=new DatagramPacket(buffer,buffer.length);
ds.receive(dp);
System.out.print(new String(dp.getData(),0,dp.getLength()));
}
public static void main(String args[])throws Exception {
if(args.length==1) {
Client();
}
}
Run Code Online (Sandbox Code Playgroud)
}
在这里,我试图使我的计算机既服务器又客户端.
我在cmd上运行这个程序,
java tester hello但程序一直在等待.我应该怎么做以接收键入的消息.
*如果代码中有任何修改,请提出建议.请注意,目的是使我的电脑和服务器兼容.
这是片段:
protected void paintComponent(final Graphics g) {
Runnable r=new Runnable() {
@Override
public void run() {
while(true) {
super.paintComponent(g); // <----- line of error
g.setColor(Color.red);
g.drawOval(x,y,width,height);
g.fillOval(x,y,width,height);
x++;
y++;
width++;
height++;
if(width==20)
break;
try {
Thread.sleep(100);
} catch(Exception exc) {
System.out.println(exc);
}
}
}
};
Thread moveIt=new Thread(r);
moveIt.start();
}
Run Code Online (Sandbox Code Playgroud)
编译完整代码时会产生以下错误:
d:\UnderTest>javac mainClass.java
mainClass.java:18: cannot find symbol
super.paintComponent(g);
^
symbol: method paintComponent(Graphics)
location: class Object
1 error
Run Code Online (Sandbox Code Playgroud)
如果这是我的完整代码:
import java.awt.*;
import javax.swing.*;
import java.lang.Thread;
class movingObjects extends JPanel { …Run Code Online (Sandbox Code Playgroud) 如何从目录中删除文件c++?
我知道这个函数int remove ( const char * filename )删除了在参数中指定了文件名的文件.但它只接受char*.c ++中还有其他函数可以接受string它的参数吗?
这是获取电子邮件sender和subject电子邮件的代码.使用此代码,我看到正确的主题显示,但我看到不同格式的发件人的地址.
Properties props = new Properties();
props.put("mail.imap.host" , "imap.gmail.com" );
props.put("mail.imap.user" , "username");
// User SSL
props.put("mail.imap.socketFactory" , 993);
props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.imap.port" , 993 );
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username" , "password");
}
});
try {
Store store = session.getStore("imap");
store.connect("imap.gmail.com" , "username" , "password");
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.READ_ONLY);
Message msgs[] = fldr.getMessages();
for(int i = 0 ; i < msgs.length ; …Run Code Online (Sandbox Code Playgroud) 代码
import javax.sound.sampled.*;
import java.io.*;
public class Tester {
static Thread th;
public static void main(String[] args) {
startNewThread();
while( th.isAlive() == true) {
System.out.println("sound thread is working");
}
}
public static void startNewThread() {
Runnable r = new Runnable() {
public void run() {
startPlaying();
}
};
th =new Thread(r);
th.start();
}
public static void startPlaying() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("d:/UnderTest/wavtester.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.loop(-1); // keep playing the sound
} catch(Exception exc) { …Run Code Online (Sandbox Code Playgroud) 这是产生错误的片段:
')' expected
';' expected
not a statement
cannot find symbol
symbol : variable ActionEvent
Run Code Online (Sandbox Code Playgroud)
片段:
private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooseToAdd = new JFileChooser();
int option = chooseToAdd.showOpenDialog(this);
if( option == JFileChooser.APPROVE_OPTION ) {
nameOfAudioFile = chooseToAdd.getSelectedFile().getAbsolutePath();
//clonejTree1ValueChanged( TreeSelectionEvent evt );
tester(java.awt.event.ActionEvent evt);
}
}
private void tester(java.awt.event.ActionEvent evt) {
System.out.println("tester");
}
Run Code Online (Sandbox Code Playgroud)
有语法错误吗?
我有一个JDialog名为的班级Preferences.这个类创建一个构造函数,如:
class Preferences extends javax.swing.JDialog {
Preferences(java.awt.Frame parent,modal) {
super(parent,modal);
//......
}
}
Run Code Online (Sandbox Code Playgroud)
在我的程序中,我希望在单击JFrame表单中的按钮时打开此首选项对话框.在按钮上注册动作监听器后,我将代码写入:
Frame fr = new Frame();
Preferences p = new Preferences(fr,false);
fr.add(p);
fr.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到以下异常(当我单击按钮时):
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: adding a window to a container
Run Code Online (Sandbox Code Playgroud)
这是什么意思,我该如何解决?
到现在为止,每当我查询数据库时,我都会打开一个与数据库的新连接.我如何实现该属性,一旦我打开连接,我可以重用它?
完成后,请告诉我是否可以泄漏资源.
单击netbeans项目时,Deploy to Google App Engine这些是Google App Deployment控制台上显示的消息:
May 28, 2012 4:22:06 PM com.google.apphosting.utils.config.AppEngineWebXmlReader readAppEngineWebXml
INFO: Successfully processed /home/non-admin/NetBeansProjects/Guestbook/build/web/WEB-INF/appengine-web.xml
May 28, 2012 4:22:06 PM com.google.apphosting.utils.config.AbstractConfigXmlReader readConfigXml
INFO: Successfully processed /home/non-admin/NetBeansProjects/Guestbook/build/web/WEB-INF/web.xml
Beginning server interaction for ...
Password for suhailgupta03@gmail.com:
com.google.appengine.tools.admin.HttpIoException: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=&version=1&
400 Bad Request
app_id GET query string parameter must be supplied.
Unable to update app: Error posting to URL: https://appengine.google.com/api/appversion/getresourcelimits?app_id=&version=1&
400 Bad Request
app_id GET query string parameter must be supplied.
Please see the logs …Run Code Online (Sandbox Code Playgroud) 以下片段尝试将某些目录中存在的目录和文件的名称写入文本文件.每个名称应写入单独的行.而不是在同一行上打印每个名称.为什么会这样?
try {
File listFile = new File("E:" + System.getProperty("file.separator") + "Shiv Kumar Sharma Torrent"+ System.getProperty("file.separator") +"list.txt");
FileWriter writer = new FileWriter(listFile,true);
Iterator iterator = directoryList.iterator();
while(iterator.hasNext()) {
writer.write((String)iterator.next());
writer.write("\n"); // Did this so each name is on a new line
}
writer.close();
}catch(Exception exc) {
exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
输出:

我在哪里弄错了?