gpt4 book ai didi

java - 如何调整 JOptionPane 的大小?

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:23 24 4
gpt4 key购买 nike

我使用 JOptionPane 创建了一个登录对话框。

该对话框包含自定义 JPanel 和自定义 JButton 组件。

当用户输入错误的密码时,我想在面板底部 (SOUTH) 显示一条消息。

为了显示该消息,我使用自定义 JLabel

问题:当显示消息时,按钮被“按下”并且不再完全位于选项 Pane 内。理想情况下,选项 Pane 会自动调整大小以适应面板的新大小。

我可以设置标签的 preferredSize 以匹配我在其中使用的 Font 以及我想要在标签中显示的行数,例如字体大小 10 和一行文本 setPreferredSize(Integer.MIN_VALUE, 10)、2 行文本 setPreferredSize(Integer.Min_Value, 20)

缺点:

1)当消息未显示时,空标签占用的空间比我想要的更多。

我尝试了JOptionPane.getRootFrame.pack()。没有成功。

下面的代码(SSCC)说明了我的意思。如果您单击“登录”,则会设置错误标签文本,并且按钮将被“按下”。

import java.awt.event.ActionEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Arrays;
import java.util.logging.Logger;

public class LoginDialog {

private JTextField usernameField;
private JLabel errorLabel;
private JPasswordField passwordField;

public LoginDialog(){
}


private JPanel createMainPanel(){

JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.add(createLabelsPanel(), BorderLayout.WEST);
mainPanel.add(createTextFieldsPanel(), BorderLayout.CENTER);


errorLabel=new JLabel();
errorLabel.setFont(new Font(errorLabel.getFont().getName(), Font.PLAIN, 10));
errorLabel.setForeground(Color.RED);
errorLabel.setHorizontalAlignment(SwingConstants.CENTER);
errorLabel.setText("");
mainPanel.add(errorLabel, BorderLayout.SOUTH);

return mainPanel;
}

private JPanel createLabelsPanel(){

JPanel labelsPanel = new JPanel(new GridLayout(0, 1, 10, 10));
labelsPanel.add(new JLabel("Username", SwingConstants.RIGHT));
labelsPanel.add(new JLabel("Password", SwingConstants.RIGHT));

return labelsPanel;
}

private JPanel createTextFieldsPanel(){

JPanel textFieldsPanel = new JPanel(new GridLayout(0, 1, 10, 10));

usernameField = new JTextField();
passwordField = new JPasswordField();

textFieldsPanel.add(usernameField);
textFieldsPanel.add(passwordField);

return textFieldsPanel;
}

private JButton[] createButtons() {

JButton loginButton = new JButton("Login");

loginButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

errorLabel.setText("<html> More than two <BR> lines of text <BR> </html>");
}
});

JButton registerButton = new JButton("Register");


JButton cancelButton = new JButton("Cancel");

return new JButton[]{loginButton, registerButton, cancelButton};
}


private void showAndCreateDialog() {

JOptionPane.showOptionDialog(null, createMainPanel(), "Login", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, createButtons(), createButtons()[0]);

}

public static void main(String[] args){

SwingUtilities.invokeLater(new Runnable(){
public void run(){
new LoginDialog().showAndCreateDialog();
}
});
}
}

按钮被“按下”,并且不再完全显示在选项 Pane 中。希望了解如何调整选项 Pane 的大小以适应新的 JPanel 尺寸。

最佳答案

为了在显示后打包对话框,您将需要访问实际的 JDialog。这可以通过添加新的属性对话框并将 showOptionDialog 替换为

来完成
JOptionPane pane = new JOptionPane(createMainPanel(),
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
createButtons(), createButtons()[0]);
dialog = pane.createDialog("Login");
dialog.setVisible(true);

然后您只需在设置文本后调用 dialog.pack() 即可调整对话框的大小。

注意:默认选项对象必须包含在选项数组中,如果您调用 createButtons() 两次,它将是一个不同的对象并且不起作用,而是像这样存储选项:

JButton[] buttons = createButtons();
JOptionPane pane =
new JOptionPane(createMainPanel(), JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);

关于java - 如何调整 JOptionPane 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57655612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com