java - 在 JOptionPane 内的 JScrollPane 上方添加文本
问题描述
我有以下代码:
JTextArea textArea = new JTextArea(5, 120);
textArea.setText("Error message more detail");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
JOptionPane.showMessageDialog(ScenariosUploader.this, scrollPane, "Error Message", JOptionPane.ERROR_MESSAGE);
这将创建以下 JOptionPane:
我的问题是如何在窗格上方添加类似“错误详细信息”的文本?
解决方案
与其给showMessageDialog
方法提供 scrollPane,不如给它一个面板(使用BorderLayout),其中包含滚动窗格和一个“errorDetail”标签:
public static void main(String[] args) {
JTextArea textArea = new JTextArea(5, 120);
textArea.setText("Error message more detail");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel panel = new JPanel(new BorderLayout());
JLabel errorDetailLabel = new JLabel("Error detail:");
panel.add(errorDetailLabel, BorderLayout.PAGE_START);
panel.add(scrollPane, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, panel, "Error Message", JOptionPane.ERROR_MESSAGE);
}
结果:
推荐阅读
- php - 如何对多维数组中的列值或多个值求和?
- ruby-on-rails - 应用程序信号 gem 向应用程序信号发送错误而没有崩溃请求
- zend-framework - 为什么可以重定向到 Laminas 中的命名路由,并插入控制器和操作?
- docker - 构建工具/实用程序时如何从另一个容器中运行容器
- azure-active-directory - 天蓝色广告如何使用从另一个应用程序传递的令牌进行身份验证
- javascript - React中date-fns格式日期和年龄计算问题
- java - 如何在 pom.xml 中使用 maven-assembly-plugin 来构建 Spark 应用程序时减小 jar 大小
- java - Openapi 生成器无法使用枚举生成查询参数
- spring-boot - 没有 web 但带执行器的弹簧启动
- c - char 和 toupper 的结果模棱两可