标签: joptionpane

如何通过JOptionPane格式化十进制数的输出

我的作业要求我将值格式化为两位小数.我们被要求使用JOptionPane进行输入/输出.我知道如何使用System.out.printf格式化为两位小数.但是,我认为这不会起作用,因为我们需要使用JOptionPane.

如果有人能给我任何关于如何在字符串中格式化它的建议(所以我可以将字符串解析成双字符串),我会很感激.谢谢.

这是我的代码如下:

package Program4;
import javax.swing.*;
public class Program4 {
     public static void main (String[] args)
        {
         //Declaration of Variables
         Double AssessedValue;
         Double TaxableAmount;
         Double PropertyTax;
         Double TaxRatefor100 = 1.05;
         String StrAssessedValue;
         String OutputStr;           
         //Ask the user for the Assessed Value of their property
         StrAssessedValue = JOptionPane.showInputDialog("Enter the assessed " +
                "value of your property: ");
         //Convert the string into a double
         AssessedValue = Double.parseDouble(StrAssessedValue);          
         //Calculations
         TaxableAmount = 0.92*AssessedValue;
         PropertyTax = (TaxableAmount/100)*1.05;             
         //Store the output in a string
         OutputStr …
Run Code Online (Sandbox Code Playgroud)

java swing joptionpane number-formatting

3
推荐指数
1
解决办法
1万
查看次数

如何在EDT上运行JOptionPane?

还有空白问题JOptionPane.基于SO和Java Docs的研究,这显然与不使用它有关EDT.我的问题是EDT它及其方法究竟是如何适用的JOptionPane?例如,终端错误输出清楚地表明JOptionPane下面没有运行EDT.特别缺少什么,以及如何Runnable适应?

import javax.swing.*;
public class PaneDemo
{
public static void main(String[] args)
{
    final String[] TEXT ={
            //message
            "Hello, World!",
            //title
            "Greeting"};//end TEXT
    showMyPane(TEXT);
}//end main
public static void showMyPane(final String[] TEXT)
{
    JOptionPane.showMessageDialog(null, TEXT[0], TEXT[1], 
        JOptionPane.INFORMATION_MESSAGE);
    if(!SwingUtilities.isEventDispatchThread())
    {
        System.err.println("Err: GUI failed to use EDT.");
    }//end if(!SwingUtilities.isEventDispatchThread())
}//end showMyPane
}//end class PaneDemo
Run Code Online (Sandbox Code Playgroud)

答案建议添加invokeLater.然而,这似乎并没有在BlueJ中表现得很好.

BlueJ截图

isEventDispatchThread()仍然在终端中返回错误.这只是因为它现在在错误的位置?

java swing joptionpane event-dispatch-thread

3
推荐指数
1
解决办法
1715
查看次数

JOptionPane中的JTextArea Mac OS X和Windows 8中的不同大小

我喜欢JOptionPane的便利性,但不喜欢它不包装文本的事实.所以我决定将这个问题的答案如下:

public static void main(String[] args)
 {
  String text = "one two three four five six seven eight nine ten ";
  text = text + text + text + text + text
  JTextArea textArea = new JTextArea(text);
  textArea.setColumns(30);
  textArea.setLineWrap( true );
  textArea.setWrapStyleWord( true );
  textArea.append(text);
  textArea.setSize(textArea.getPreferredSize().width, 1); //Explanation for this line in the comments of the linked thread
  JOptionPane.showMessageDialog(
   null, textArea, "Not Truncated!", JOptionPane.WARNING_MESSAGE);
 }
Run Code Online (Sandbox Code Playgroud)

这在Mac OS X上运行得很好:

在此输入图像描述

但是在Windows 8上不起作用,因为窗口没有调整大小,因为JTextArea的高度随着多行的增加而增加,从而将按钮推开:

在此输入图像描述

有什么我做错了吗?两个平台上的Java Swing表现不同吗?我该如何解决这个问题?

java swing joptionpane jtextarea windows-8

3
推荐指数
1
解决办法
497
查看次数

JOptionPane是没有选项

我有一个JOptionPane,是的,没有按钮.但是,无论你点击它的哪个按钮仍然存在.救命!下面是代码:

int dialogButton = JOptionPane.YES_NO_OPTION;
            JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
            if(dialogButton == JOptionPane.YES_OPTION) {
                System.exit(0);
            if(dialogButton == JOptionPane.NO_OPTION) {
                  remove(dialogButton);
                }
              }
Run Code Online (Sandbox Code Playgroud)

java applet swing awt joptionpane

3
推荐指数
1
解决办法
8万
查看次数

如何删除JOptionPane中的问号?

如何摆脱JOptionPane左上角的任何标记?

int result = JOptionPane.showConfirmDialog(null, myPanel, 
                   "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
Run Code Online (Sandbox Code Playgroud)

这很好,但我想删除?左上角的讨厌.

java swing joptionpane

3
推荐指数
1
解决办法
2601
查看次数

如何在JOptionPane中显示多行?

如何在一个对话框中显示所有这些信息?每次运行文件时都会出现不同的对话框,我真的需要它们出现在一个包含所有信息的对话框中.

JOptionPane.showMessageDialog(null,"Your Name:"+a1,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your age:"+age,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Birth year:"+a3,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Height:"+H,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your Weight:"+W,"Output",JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog(null,"Your BMI:"+BMI,"Output",JOptionPane.INFORMATION_MESSAGE );
Run Code Online (Sandbox Code Playgroud)

java swing joptionpane

3
推荐指数
2
解决办法
2万
查看次数

GUI - 在不同窗口之间传输数据(JFrame,JDialog,JOptionPane)

我已经开始使用bash脚本以及一些java(控制台)和python来满足我的编程需求.想想我将工作整理到某个图形环境的时候,我偶然发现了一个基本的任务,让我现在几个小时都挂了.

main函数在启动主应用程序窗口之前调用登录JDialog.JDialog实例运行基本身份验证检查(简单if语句),并在登录对话框中将类字段设置为true/false.我希望在调用类中检索此检查的结果和在文本框中键入的用户名.似乎接下来是不可能的.

那么,创建一个新窗口实例的最佳方法是什么(更合适的框架,对话框......)并能够检索一些类字段?我想到的形式比是/否答案要复杂一些.我应该能够打开登录窗口,设置窗口或带有报告参数的表单.

编辑:一些代码来解决问题.

public class Main extends JFrame {

private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Login login = new Login();
                login.showLogin();
                System.out.println(login.getTheText());

                Main frame = new Main();
                frame.setVisible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    setContentPane(contentPane);
}
Run Code Online (Sandbox Code Playgroud)

}

public class Login extends JDialog {

private JTextField txtTest;
public void showLogin() {
    try …
Run Code Online (Sandbox Code Playgroud)

java swing jframe jdialog joptionpane

3
推荐指数
1
解决办法
3881
查看次数

JOptionPane 处理确定、取消和 x 按钮

如果我不输入任何内容,我的 JOptionPane 将面临一个问题,无论我按什么(确定、取消或 x 按钮(JOptionPane 中的右上角按钮)),它都会提示我,直到我输入一个正值。但我只希望在我按下确定时出现提示。

如果我单击取消或 x 按钮(JOptionPane 中的右上角按钮),它将关闭 JOptionPane

我怎样才能做到这一点?

import javax.swing.JOptionPane;

public class OptionPane {
    public static void main(final String[] args) {
        int value = 0;
        boolean isPositive = false , isNumeric = true;
        do {
            try {
                value = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Enter value?", null));
            } catch (NumberFormatException e) {
                System.out.println("*** Please enter an integer ***");
                isNumeric = false;
            }

             if(isNumeric) {
                if(value <= 0) {
                    System.out.println("value cannot be 0 or negative");
                }

                else {
                    System.out.println("value is positive"); …
Run Code Online (Sandbox Code Playgroud)

java swing joptionpane

3
推荐指数
1
解决办法
2万
查看次数

Java:JOptionPane Radio Buttons

我正在研究一个简单的程序来帮助我计算混合eLiquid的东西.我试图在JOptionPane.showInputDialog中添加单选按钮,但我无法将它们链接在一起.当我运行程序时,什么都没有出现.这就是我的全部:

JRadioButton nicSelect = new JRadioButton("What is the target Nicotine level? ");
        JRadioButton b1 = new JRadioButton("0");
        JRadioButton b2 = new JRadioButton("3");
        JRadioButton b3 = new JRadioButton("6");
        JRadioButton b4 = new JRadioButton("12");
        JRadioButton b5 = new JRadioButton("18");
        JRadioButton b6 = new JRadioButton("24");
Run Code Online (Sandbox Code Playgroud)

java swing input button joptionpane

3
推荐指数
2
解决办法
1万
查看次数

在 JOptionPane 中放置一个 gif

我试图在 JOptionPane 中放置一个 Gif 图像来模拟“加载”,但没有结果。我尝试使用 URL、本地图像和图像的绝对路径,但我只能在其中放置图像(png、jpg...)。我最后一次尝试是这样

final ImageIcon icon = new ImageIcon(new URL("http://www.archisevilla.org/wp-content/themes/archisevilla/images/loading.gif"));
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,JOptionPane 图像的占位符看起来是空的。

所以我想知道:是否可以将 GIF 放在 JOptionPane 中,甚至放在 JFrame 中?

这是我正在使用的代码

private void cerrarsesion() throws Exception {

        this.dispose();
        String path = "/com/icono/loading.gif";  
        URL url = this.getClass().getResource(path);
        System.out.println(url);
        ImageIcon icon = new ImageIcon(url);
        createTimerClose(10).start();
        JOptionPane.showMessageDialog(null, "Cerrando sesión...", "About", JOptionPane.INFORMATION_MESSAGE, icon);
        new Login().setVisible(true);

}
Run Code Online (Sandbox Code Playgroud)

java swing gif joptionpane

3
推荐指数
1
解决办法
3456
查看次数