增加 JOptionPane 中的按钮大小 - Java
Increase button size in JOptionPane - Java
我想增加 JOptionePane
的 button
大小。 button
的 JOptionPane
的代码是:
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
的大小?
此代码可以帮助您显示选项对话框:
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]);
}
}
另一种 功能性 方法是真正利用 JOptionPane#showOptionDialog() method (as already indicated) instead of the JoptionPane#showConfirmDialog()。 showOptionDialog()
方法允许您拥有更多您可能需要的自定义按钮。
我认为自定义消息区域和按钮文本、颜色和大小的最简单方法是使用基本 HTML 字符串而不是纯文本字符串。这在所有 JOptionPane 对话框的消息区域中很典型,如果您愿意,您甚至可以通过这种方式将小图像添加到文本中。就我而言,它为您的对话框如何呈现给最终用户打开了一个全新的灵活性世界。如果您真的想要低调和肮脏,请创建您自己的 JDialog 选项 window 对话框,以完全按照您想要的方式进行外观和感觉。
对于您当前想要做的事情(使用自定义按钮),JOptionPane#showOptionDialog() 方法就足够了,例如(英文):
下面是显示上述确认类型对话框的代码:
// 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 实际上 returns 对话框中选择的按钮的 index 值和选择该按钮后对话框正确关闭(应该如此)。
关于上面代码中使用的HTML标签:
使用 HTML 字符串时,您需要始终以 <html>
开始字符串,并以 </html>
结束标记结束整个字符串。
<br>
标签。
<b>
and </b>
个标签。
<u>
and </u>
个标签。
<font>
和
</font>
标签。
- 实体 - 不间断 Space.
在 某些 swing 组件(例如 JLabels 和 JButtons,等(包括工具提示)通过使用 HTML.
我想增加 JOptionePane
的 button
大小。 button
的 JOptionPane
的代码是:
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
的大小?
此代码可以帮助您显示选项对话框:
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]);
}
}
另一种 功能性 方法是真正利用 JOptionPane#showOptionDialog() method (as already indicated) instead of the JoptionPane#showConfirmDialog()。 showOptionDialog()
方法允许您拥有更多您可能需要的自定义按钮。
我认为自定义消息区域和按钮文本、颜色和大小的最简单方法是使用基本 HTML 字符串而不是纯文本字符串。这在所有 JOptionPane 对话框的消息区域中很典型,如果您愿意,您甚至可以通过这种方式将小图像添加到文本中。就我而言,它为您的对话框如何呈现给最终用户打开了一个全新的灵活性世界。如果您真的想要低调和肮脏,请创建您自己的 JDialog 选项 window 对话框,以完全按照您想要的方式进行外观和感觉。
对于您当前想要做的事情(使用自定义按钮),JOptionPane#showOptionDialog() 方法就足够了,例如(英文):
下面是显示上述确认类型对话框的代码:
// 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 实际上 returns 对话框中选择的按钮的 index 值和选择该按钮后对话框正确关闭(应该如此)。
关于上面代码中使用的HTML标签:
使用 HTML 字符串时,您需要始终以 <html>
开始字符串,并以 </html>
结束标记结束整个字符串。
<br>
标签。
<b>
and </b>
个标签。
<u>
and </u>
个标签。
<font>
和
</font>
标签。
- 实体 - 不间断 Space.
在 某些 swing 组件(例如 JLabels 和 JButtons,等(包括工具提示)通过使用 HTML.