我有一个程序,我正在加载一个文件,同时我正在显示一个窗口,通知用户该文件正在加载.我决定创建一个FileLoader类,它实际上是处理文件的SwingWorker和实现PropertyChangeListener的ProgressWindow,以通知用户传递给它的SwingWorker的状态.
我的代码目前看起来像这样:
FileLoader loader = new FileLoader(filePath);
new ProgressWindow(loader, "Loading File", "Loading File");
//ProgressWindow's constructor calls loader.execute() inherited from SwingWorker
doc = loader.get(); //GUI Freezes when called
Run Code Online (Sandbox Code Playgroud)
问题是,无论何时我调用loader.get(),它都会冻结GUI,因此进度窗口中的进度条不会运行,整个过程毫无意义.据我所知,这是因为控制GUI的线程与调用loader.get()的线程相同,而loader.get()在loader.execute()运行时保持不变.
到目前为止,我已经尝试为loader.get()命令或loader.execute()方法创建一个新线程,并在线程上调用SwingUtilities.invokeLater(),但随后整个程序冻结.
我考虑过为SwingWorker.isDone()创建一个ChangeListener,然后运行loader.get(),但是这需要对我的代码进行一些修改,而我宁愿不做.
谁能告诉我最好的方法是让它发挥作用?
我喜欢JOptionPane的便利性,但不喜欢它不包装文本的事实.所以我决定将这个问题的答案如下:
public static void main(String[] args)
{
String text = "one two three four five six seven eight nine ten ";
text = text + text + text + text + text
JTextArea textArea = new JTextArea(text);
textArea.setColumns(30);
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.append(text);
textArea.setSize(textArea.getPreferredSize().width, 1); //Explanation for this line in the comments of the linked thread
JOptionPane.showMessageDialog(
null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
这在Mac OS X上运行得很好:

但是在Windows 8上不起作用,因为窗口没有调整大小,因为JTextArea的高度随着多行的增加而增加,从而将按钮推开:

有什么我做错了吗?两个平台上的Java Swing表现不同吗?我该如何解决这个问题?
我想在MigLayout中创建一个表单.我希望任何文本字段都标有前面的小JLabel,文本字段随着空间的增加而增长.我可以在以下代码中成功完成此操作:
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new MigLayout("", "fill", ""));
panel.add(new JLabel("Testing"));
panel.add(new JTextField(), "growx, pushx, wrap");
frame.setContentPane(panel);
frame.pack();
frame.setMinimumSize(new Dimension(400, 100));
frame.setPreferredSize(new Dimension(400, 100));
frame.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
结果如下所示,这是预期的(JLabel是最小必需大小,JTextField占用了该区域的其余部分):

但是,如果我将JEditorPane放在这个下方并使其跨越整个窗口的长度,那么如果我放大窗口,JLabel会变大并增长.代码更改如下所示:
...
panel.add(new JTextField(), "growx, pushx, wrap");
//New line of code here:
panel.add(new JEditorPane(), "growx, pushx, span");
frame.setContentPane(panel);
...
Run Code Online (Sandbox Code Playgroud)
这导致我不期望的结果(JLabel已经增长):

我试图通过为JLabel添加MigLayout参数来解决这个问题,比如"growx 0"和"shrink",但这似乎对标签的大小没有任何影响.
如何防止JLabels在这种情况下成长?
我正在研究在网络应用程序中使用 PDF.js。到目前为止,它满足了我们所有的业务需求。然而,管理层要求我们能够禁用 PDF 中的超链接。我们不一定要去掉蓝色文本和下划线,但如果用户单击超链接,它不应该去任何地方。
我仔细查看了 API,但没有找到任何相关内容。我还查看了源代码,但我没有发现任何可以注释掉以禁用超链接的内容。有什么方法可以禁用 PDF 中包含的超链接吗?
我的数据库中有一个序列,我通过Liquibase生成.在重构期间,我们决定我们不喜欢我们给它的名称,我们想重命名它,保留当前存在的所有数据.
似乎可以改变序列,但我没有看到任何关于如何重命名序列的内容.有没有办法做到这一点,或合理的解决方法?
(如果重要,我正在使用Oracle SQL)
据我了解,通常Expectations用于模拟具有不同返回值的值。例如:
new Expectations() {{
bar.getGreeting();
result = "Hello, world!";
times = 2;
}};
Run Code Online (Sandbox Code Playgroud)
我注意到这result是可选的。那时,此块仅确认该方法已被调用两次,MissingInvocation如果未调用则抛出错误。因此,例如:
@Test
public void testRunFoo(@Mocked final Bar bar) {
Foo foo = new Foo(bar);
new Expectations() {{
bar.runBar();
times = 2;
}};
foo.runFooWithBarTwice(); //Successful
//foo.runFooWithoutBar(); //Will throw a MissingInvocationException
}
Run Code Online (Sandbox Code Playgroud)
我注意到此代码似乎与使用相同Verifications:
@Test
public void testRunFoo(@Mocked final Bar bar) {
Foo foo = new Foo(bar);
foo.runFooWithBarTwice(); //Successful
//foo.runFooWithoutBar(); //Will throw a MissingInvocationException
new Verifications() {{
bar.runBar();
times = 2;
}}; …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 Gradle 复制任务:
task copyFromZip(type: Copy) {
from zipTree("Example.zip")
into "build/output"
}
Run Code Online (Sandbox Code Playgroud)
Zip 文件内是这样的:
+--- photos
| +--- StackOverflow
| \--- foo.png
| \--- Super User
| \--- bar.png
+--- text
| \--- Stack Exchange
| +--- StackOverflow
| \--- foo.txt
| \--- Super User
| \--- bar.txt
Run Code Online (Sandbox Code Playgroud)
当我运行复制任务时,目录将被保留。例如,我有build/output/photos/StackOverflow/foo.png.
我想要做的是在复制时重命名目录。例如,假设我意识到“StackOverflow”中应该有空格。我希望将文件photos/StackOverflow/foo.png复制到build/output/photos/Stack Overflow/foo.png,并将同样的内容应用于其他文件夹中名称为“StackOverflow”而不是“Stack Overflow”的文件。
我的第一个想法是添加一个rename{}块。
task copyFromZip(type: Copy) {
from zipTree("Example.zip")
into "build/output"
rename { String filePath ->
filePath.replace("StackOverflow", "Stack Overflow") …Run Code Online (Sandbox Code Playgroud)