【问题标题】:JButton not respondingJButton 没有响应
【发布时间】:2021-07-27 06:01:59
【问题描述】:

您好,我是 Java 新手,现在我正在解决其他人的代码问题,因为按钮没有响应。当点击 UI 时没有任何反应。

非常感谢您对此的帮助。 (添加一些文字,因为 stackoverflow 告诉我您的帖子看起来主要是代码;请添加更多详细信息。)

代码如下:

private JButton printAddressButton;
String p_link = http://www.google.com/images/logo_google_suggest.gif;
String printer_keyword = "DYMO";


private void jbInit() throws Exception {
    getContentPane().setLayout(null);
    setSize(100, 35);
    printAddressButton.setText("Print Here");
    printAddressButton.setBounds(new Rectangle(0, 0, 30, 30));
    printAddressButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            printAddressButton_actionPerformed(e);
        }
    });
    getContentPane().add(printAddressButton, null);
}

private void printAddressButton_actionPerformed(ActionEvent e) {
    print_address(p_link);
}

public void print_address(String link) {
    try {
        System.out.println("Servlet URL: " + link + ", Printer Keyword: " + printer_keyword);
        PrintService services[] = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println("Number of printers: " + services.length);
        for (int i = 0; i < services.length; i++) {
            System.out.println(services[i].getName());
            PrintService myprinter = services[i];
            if (services[i].getName().indexOf(printer_keyword) >= 0) {
                System.out.println("Found " + printer_keyword + " printer: " + services[i]);
                DocPrintJob print_job = myprinter.createPrintJob();
                URL url = new URL(link);
                DocFlavor flavor = "GET".equalsIgnoreCase(method) ?
                                    javax.print.DocFlavor.URL.PNG:
                                    javax.print.DocFlavor.INPUT_STREAM.PNG;
                DocAttributeSet doc_attrs = new HashDocAttributeSet();
                doc_attrs.add(OrientationRequested.LANDSCAPE);
                SimpleDoc doc = "GET".equalsIgnoreCase(method) ?
                                new SimpleDoc(url, flavor, doc_attrs) :
                                new SimpleDoc(createHttpRequestWithPostMethod(url).getInputStream(), flavor, doc_attrs);
                PrintRequestAttributeSet print_attrs = new HashPrintRequestAttributeSet();
                print_attrs.add(new Copies(1));
                print_attrs.add(new MediaPrintableArea(20, 30, 56, 84, 1000));
                print_job.print(doc, print_attrs);
                System.out.println("finished printing");
                break;
            }
        }
    }
    catch(Throwable ex) {
        ex.printStackTrace();
    }
    finally {
    }
}

【问题讨论】:

  • 检查控制台,如果你点击它会打印一些东西
  • 就在 print_address 方法中的 try 之前。放一个打印语句来显示方法已经被输入。如果没有,你的问题就出在其他地方。请注意,如果该过程需要很长时间,它将锁定 GUI,因为 EDT 上的事件是按顺序处理的,我还建议使用布局管理器而不是对组件进行绝对定位。
  • 您正在使用 *PNG DocFlavors,但您的 URL 指向 GIF。 GIF 不是 PNG。

标签: java swing jbutton


【解决方案1】:

在您的for (int i = 0; i &lt; services.length; i++) { 行之后插入System.out.println("Looping " + i);,您将在控制台中看到它实际运行,但可能需要很长时间才能完成,看起来它似乎没有运行。

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多