如何将actionlistenerand actioncommand放到多个jbuttons

所以我希望我的按钮标记为1-9,但我不想列出每个按钮的所有动作监听器和动作命令。 我怎样才能做到这一点 而且我也不能使用add.ActionListener(this)所以我可以使用什么 JButton[] button = new JButton[9]; panel.setLayout(new GridLayout(3,3)); for (int i = 0; i < button.length; i++) { button[i] = new JButton(); panel.add(button[i]); String bu = Integer.toString(i); button[i].setActionCommand(bu); button[i].addActionListener(new ActionListener()); 对不起,我刚接触java swing,所以它的abit仍然令人困惑

使图像适合打印区域

我需要用Java打印图像。 所以我实现了Printable接口的print()方法。 但是打印机总是只打印一些原始图像。 如何使图像适合打印区域(A4),这样打印机能够打印整个图像,而不是它们的一部分? 现在我的代码看起来像这样: public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= images.size()) { return Printable.NO_SUCH_PAGE; } RenderedImage image; Graphics2D graphics2D = (Graphics2D) graphics; image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND); double x = (pageFormat.getImageableWidth() – image.getWidth())/2 + pageFormat.getImageableX(); double y = (pageFormat.getImageableHeight() – image.getHeight())/2 + pageFormat.getImageableY(); […]

Swing Worker线程不并发

似乎当我实例化12个Swing Worker线程时,前六个开始完成它的任务,它完成AND然后最后六个开始并完成。 我正在寻找的行为是所有12个线程同时开始工作并同时完成。 我有以下内容: for (int i = 0; i < 12; i++ ) { myTask m = new Mytask(i, START); m.execute(); } myTask m将以0到100递增一个进度条,增量为25.我发现前六个线程开始递增的exception行为,它们以100结束,然后最后六个线程从0开始递增,然后结束。 是否存在可能具有的Swing Worker线程数量的限制因素?

在记录活动中使用readConfiguration方法

为了在小型Java桌面应用程序中使用日志记录,我试图深入理解某些方法的操作。 我使用一个非常愚蠢的小程序来测试它们。 特别是,在测试LogManager.readConfiguration()方法的行为时,我发现了一些奇怪的东西。 在所有测试中,LogManager从位于JRE目录中的lib / logging.properties中的属性文件中读取其配置。 这时,该文件的内容如下: handlers=java.util.logging.ConsoleHandler myapp2.handlers=java.util.logging.ConsoleHandler myapp2.MyApp2.handlers=java.util.logging.ConsoleHandler .level=OFF java.util.logging.ConsoleHandler.level=ALL java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n myapp2.level=WARNING myapp2.MyApp2.level=INFO java程序的代码是: package myapp2; import java.io.IOException; import java.util.logging.LogManager; import java.util.logging.Logger; public class MyApp2 { private static final Logger LOGGER = Logger.getLogger(MyApp2.class.getPackage().getName()); private static final Logger LOGGER1 = Logger.getLogger(MyApp2.class.getName()); /** * @param args the command line arguments */ public […]

Java AWT drawString()不会显示在窗口上

我正在关注Java : The complete reference 8th edition (JDK 7)的示例Java : The complete reference 8th edition (JDK 7) AWT上Java : The complete reference 8th edition (JDK 7) ,我无法在出现的窗口上显示字符串。 正确设置大小和标题,窗口显示。 如果我在paint()方法中在控制台上输出一个字符串,我发现它实际上被调用了几次,但字符串没有出现在我的应用程序的窗口中。 我看不出我偏离这个例子的地方; 我实际上有一点代码(他们添加了一个鼠标监听器和一个键监听器):\ import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Main { public static void main(String[] args) { Application app = new […]

“这个”参考在施工过程中逃脱了吗?

如果我做以下, final class FooButton extends JButton{ FooButton(){ super(“Foo”); addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ // do stuff } }); } } 我让this引用隐含地逃脱了吗?

我得到java.net.SocketException:权限被拒绝:在Jenkins中发送电子邮件时连接

我的配置: Windows 7机器 Java 7 Jenkins 1.511在本地帐户上作为服务运行 我的jenkins.xml文件 jenkins Jenkins C:\Program Files\IBM\SDP8.5\jdk\bin\java.exe -Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar “%BASE%\jenkins.war” –httpPort=8080 -Djava.net.preferIPv4Stack=true 我的hudson.tasks.Mailer.xml文件 @example.com http://localhost:8081/ Jenkins Build Server <do-not-reply@example.com> smtp.example.com false UTF-8 注意:“example.com”取代了我的真实域名/电子邮件/ smtp服务器。 当我指示Jenkins发送测试电子邮件时,我收到此错误: Failed to send out e-mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.example.com, port: 25; nested exception is: java.net.SocketException: Permission denied: connect at […]

java什么比较重:Canvas或paintComponent()?

有人能告诉我在JPanel上绘制图形应该用什么: Canvas ,或者只是在paintComponent()绘制所有内容? 我每秒约30次绘制数百个小图像,我想知道哪一个是最轻量级的,我应该在什么条件下使用它们? 谢谢。

在整个selenium套件中使用相同的Web驱动程序

我有一个inheritance自相同基类的以下selenium测试套件,当我运行整个测试套件时,如何让测试使用相同的Web驱动程序实例? 我也希望孤立地运行每个测试。 我相信这将大大减少运行套件所需的时间。 此测试从maven运行,而maven又运行每个测试类。 @RunWith(Suite.class) @SuiteClasses({ AdminPortalTestSuite.class, DevPortalTestSuite.class, CommonTestSuite.class }) public class SeleniumTestSuite { } 基类所有测试都inheritance自 @BeforeClass public static void setUp() throws Exception { if (null == baseUrl || !baseUrl.startsWith(“https://”)) { baseUrl = “https://localhost:8443”; } if (null == browser || browser.startsWith(“${“)) { browser = “firefox”; } //retrieve properties including locale. retrieveProperties(); Thread.sleep(4000); setUpDriver(); } @After […]

为什么我无法使用父类型访问子对象方法

对象a2属于A类但引用了类C的对象。因此,a2应该能够访问m3()。 但是,为什么不发生呢? 如果在类A中定义了m3()方法,则代码可以正常运行 class A { int var = 7; void m1() { System.out.println(“A’s m1 ,”); } void m2() { System.out.println(“A’s m2 ,”); } } class B extends A { void m1() { System.out.println(“B’s m1 ,”); } } class C extends B { void m3() { System.out.println(“c’s m3 ,” + (var + 6)); } } […]