我有一个案例,需要从 TOML 文件中提取一些数据。它工作得很好,但绝大多数代码都是匹配Results 或Options。
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::exit;
extern crate toml;
fn main() {
// Get the path of the config file
let homedir = match env::home_dir() {
Some(path) => path,
None => {
println!("Error: Could not find home directory");
exit(1);
}
};
let mut config_path = PathBuf::from(homedir);
config_path.push(".relay");
config_path.set_extension("toml");
// Open the config file
let mut file = match File::open(&config_path) {
Ok(file) => file,
Err(why) => { …Run Code Online (Sandbox Code Playgroud) 前言:这是我做的第一个真正的挥杆程序。
我有一个摇摆程序,其中一个 JButton 应该退出程序。该按钮触发 this.dispose();。当我单击此 JButton 时,它确实使窗口完全消失,但查看调试器,程序本身仍在运行。
我的主要方法只包括:
public static void main (String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new StartupGui().setVisible(true);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的退出按钮看起来像操作按钮:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
this.dispose();
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个退出按钮:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
dispose();
}
});
}
Run Code Online (Sandbox Code Playgroud)
按下退出按钮后查看调试器,我看到以下内容(仅以下内容):
Daemon Thread [AWT-XAWT] (running)
Thread [AWT-Shutdown] (running)
Thread [AWT-EventQueue-0] (running)
Thread [DestroyJavaVM] (running)
Run Code Online (Sandbox Code Playgroud)
任何人都可以指出我为什么在此之后程序没有关闭的正确方向?我已经做了一些谷歌搜索,但到目前为止还没有到任何地方。如果您需要更多信息,请告诉我
谢谢 :)
检查传递给方法的对象的最佳方法是扩展给定的类?
目前我有一个方法需要发送ByteBuffer数据和我编写的'播放器'类,并将数据排队到IO服务器上发送给客户端:
public void send(ButeBuffer toSend, Player player)
{
// prep the byte buffer for sending, and queue it on the IO server
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是让传入的玩家对象成为扩展玩家类的任何对象.我做了一些搜索,发现了这样的事情:
public void send(ByteBuffer toSend, Player<? extends Player> player)
{
// prep the byte buffer for sending, and queue it on the IO server
}
Run Code Online (Sandbox Code Playgroud)
但这给了我编译错误,我不明白到底发生了什么.这是正确的方法吗?如果是这样,任何人都可以解释这段代码具体做什么以及它为什么不起作用,或者将我链接到一篇更详细解释这一点的文章.
或者,我想我可以设置这样的东西:
public void send(ByteBuffer toSend, Object player)
{
// Check that the player extends Player, using instanceof or something
// along those lines
// Prep the ByteBuffer, and queue the …Run Code Online (Sandbox Code Playgroud) 尝试从MySql数据库中执行select语句.当我直接插入变量时,以下工作:
MySqlCommand playerSearch = conn.CreateCommand();
playerSearch.CommandText = @"select username from players where username like '%" + username + "%'";
playerSearch.Prepare();
// Execute the command, get the restuls, etc.
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用参数添加的首选方式,例如:
MySqlCommand playerSearch = conn.CreateCommand();
playerSearch.CommandText = @"select username from players where username like @username";
playerSearch.Prepare();
playerSearch.Parameters.AddWtihValue("@username", "'%" + username + "%'");
// Execute the command, get the restuls, etc.
Run Code Online (Sandbox Code Playgroud)
我从查询中得不到任何结果.到目前为止,我还没弄清楚为什么这不起作用.有什么建议?