- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用 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/
我正在使用 JOptionPane 来获取字符串。单击“确定”即可保存字符串。现在我希望弹出第二个 JOptionPane 以输入第二个必要的字符串。是否有机会将 ActionListener 添加到
我有一个显示来自 JOptionPane 的确认消息的过程。此过程从 JMenuItem 的 Actionlistener 内的 SwingUtilities.invokeLater(runnable
我使用以下代码来获取InputDialog: String c = JOptionPane.showInputDialog("Select number",JOptionPane.OK_OPTION)
正如你从我上面的主题中看到的, 我想知道我怎么能解雇一个 JOptionPane 因为另一个 JOptionPane 而变得无关紧要,因为用户出于某种原因没有通过自己点击确定按钮(例如)解雇第一个。
没有收到“文件已成功接收”消息,请帮助我。将文件从客户端传输到服务器时,即使文件已收到,也不会显示该消息。 这是使用套接字将文件从客户端传输到服务器的程序,接口(interface)也是使用java
JOptionPane.showInputDialog 有一个不需要父组件参数的表单。 JOptionPane.showConfirmDialog 确实如此。这是为什么? 最佳答案 JOptionPa
我正在用 Java 创建一个基于 Tic Tac Toe GUI 的游戏,并且正在努力将 2D 数组与 JOptionPane 结合使用。到目前为止,我已经能够创建可供选择的按钮: import ja
使用 JOprionPane 时,光标出现了一些问题。我将光标设置到 pharent 框架,然后使用这个显示一个对话框: Object[] possibilities = {"ham", "spam"
我创建了一个打开 JOptionPane 的按钮。它允许用户输入 string..>> String str = JOptionPane.showInputDialog 如何获取用户输入到 jopti
该代码正在运行。但问题是,当选择 NO_OPTION 时,窗口就会被释放。我想在选择 NO_OPTION 时保留窗口?你能给点建议吗? int dialogButton = JOptionPa
我正在 Java 上开发 Tic Tac Toe 游戏(eclipse)。在我的计算机上,我的对话框非常小。我一直在努力把它做得更大。我没有任何运气。我希望这里有人能引导我走向正确的方向。下面的代码是
我正在使用脚本 API 为我玩的游戏的机器人制作脚本,但是每当我覆盖机器人管理器时,就会出现一个 JOptionPane 来阻止执行,直到我关闭它,但是我想在不运行该脚本的情况下运行此脚本人为干预,所
我正在用 Java 开发一个游戏,我试图在 JOptionPane 确认框打开时暂停它。这是代码: window.addWindowListener(new WindowAdapter() {
这个问题已经有答案了: Calling one JFrame from another using Timer without any buttons (1 个回答) 已关闭 9 年前。 需要帮助制作
很抱歉代码这么长,我只需要知道我缺少什么来初始化我的所有决定。编译器提示我的变量没有被初始化。谢谢你。 import javax.swing.JOptionPane; public class Sem
我想使用 JOptionPane 处理一些异常。这是主要方法: public class MainRun { public static void main(String args[]){
不允许使用数组,该函数正在工作,但只是返回 0,就好像它没有计算正确的输入字符一样,但现在它给了我一个“字符串超出范围:3” 这应该运行,打开一个窗口,要求我输入一个字符串,在本例中是一个单词,然后打
我正在使用 Jfilechooser,如果我选择文件,它将计算文件名的字符数,但是如果文件超过 3kb,它将限制 Joptionpane 将显示。我的问题是即使文件是0kb,Joptionpane也会
我创建了一个应用于框架的名为 addSupplier 的按钮,然后创建了一个操作监听器,因此一旦按下 addSupplier 按钮,它将创建一个 JOptionPane,其中有一个附加了 JTextF
我知道解决方案是使用 for 循环来逐步遍历数组并显示在 Pane 中。但是我没有找到对此的直接解释。我需要一个下一个和一个上一个按钮来显示每个数组元素,并且在按下下一个按钮时到达第一个元素后仅返回到
我是一名优秀的程序员,十分优秀!