用于拖放的JLabel鼠标事件

我希望通过覆盖它上面的鼠标事件来启用JLabel上的拖放function,但是当我在mousePressed事件中定义拖放时,mouseReleased不会对该JLabel生效。 难道我做错了什么 ? Thumbnails[I_Loop].setText(“1”); Thumbnails[I_Loop].setTransferHandler(new TransferHandler(“text”)); Thumbnails[I_Loop].addMouseListener( new MouseAdapter() { public void mouseReleased(MouseEvent me) { System.out.println(“here mouse released”); } public void mousePressed(MouseEvent me) { System.out.println(“here mouse pressed”); JComponent comp = (JComponent) me.getSource(); TransferHandler handler = comp.getTransferHandler(); handler.exportAsDrag(comp, me, TransferHandler.COPY); }); *缩略图是JLabel的数组 在运行程序时,拖放工作但是“此处鼠标已释放”语句不会被打印。 但是,当我从mousePressed()方法中删除负责DND的代码时,将打印“此处鼠标已释放”。 这段代码有什么问题?

JLabel – 将更长的文本显示为多行?

所以说我想要在JLabel显示一条非常长的行。 我该怎么做? 目前,更长的线路出现了: 我必须调整窗口大小以查看完整的文本。 当文本几乎达到我的JFrame的宽度时,我怎样才能使它有换行符? 我不确定这里是否需要任何代码才能回答这个问题,但仍然: 我的框架属性: frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(450, 400)); frame.setLocation(new Point(400, 300)); frame.setLayout(new BorderLayout()); 我要修改的标签: question = new JLabel(“Question:”); question.setFont(new Font(“Serif”, Font.BOLD, 15)); question.setHorizontalAlignment(JLabel.CENTER); 编辑:更多细节: 我正在从文件中读取行,然后显示它们。 线条的大小不固定,所以我不知道放在哪里。 编辑2: 我最终使用了JTextArea 。 private JTextArea textAreaProperties(JTextArea textArea) { textArea.setEditable(false); textArea.setCursor(null); textArea.setOpaque(false); textArea.setFocusable(false); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); return textArea; }

连接超时。 为什么?

运行此代码时出现exception。 为什么? 线程“main”中的exceptionjava.net.ConnectException:连接超时:connect import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class MainClass { public static void main(String[] args) throws Exception { System.setProperty(“java.protocol.handler.pkgs”, “com.sun.net.ssl.internal.www.protocol”); URL url = new URL(“https://www.verisign.com/”); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } } 例外: Exception in thread “main” java.net.ConnectException: Connection […]

正则表达式:如何逃避反斜杠和特殊字符?

有没有办法逃避(或保护)正则表达式中的特殊字符? 我想要做的是创建一个简单的正则表达式测试器: import java.util.regex.*; class TestRegex { public static void main( String … args ) { System.out.printf(“%s ~= %s ? %s %n” , args[0], args[1], Pattern.matches( args[0], args[1] ) ); } } 在将模式插入程序之前测试我的模式非常有用: $java TestRegex “\d” 1 \d ~= 1 ? true $java TestRegex “\d” 12 \d ~= 12 ? false $java TestRegex “\d+” 12 […]

三元运算符 – JAVA

有可能改变这个: if(String!= null) { callFunction(parameters); } else { // Intentionally left blank } ……给三元运营商?

自定义按钮不能在mac上工作(ButtonUI)

我有一个应用程序,它使用遍布文本和图标的自定义按钮。 适用于Windows和Linux,但现在OSX用户抱怨。 文本不会显示在Mac上,只是’…’。 代码看起来很简单,但是对于mac来说我很无能为力。 我怎样才能解决这个问题? 测试用例: package example.swingx; import example.utils.ResourceLoader; import com.xduke.xlayouts.XTableLayout; import javax.swing.*; import javax.swing.plaf.ComponentUI; import java.awt.*; import java.awt.event.ActionEvent; public class SmallButton extends JButton { private static ComponentUI ui = new SmallButtonUI(); public SmallButton() { super(); /* final RepaintManager repaintManager = RepaintManager.currentManager(this); repaintManager.setDoubleBufferingEnabled(false); setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);*/ } public SmallButton(AbstractAction action) { super(action); } public SmallButton(String text) […]

测试浮点相等。 (FE_FLOATING_POINT_EQUALITY)

我在ANT脚本中使用了一个findbugs,我无法弄清楚如何解决我的两个错误。 我已阅读文档,但不明白。 以下是我的错误以及与之相关的代码: 错误1:测试浮点相等性。 (FE_FLOATING_POINT_EQUALITY) private boolean equals(final Quantity other) { return this.mAmount == convertedAmount(other); } 错误2:EQ_COMPARETO_USE_OBJECT_EQUALS public final int compareTo(final Object other) { return this.description().compareTo(((Decision) other).description()); } 我已经阅读了ComparesTo问题的文档 强烈建议,但并非严格要求(x.compareTo(y)== 0)==(x.equals(y))。 一般来说,任何实现Comparable接口并且违反此条件的类都应该清楚地表明这一事实。 推荐的语言是“注意:此类具有与equals不一致的自然顺序。” 以及有关浮点平等的文档 此操作比较两个浮点值是否相等。 由于浮点计算可能涉及舍入,因此计算的浮点值和双精度值可能不准确。 对于必须精确的值,例如货币值,请考虑使用固定精度类型,例如BigDecimal。 对于不必精确的值,请考虑比较某些范围内的相等性,例如:if(Math.abs(x-y)<.0000001)。 请参阅Java语言规范,第4.2.4节。 我不明白。 有人可以帮忙吗?

单身设计模式:陷阱

目前我对这种“设计模式”非常感兴趣。 我不确定是否有使用这种严格的全局状态实施的垮台。 那么,你认为什么时候不在应用程序中练习单例?

如何在Java applet中正确指定代码库和存档?

我使用firefox版本> 3.5(3.5。 ,3.6。 ,4. *),我尝试正确指定archive和codebase属性,但它不起作用。 我的applet主类位于archive ,在运行时加载的一些必要类位于codebase 。 如果我只指定archive则加载applet但缺少codebase中的类。 如果我指定archive和codebase则无法加载applet。 看起来applet尝试从codebase文件夹加载主类,而不是查看archive文件。 no applet 主类位于http://myurl.com/archive/myjar.jar ,运行时类位于http://myurl.com/classes 。

Java中的主线程与UI线程

在这里作为答案给出的许多Swing片段中,从main方法调用SwingUtilities#invokeLater : public class MyOneClassUiApp { private constructUi() { // Some Ui related Code } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MyOneClassUiApp().constructUi(); } }); } } 但根据Threads and Swing的文章 ,从主线程构造UI是安全的: 一些方法是线程安全的:在Swing API文档中,线程安全的方法用这个文本标记: 虽然大多数Swing方法都不是,但这种方法是线程安全的。 应用程序的GUI通常可以构造并显示在主线程中:只要没有实现组件(Swing或其他),以下典型代码是安全的: public class MyApplication { public static void main(String[] args) { JFrame f = […]