我正在尝试创建一个可以将本地化消息打印到控制台的自定义打印流。我在Windows上执行此操作时遇到问题。这是我正在尝试做的
在这段代码中,我尝试执行上述步骤,但是失败了。奇怪的是,默认的System.out.println调用可以正常工作。但是,我要使用自定义打印流,而不要依赖默认的System.out。
有人可以解释我如何使用自定义打印流将unicode打印到控制台吗?为什么默认的System.out已经可以正确打印东西?
这是我的代码-我编译了它并从命令行运行了它。我预先将系统语言环境设置为zh-CN。
public static void main(String[] args) throws Exception{
Charset defaultCharset = Charset.defaultCharset();
System.out.println(defaultCharset);
// charset is windows-1252
String unicodeMessage =
"\u4e16\u754c\u4f60\u597d\uff01";
System.out.println(unicodeMessage);
// string is printed correctly using System.out (?????)
byte[] sourceBytes = unicodeMessage.getBytes("UTF-8");
String data = new String(sourceBytes , defaultCharset.name());
PrintStream out = new PrintStream(System.out, true, defaultCharset.name());
out.println(data);
// prints gibberish: ??–????????????
}
Run Code Online (Sandbox Code Playgroud) 我有一组平假名字符,我想计算字符的端点/提示数量。
示例:输入图像:
所需的输出图像:
我试过使用凸包
代码:(基于此处的opencv教程)
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<vector<Point> >hull(contours.size());
for (int i = 0; i < contours.size(); i++)
{
convexHull(Mat(contours[i]), hull[i], false);
}
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
if (hierarchy[i][3] == 0) {
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
}
}
Run Code Online (Sandbox Code Playgroud)
然后 conrnerHarris() 但它返回了太多不需要的角落
代码:(基于此处的opencv教程)
int blockSize = 2;
int …Run Code Online (Sandbox Code Playgroud) 我正在为聊天程序编写GUI.我似乎无法scroller水平和垂直messageInput地填充框架并且水平地填充框架.这是它的样子:
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame{
private JPanel panel;
private JEditorPane content;
private JTextField messageInput;
private JScrollPane scroller;
private JMenu options;
private JMenuBar mb;
private JMenuItem item;
public GUI(){
/** This is the frame**/
this.setPreferredSize(new Dimension(380,600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
/** This is where the context shows up **/
content = new JEditorPane();
content.setEditable(false);
/** Scroller that shows up in the …Run Code Online (Sandbox Code Playgroud) 这个问题哪个@NotNull的Java注解,我应该使用?是过时的,有点基于意见。从那时起,Java 8 以及更新的 IDE 出现了。
虽然 Java 8 允许通过集成JSR 308进行类型注释,但它没有附带任何注释。来自JSR 308 解释:Josh Juneau 的 Java 类型注释:
JSR 308,Java 类型注解,已作为 Java SE 8 的一部分合并
。...
编译器检查器可以编写来验证注解的代码,通过在代码不满足某些要求时生成编译器警告来强制执行规则。Java SE 8 不提供默认的类型检查框架,但可以编写自定义注解和处理器来进行类型检查。还有很多类型检查框架可以下载,它们可以作为Java编译器的插件来检查和强制执行已经注解的类型。类型检查框架包括类型注释定义和一个或多个与编译器一起用于注释处理的可插拔模块。
仅考虑提供至少某种@CanBeNulland 的解决方案@CannotBeNull,我找到了有关以下内容的信息(可能是错误的):
org.eclipse.jdt.annotation. 其他 IDE 有各自的包。使用 JSR 308,并且仍然支持 Java 8 之前的注释。javax.annotation。使用休眠的JSR 305,它似乎没有使用 Java 8 的类型注释。尽管它没有集成到 Oracle 的 API 中,但出于某种原因它仍然使用javax域,这意味着它确实如此。org.checkerframework.checker.nullness. …我在JavaFX中使用时间轴对a进行倒计时Label:
timeline.setCycleCount(6);
timeline.play();
Run Code Online (Sandbox Code Playgroud)
我想在时间轴完成后返回一个值:
return true;
Run Code Online (Sandbox Code Playgroud)
但是,似乎该值会立即返回并且时间轴并行运行。如何等待时间轴完成其倒计时,然后返回值而不阻塞时间轴?
编辑:
为了更加清楚,我已经尝试过:
new Thread(() -> {
timeline.play();
}).start();
while(!finished){ // finished is set to true, when the countdown is <=0
}
return true;
Run Code Online (Sandbox Code Playgroud)
(此解决方案不会更新倒计时。)
编辑2:
这是一个最小,完整和可验证的示例:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CountdownTest extends Application {
private Label CountdownLabel;
private int Ctime;
@Override
public void start(Stage primaryStage) {
CountdownLabel=new Label(Ctime+"");
StackPane root = new StackPane();
root.getChildren().add(CountdownLabel);
Scene scene = …Run Code Online (Sandbox Code Playgroud) 这个问题出现在我用 Java 编写的实习职位面试问题之一中。请注意,布尔函数isSame实际上将 2 个参数声明为Integerclass - not int,所以我认为a和b是对象,对吗?
public class ForLoop{
public static boolean isSame(Integer a, Integer b) {
return a == b;
}
public static void main(String []args){
int i = 0;
for (int j=0; j<500; ++j) {
if (isSame(i,j)) {
System.out.println("Same i = "+i);
System.out.println("Same j = "+j);
++i;
continue;
} else {
System.out.println("Different i = "+i);
System.out.println("Different j = "+j);
++i;
break;
}
}
System.out.println("Final i = " …Run Code Online (Sandbox Code Playgroud) 如果我有
public interface Foo {
public void oneMethod();
}
Run Code Online (Sandbox Code Playgroud)
然后我又上了一堂课:
public class TestClass {
SoftReference sr;
public test() {
doSomething(this::lalala);
}
public lalala() {
}
public doSomethingFoo(Foo foo) {
sr = new SoftReference(foo);
//can sr.get() ever return null?
}
}
Run Code Online (Sandbox Code Playgroud)
可以sr.get()返回null吗?
也有兴趣知道在不使用方法引用而是使用匿名内部类或非静态内部类的情况下的行为.
Java开发人员为什么不创建一个类AbstractStringBuilder并将其重命名为StringBuilder?
例如,以下方法AbstractStringBuilder:
public AbstractStringBuilder append(double d) {
FloatingDecimal.appendTo(d,this);
return this;
}
Run Code Online (Sandbox Code Playgroud)
和中的方法StringBuilder:
@Override
public StringBuilder append(double d) {
super.append(d);
return this;
}
Run Code Online (Sandbox Code Playgroud)
我想我们只能保留一种方法AbstractStringBuilder,它将很好地工作。创建无用包装的目的是StringBuilder什么?
我不是很擅长Java GUI,需要寻求帮助.
我打算在我的BorderLayout中心西边添加图像作为我的内容和底部的按钮.
我创建了一个空边框,在我的南面板和我的西面板和中心面板之间做了一些填充.现在我只想在南边界的顶部添加一条线.
如下面的屏幕截图所示,西面板和中心面板之间也有一条线,我该如何移除该线并将线保持在南面板的顶部?
附上我的代码:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
public static void main(String[] args) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JFrame frame = new JFrame();
panel2.add( new JLabel( "WEST <will be adding image here>" ));
panel3.add( new JLabel( "CENTER <contents>"));
panel4.add( new JLabel( "SOUTH <will be …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用鼠标滚轮进行缩放。放大更大效果完美。但是,当我滚动较小时,它不会无限缩小。这意味着,在窗格达到特定大小之后,它不会继续缩小。这是我的方法:
package application;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.SVGPath;
import javafx.scene.transform.Affine;
import javafx.stage.Stage;
public class fab extends Application {
private Stage stage;
private Pane pane;
private BorderPane borderPane;
private Scene scene;
private Affine affine = new Affine();
private ScrollPane scrollPane;
private String coords = "M545.296,92.562c-1.399-1.758-2.823-3.852-4.732-4.777c-8.87-4.309-17.83-8.47-26.933-12.256 c-4.268-1.775-7.49-4.035-8.735-8.65c-0.269-1-0.755-2.084-1.473-2.799c-4.492-4.455-4.533-4.447,0.808-7.854 c0.698-0.445,1.567-0.624,1.73-0.685c-0.412-2.179-0.637-3.901-1.102-5.557c-0.225-0.804-0.755-1.681-1.408-2.179 c-3.211-2.419-7.021-4.264-9.629-7.193c-2.566-2.885-5.186-2.742-8.307-2.448c-1.044,0.098-2.513,0.13-3.146-0.473 c-3.672-3.55-8.731-1.82-13.031-3.321c-3.75-1.31-8.928,1.493-13.477,2.448c-0.633,0.13-1.399,0.293-1.946,0.065 c-7.797-3.284-14.378-1.281-19.689,4.859c-0.262,1.359-0.625,2.905-1.596,3.66c-3.696,2.864-7.854,5.182-11.326,8.274 c-2.424,2.158-4.981,5.01-5.708,7.984c-0.73,2.995-2.171,4.023-4.521,4.77c-2.529,0.8-4.859,1.579-4.949,4.953 c-0.024,0.841-1.562,1.55-1.885,2.497c-0.682,1.995-1.098,4.096-1.473,6.177c-0.208,1.154,0.253,2.554-0.241,3.505 c-1.758,3.415-3.888,6.638-5.699,10.024c-1.588,2.958-4.48,6.328-3.995,9.004c0.588,3.231-0.216,6.389,0.531,9.335 c1.252,4.961-1.191,8.45-3.48,12.24c-1.869,3.088-3.729,6.259-5.002,9.612c-1.122,2.966-3.428,5.826-1.616,9.531 …Run Code Online (Sandbox Code Playgroud)