簡體   English   中英

增加 JOptionPane 中的按鈕大小 - Java

[英]Increase button size in JOptionPane - Java

我想增加JOptionePanebutton大小。 buttonJOptionPane的代碼是:

         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的大小?

此代碼可以幫助您使用 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]);
}
}

在此處輸入圖像描述

執行此操作的另一種功能方法是確實利用JOptionPane#showOptionDialog()方法(如前所述)而不是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>&nbsp;&nbsp; Yes &nbsp;&nbsp;</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 字符串時,您需要始終以<html>開始字符串,並以結束</html>標記結束整個字符串。

<br>標簽。 <b></b>標簽。

<u></u>標簽。

<font></font>標簽。

<center> and </center>標簽。

&nbsp; - 實體 - 非中斷空間。

通過使用 HTML,您可以做很多事情來為某些swing 組件(如JLabelsJButtons等(包括工具提示))中的消息框和文本增添趣味。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM