我想增加button一個JOptionePane. 的代碼JOptionPane是button:
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt) {
if (JOptionPane.showConfirmDialog(rootPane, "?Desea salir de la aplicación?",
"Gestor de clientes", JOptionPane.ERROR_MESSAGE) == JOptionPane.ERROR_MESSAGE) {
dispose();
Login login = new Login();
login.setVisible(true);
}
}
});

如何增加文本的字體和大小button?
uj5u.com熱心網友回復:
此代碼可以幫助您使用 showOptionDialog :
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class CustomizedJOption {
public static void main(String[] args) {
//Confirm button
JButton btnYes = new JButton ( " OK ");
btnYes.setFont(new Font("young circle", Font.BOLD, 30));
btnYes.setForeground(Color.MAGENTA);
//Negative button
JButton btnNo = new JButton ( " No ");
btnNo.setFont(new Font("young circle", Font.ITALIC, 30));
btnNo.setForeground(Color.blue);
//Add button options to the array
Object [] options = {btnYes, btnNo} ;
//text content
JLabel label = new JLabel ( "\"?Desea salir de la aplicación?\"");
label.setForeground(Color.BLUE);
label.setFont(new Font("young circle", Font.ITALIC, 30));
//Display Dialog
JOptionPane .showOptionDialog(null, label, "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
}
}

uj5u.com熱心網友回復:
執行此操作的另一種功能方法是確實利用
這是顯示上述確認型別對話框的代碼:
// The dialog box title
String title = "Exit Application?"; // HTML won't work here :(
// The dialog's Message area - Basic HTML can be used here. :)
String message = "<html><font size='4'>Are you Sure"
" you want to exit this application?</font><br><br><center>"
"<font size='5',color=blue><b>My Application Name</b>"
"</font></center><br></html>";
/* The desired buttons for the dialog Box. - Basic HTML can be used here. :)
The buttons are automatically sized based on the font size of the text
applied to the button Text (caption). */
String[] optionButtons = {
// The YES Button.
"<html><font size='5',color=red> Yes </font><br>"
"<center><font size='1',color=gray>....</font></center</html",
// The NO button.
"<html><font size='5'>No! Not Now</font><br>"
"<center><font size='1',color=gray>....</font></center</html",
// The NOT SURE button. :)
"<html><font size='5'>Well, Not Sure ???</font><br>"
"<center><font size='2'>(I think)</font></center</html"};
// Display the JOptionPane dialog.
/* The Swing Component that will be parent for this Dialog.
Use 'this' if the class this code is run in an extend JFrame.
If the code is run through an action event of a JButton
then just provide the variable name for that button. If
worse comes to worse...just supply: null. */
java.awt.Component comp = (java.awt.Component) null;
int result = JOptionPane.showOptionDialog(comp, message, title,
int result = JOptionPane.showOptionDialog(comp, message, title,
JOptionPane.DEFAULT_OPTION, // Button Options - DEFAULT_OPTION - moot here.
JOptionPane.QUESTION_MESSAGE, // The internal image to use.
null, // No custom icon
optionButtons, // Button Captions (Text)
optionButtons[1] // Default focused button - NO is made to have default focus.
);
switch (result) {
// Yes button selected
case 0:
System.out.println("You selected: Yes! Please");
break;
// No button selected
case 1:
System.out.println("You selected: No! Not now.");
break;
// Don't Know button selected
case 2:
// Just a little decision making between Yes and No.
while (result == 2) {
result = new java.util.Random().nextInt(2);
System.out.println(result);
}
// Display the Decision _ Ternary Operator used here.
System.out.println("You selected: Well, Not Sure - Decided this one for you, "
(result == 0 ? "Yes selected!" :
result == 1 ? "No Sselected!" :
"Dialog Canceled (Closed)"));
break;
// Dialog simply closed from title bar.
default:
System.out.println("None selected - Dialog Canceled (Closed)");
}
在上面的代碼中,JOptionPane實際上回傳對話框中選擇的按鈕的索引值,并且對話框在該按鈕選擇時正確關閉(應該如此)。
關于上面代碼中使用的 HTML 標簽:
使用 HTML 字串時,您需要始終以 the 開始字串,并以結束標記<html>結束整個字串。</html>
<br>
標簽。
<b>和</b>標簽。
<u>和</u>標簽。
<font>和
</font>
標簽。
<center> and </center> 標簽。
- 物體 - 非中斷空間。
通過使用 HTML,您可以做很多事情來為JLabels和JButtons等(包括 ToolTips)等擺動組件中的訊息框和文本增添趣味。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/416543.html
標籤:
下一篇:如何定位特定行程的unix套接字
