小编Nik*_*sov的帖子

MouseEvent 在 JScrollPane 中丢失

这是我用来显示我在另一个项目中面临的问题的代码。

如果我使用 JScrollPane 作为 panel2 的包装器,我不会得到任何这样的行。为什么? 我想点击 JscrollPane 并打印如下事件。

java.awt.event.MouseEvent[MOUSE_CLICKED,(800,469),absolute(808,499),button=1,modifiers=Button1,clickCount=1] on javax.swing.JPanel[,0,0,934x612,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@cc0e01,flags=9,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=880,height=630]]
Run Code Online (Sandbox Code Playgroud)

如果现在我改变

panel1.add(pane);
Run Code Online (Sandbox Code Playgroud)

panel1.add(panel2);
Run Code Online (Sandbox Code Playgroud)

然后上面的消息被打印出来。

public class LostMouseEvent {

public static void main(String[] args) {
    new LostMouseEvent();
}

public LostMouseEvent() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());

            JPanel panel1 = new JPanel();
            JPanel panel2 = new JPanel();
            JScrollPane pane = new JScrollPane(panel2);

            panel1.setPreferredSize(new Dimension(880, 630));
            panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
            panel2.setPreferredSize(new Dimension(840, 610));
            panel2.setBorder(BorderFactory.createLineBorder(Color.green));


            panel1.add(pane);
            frame.add(panel1);

            frame.pack();
            frame.setVisible(true);
            frame.setSize(950, 650);

            panel1.addMouseListener(new …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel jframe jscrollpane

4
推荐指数
1
解决办法
1457
查看次数

通用0无法强制转换为java.lang.Short

我班上有两张地图(我是仿制药的新手)

private Map<Integer, Integer> aMap = new ConcurrentHashMap<Integer, Integer>();
private Map<Integer, Short> bMap = new HashMap<Integer, Short>();
Run Code Online (Sandbox Code Playgroud)

如果地图中不存在键我想获得零值.所以我制作了这个包装方法来减少输入containsKey(key)

@SuppressWarnings("unchecked")
private <T extends Number> T getValue (Map<Integer, T> map, Integer key) {
    return (T) ((map.containsKey(key)) ? map.get(key) : 0);
}
Run Code Online (Sandbox Code Playgroud)

我称之为

Integer a = getValue(aMap, 15); //okay in any case
Short b = getValue(bMap, 15); //15 key does not exist
Run Code Online (Sandbox Code Playgroud)

对于第二种情况,它给了我:

ClassCastException: java.lang.Integer cannot be cast to java.lang.Short
Run Code Online (Sandbox Code Playgroud)

所以我可能需要做类似的事情: new Number(0),但是数字是抽象的.

我该如何解决?

编辑:

我的想法是在没有额外ifs的情况下进行算术运算:

Integer a = getValue(aMap, …
Run Code Online (Sandbox Code Playgroud)

java generics casting map bounded-wildcard

4
推荐指数
1
解决办法
3243
查看次数

Findbugs:RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

FindBugs用来从Ant分析Eclipse中的代码.

以下片段给出RV_RETURN_VALUE_IGNORED_BAD_PRACTICE:

RV:方法忽略异常返回值(RV_RETURN_VALUE_IGNORED_BAD_PRACTICE)

此方法返回未检查的值.应检查返回值,因为它可以指示异常或意外的函数执行.例如,如果无法成功删除文件(而不是抛出异常),则File.delete()方法返回false.如果不检查结果,则不会注意方法调用是否通过返回非典型返回值来表示意外行为.

public void export (File file) throws IOException {
    if (!file.exists()) {
        file.createNewFile();
    }

    BufferedWriter bw = null;
    try {
        bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
    ...
Run Code Online (Sandbox Code Playgroud)

实际上我并不关心文件存在与否,该方法应该继续执行.如果发生异常,它将被抛出export()

如何重写此代码段,以便不显示警告/错误,而不在Findbugs配置文件中禁用它?

java io findbugs

4
推荐指数
1
解决办法
7662
查看次数

内部静态枚举作为泛型类型?

我正在学习和试验Java泛型,并提出了一段不能按预期编译的代码.Result无法解决. I代表输入,O输出.

public interface Stats<I, O> {
    O addItem (int index, I item);
}

public class AStats implements Stats<Item, Result> {
    public static enum Result {
        SUCCESS1,
        SUCCESS2,
        ERROR;
    }

    @Override
    public Result addItem (int index, Item item) {
        //valid code
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 有没有比Result在单独文件中声明更优雅的解决方案?

  • 一般来说,有一个返回泛型类型实例的方法是不是很糟糕?

java generics enums static static-members

4
推荐指数
1
解决办法
155
查看次数

如何在jOOQ中配置乐观锁?

jOOQ 手册中至少我不清楚如何正确配置乐观锁定。

 <!-- All table and view columns that are used as "version" fields for
   optimistic locking (A Java regular expression. Use the pipe to separate several expressions).
   See UpdatableRecord.store() and UpdatableRecord.delete() for details -->

<recordVersionFields>REC_VERSION</recordVersionFields>
Run Code Online (Sandbox Code Playgroud)

及以下

recordVersionFields: Relevant methods from super classes are overridden to return the VERSION field
Run Code Online (Sandbox Code Playgroud)

它到底意味着什么?有人可以举个例子吗?

想象一下,如果有这张表,例如:

CREATE TABLE "users" (
   "id"      INTEGER DEFAULT nextval('global_seq') NOT NULL,
   "code"    TEXT                                  NOT NULL,
   "version" INTEGER                               NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

我应该在 pom.xml 中放入什么recordVersionFields

<database>
    <inputSchema>PUBLIC</inputSchema> …
Run Code Online (Sandbox Code Playgroud)

java sql optimistic-locking jooq

4
推荐指数
1
解决办法
2597
查看次数

ByteBuffer与ChannelBuffer

我正在使用在netty顶部实现的一些框架.我正在使用以下两个选项从客户端向服务器发送消息.我想这两个片段应该将相同的字节写入套接字,它在服务器端的行为是不同的.它有什么不同?

选项1:好的

ChannelBuffer buf = ChannelBuffers.buffer(1);
buf.writeByte(0x1c);
e.getChannel().write(buf);
Run Code Online (Sandbox Code Playgroud)

选项2:失败

ByteBuffer buf = ByteBuffer.allocate(1);
buf.put(0x1c);
e.getChannel().write(ChannelBuffers.wrappedBuffer(buf));
Run Code Online (Sandbox Code Playgroud)

java asynchronous bytebuffer netty

3
推荐指数
1
解决办法
2132
查看次数

java游戏室使用log4j登录到单独的日志文件是个好主意?

我正在开发一个基于其他框架,netty和spring的游戏服务器.

我想知道使用log4j为每个游戏室生成一个单独的文件是否是一个好方法.由于许多游戏室服务器必须保持打开很多日志文件.另一方面,只有一个日志文件,这将是一个混乱,在分析问题时需要解析和过滤.

如果我需要知道如何实现它,许多日志文件仍然是一个很好的方法.我已经看过这个问题,配置log4j在运行时登录到自定义文件,但第一个答案不清楚,如何最小化每次尝试写入单独日志文件所需的代码行.我已经通过输入配置文件尝试了第二个答案

log4j.appender.logfile.File=${logfile.name}
Run Code Online (Sandbox Code Playgroud)

但框架有

PropertyConfigurator.configure(System.getProperty("log4j.configuration"));
Run Code Online (Sandbox Code Playgroud)

如果在开始时未指定日志文件,则会失败.

欢迎任何想法和意见!谢谢

UPD:这个链接对我来说似乎也很有用.Log4j记录到单独的文件但仍然:这是一个好主意,以及如何以最少的编码动态切换输出日志文件.

java logging spring log4j netty

3
推荐指数
1
解决办法
451
查看次数

从命令行使用Eclipse ant插件?

我知道这不是一个聪明的问题.

我需要在命令行中使用ant,并且Eclipse中有Ant插件,所以我尝试在Eclipse中指定ANT_HOME设置为该插件的目录.

C:\Users\Nikolay>ant
'ant' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)

我应该从Apache安装单独的Ant 以从命令行启用Ant吗?

java eclipse ant command-line eclipse-plugin

3
推荐指数
1
解决办法
6082
查看次数

在使用Protocol Buffers的项目中使用Proguard有什么特点?

我有一个使用Google Protocol Buffers的项目.一旦我尝试使用ProGuard对其进行模糊处理,似乎protobuf会导致问题.

我打包的所有课程mybuildedclasses.jar.谷歌代码打包成protbuf.jar

mybuildedclasses.jar
protobuf.jar
other external jars
Run Code Online (Sandbox Code Playgroud)

在那之后我试图混淆mybuildedclasses.jar.配置文件与此类似一个.最终所有的罐子都装在另一个胖罐子里面.

我运行程序,一旦尝试发送消息,就会打印出这种异常.

 Caused by: java.lang.RuntimeException: Generated message class "org.mypackage.messages.Control$MessageControlHandCard$Builder" missing method "getCardId".
        at com.google.protobuf.GeneratedMessage.getMethodOrDie(GeneratedMessage.
java:1366)
        at com.google.protobuf.GeneratedMessage.access$1(GeneratedMessage.java:1
361)
        at com.google.protobuf.GeneratedMessage$FieldAccessorTable$SingularField
Accessor.<init>(GeneratedMessage.java:1502)
        at com.google.protobuf.GeneratedMessage$FieldAccessorTable.<init>(Genera
tedMessage.java:1441)
        at org.mypackage.Control$1.assignDescriptors(SourceFile:32
20)
        at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGenerated
FileFrom(Descriptors.java:300)
        at org.evogame.common.messages.Control.<clinit>(SourceFile:3278)
        ... 60 more
Caused by: java.lang.NoSuchMethodException: org.evogame.common.messages.Control$
MessageControlHandCard$Builder.getCardId()
        at java.lang.Class.getMethod(Class.java:1622)
        at com.google.protobuf.GeneratedMessage.getMethodOrDie(GeneratedMessage.
java:1364)
Run Code Online (Sandbox Code Playgroud)

 Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
        at org.mypackage.messages.Control$MessageControlGameRequest.interna
lGetFieldAccessorTable(SourceFile:527)
        at com.google.protobuf.GeneratedMessage.getAllFieldsMutable(GeneratedMes
sage.java:105)
        at com.google.protobuf.GeneratedMessage.getAllFields(GeneratedMessage.ja
va:153)
        at com.google.protobuf.TextFormat$Printer.print(TextFormat.java:229)
        at com.google.protobuf.TextFormat$Printer.access$2(TextFormat.java:226) …
Run Code Online (Sandbox Code Playgroud)

java obfuscation proguard protocol-buffers

3
推荐指数
1
解决办法
2697
查看次数

Play 2框架:scala在视图中迭代java对象的字段

我正在将Java对象传递给视图:

我不知道是否有可能将对象的所有字段都作为集合(类似于Java中的反射)然后在循环中打印它们而不是列出所有字段,如下所示:

@(item: Item)

<li data-item-id="@item.id">
    <h4>@item.name</h4>
    <h3>@item.field1</h3>
    <h3>@item.field2</h3>
    ...
</li>
Run Code Online (Sandbox Code Playgroud)

第二个问题是什么HTML标签是最相关的(而不是li,h4,h3)表示对象?

html java scala playframework playframework-2.0

3
推荐指数
1
解决办法
1603
查看次数