2016-07-25 40 views
-2

我正在考虑在JAVA中更改JOptionPane.ShowMessageDialouge(null,"Output");。基本上我需要在确认按钮上添加打印按钮,所以如果用户点击确定只需关闭它,或者如果点击打印按钮,然后继续按打印按钮的actionevent。自定义JOptionPane

+0

最好创建自己的模态JDialog,完成您所需的按钮和功能。 –

+1

或者使用JOptionPane的showOptionDialog,并根据API通过String数组添加所需的任何自定义按钮文本。 –

+0

不,我只是想添加按钮,当我打印它时,它可能会导致我进入一个动作事件。 –

回答

1

您只需使用showOptionDialog方法。看看Oracle文档。

下面是点击“OK”时关闭对话框的解决方案:JOptionPane cancel button

为例,你有自定义按钮:

JButton but_print = new JButton("PRINT"); 
    JButton but_ok = new JButton("OK"); 

    but_ok.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      //Close the dialog 
     } 
    }); 

    JOptionPane.showOptionDialog(null, "Test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{but_print,but_ok}, but_ok); 
-1

感谢您的帮助球员。我只是发现更好的东西,它正在正常工作,因为我想。

int response = JOptionPane.showConfirmDialog(null, s+"Print?",cnicstring,JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
if (response == JOptionPane.NO_OPTION) { 
    //System.out.println("Pressed NO Button"); 
} else if (response == JOptionPane.YES_OPTION) { 
    // System.out.println("Pressed Yes Button"); 
    txtarea.print(); 
} else if (response == JOptionPane.CLOSED_OPTION) { 
    //System.out.println("JOptionPane closed"); 
} 
相关问题