Java 调整JOptionPane对话框和对话框中的行的大小

Java 调整JOptionPane对话框和对话框中的行的大小,java,swing,joptionpane,Java,Swing,Joptionpane,我的程序应该有java的基本代码示例,要做到这一点,我需要帮助创建对话,在那里我可以编写代码,但我不能在对话中添加空格并调整它们的大小。请帮忙 主要类别: public class JavaHelperTester{ public static void main(String[] args){ JavaWindow display = new JavaWindow(); JavaHelper j = new JavaHelper();

我的程序应该有java的基本代码示例,要做到这一点,我需要帮助创建对话,在那里我可以编写代码,但我不能在对话中添加空格并调整它们的大小。请帮忙

主要类别:

public class JavaHelperTester{
      public static void main(String[] args){
       JavaWindow display = new JavaWindow();
          JavaHelper j = new JavaHelper();
          display.addPanel(j);
          display.showFrame();
     }
}
方法类:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class JavaHelper extends JPanel implements ActionListener{

   JButton print = new JButton("Print Statements");
   JButton classes = new JButton("Classes");
   JButton varibles = new JButton("Assiging variables");
   JButton signs = new JButton("Sign meanings");
   JButton typesv = new JButton("Different Types of variables");
   JButton scanners = new JButton("Scanner");
   JButton loops = new JButton("Loops");
   JButton ifstatements = new JButton("If statements");
   JButton graphics = new JButton("Graphics");
   JButton objects = new JButton("Making an oject");
   JButton importstatments = new JButton("Import Statements");
   JButton integers = new JButton("Different types of integers");
   JButton methods = new JButton("Scanner methods");
   JButton math = new JButton("Math in java");
   JButton creation = new JButton("Method creation");
   JButton arrays = new JButton("Arrays");
   JButton jframe = new JButton("JFrame");
   JButton stringtokenizer = new JButton("String Tokenizer");
   JButton extending = new JButton("Class extending");
   JButton fileio = new JButton("File I.O.");
   JButton quit = new JButton("Quit");
  public JavaHelper(){

    setPreferredSize(new Dimension(500,350));
    setBackground(Color.gray);
    this.add(print);
    print.addActionListener(this);
    this.add(classes);
    classes.addActionListener(this);
    this.add(varibles);
    varibles.addActionListener(this);
    this.add(signs);
    signs.addActionListener(this);
    this.add(typesv);
    typesv.addActionListener(this);
    this.add(scanners);
    scanners.addActionListener(this);
    this.add(loops);
    loops.addActionListener(this);
    this.add(ifstatements);
    ifstatements.addActionListener(this);
    this.add(graphics);
    graphics.addActionListener(this);
    this.add(objects);
    objects.addActionListener(this);
    this.add(importstatments);
    importstatments.addActionListener(this);
    this.add(integers);
    integers.addActionListener(this);
    this.add(methods);
    methods.addActionListener(this);
    this.add(math);
    math.addActionListener(this);
    this.add(creation);
    creation.addActionListener(this);
    this.add(arrays);
    arrays.addActionListener(this);
    this.add(jframe);
    jframe.addActionListener(this);
    this.add(stringtokenizer);
    stringtokenizer.addActionListener(this);
    this.add(fileio);
    fileio.addActionListener(this);
    this.add(quit);
    quit.addActionListener(this);
  }
  public void paintComponent(Graphics g){
    super.paintComponent(g);

  }
  public void actionPerformed(ActionEvent e){
    if(e.getSource() == print){
     JOptionPane.showMessageDialog (null, "System.out.println(); and System.out.print();", "Print Statements", JOptionPane.INFORMATION_MESSAGE);
    }
    if(e.getSource() == classes){
     JOptionPane.showMessageDialog (null, "Main class : public class ClassNameTester{ // public static void main(String[] args){, Other Classes : public class ClassName", "Classes", JOptionPane.INFORMATION_MESSAGE);
    }
    if(e.getSource() == quit){
     System.exit(0);
    }

  }
  private void dialogSize(){

  }
}
JavaWindow:

import java.awt.*;
import javax.swing.*;
public  class JavaWindow extends JFrame{

  private Container c;

  public JavaWindow(){
    super("Java Helper");
    c = this.getContentPane();
  }

  public void addPanel(JPanel p){
    c.add(p);
  }

  public void showFrame(){
    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

我做了一些更改来清理Java Helper GUI,并允许您格式化JOptionPane对话框上的信息

下面是Java助手GUI

这是班级作业窗格

我修改了您的JavaHelperTester类,以包含对SwingUtilities invokeLater方法的调用。此方法将Swing组件的创建和使用置于。Swing GUI必须以调用SwingUtilities invokeLater方法开始

我还格式化了所有代码并解析了导入

package com.ggl.java.helper;

import javax.swing.SwingUtilities;

public class JavaHelperTester {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                JavaWindow display = new JavaWindow();
                JavaHelper j = new JavaHelper();
                display.addPanel(j);
                display.showFrame();
            }
        };

        SwingUtilities.invokeLater(runnable);
    }
}
这是您的JavaWindow

package com.ggl.java.helper;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class JavaWindow extends JFrame {

    private static final long serialVersionUID = 6535974227396542181L;

    private Container c;

    public JavaWindow() {
        super("Java Helper");
        c = this.getContentPane();
    }

    public void addPanel(JPanel p) {
        c.add(p);
    }

    public void showFrame() {
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
这是您的JavaHelper类。我对您的操作侦听器进行了更改,以允许您将文本定义为HTML字符串。您只能使用HTML3.2命令

我还更改了按钮面板以使用GridLayout。网格布局使按钮看起来更整洁,用户可以更轻松地选择JButton

package com.ggl.java.helper;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JavaHelper extends JPanel implements ActionListener {

    private static final long serialVersionUID = -3150356430465932424L;

    JButton print = new JButton("Print Statements");
    JButton classes = new JButton("Classes");
    JButton varibles = new JButton("Assiging variables");
    JButton signs = new JButton("Sign meanings");
    JButton typesv = new JButton("Different Types of variables");
    JButton scanners = new JButton("Scanner");
    JButton loops = new JButton("Loops");
    JButton ifstatements = new JButton("If statements");
    JButton graphics = new JButton("Graphics");
    JButton objects = new JButton("Making an oject");
    JButton importstatments = new JButton("Import Statements");
    JButton integers = new JButton("Different types of integers");
    JButton methods = new JButton("Scanner methods");
    JButton math = new JButton("Math in java");
    JButton creation = new JButton("Method creation");
    JButton arrays = new JButton("Arrays");
    JButton jframe = new JButton("JFrame");
    JButton stringtokenizer = new JButton("String Tokenizer");
    JButton extending = new JButton("Class extending");
    JButton fileio = new JButton("File I.O.");
    JButton quit = new JButton("Quit");

    public JavaHelper() {
        this.setLayout(new GridLayout(0, 2));
        setBackground(Color.gray);

        this.add(print);
        print.addActionListener(this);
        this.add(classes);
        classes.addActionListener(this);
        this.add(varibles);
        varibles.addActionListener(this);
        this.add(signs);
        signs.addActionListener(this);
        this.add(typesv);
        typesv.addActionListener(this);
        this.add(scanners);
        scanners.addActionListener(this);
        this.add(loops);
        loops.addActionListener(this);
        this.add(ifstatements);
        ifstatements.addActionListener(this);
        this.add(graphics);
        graphics.addActionListener(this);
        this.add(objects);
        objects.addActionListener(this);
        this.add(importstatments);
        importstatments.addActionListener(this);
        this.add(integers);
        integers.addActionListener(this);
        this.add(methods);
        methods.addActionListener(this);
        this.add(math);
        math.addActionListener(this);
        this.add(creation);
        creation.addActionListener(this);
        this.add(arrays);
        arrays.addActionListener(this);
        this.add(jframe);
        jframe.addActionListener(this);
        this.add(stringtokenizer);
        stringtokenizer.addActionListener(this);
        this.add(fileio);
        fileio.addActionListener(this);
        this.add(quit);
        quit.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == print) {
            String title = ((JButton) e.getSource()).getText();
            JLabel label = new JLabel(createPrintText());
            JOptionPane.showMessageDialog(this, label, title,
                    JOptionPane.INFORMATION_MESSAGE);
        }

        if (e.getSource() == classes) {
            String title = ((JButton) e.getSource()).getText();
            JLabel label = new JLabel(createClassesText());
            JOptionPane.showMessageDialog(this, label, title,
                    JOptionPane.INFORMATION_MESSAGE);
        }

        if (e.getSource() == quit) {
            System.exit(0);
        }

    }

    private String createPrintText() {
        StringBuilder builder = new StringBuilder();
        builder.append("<html><pre><code>");
        builder.append("System.out.print()");
        builder.append("<br>");
        builder.append("System.out.println()");
        builder.append("</code></pre>");

        return builder.toString();
    }

    private String createClassesText() {
        StringBuilder builder = new StringBuilder();
        builder.append("<html><pre><code>");
        builder.append("Main class : public class ClassNameTester { <br>");
        builder.append("    public static void main(String[] args) { <br>");
        builder.append("    } <br>");
        builder.append("} <br><br>");
        builder.append("Other Classes : public class ClassName { <br>");
        builder.append("}");
        builder.append("</code></pre>");

        return builder.toString();
    }

}
package com.ggl.java.helper;
导入java.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
公共类JavaHelper扩展JPanel实现ActionListener{
私有静态最终长serialVersionUID=-3150356430465932424L;
JButton print=新JButton(“打印语句”);
JButton类=新JButton(“类”);
JButton varibles=新JButton(“辅助变量”);
JButton符号=新JButton(“符号含义”);
JButton typesv=新JButton(“不同类型的变量”);
JButton扫描器=新JButton(“扫描器”);
JButton循环=新JButton(“循环”);
JButton ifstatements=新JButton(“If语句”);
JButton graphics=新JButton(“图形”);
JButton objects=新JButton(“生成项目”);
JButton importstatements=新JButton(“导入语句”);
JButton integers=新JButton(“不同类型的整数”);
JButton方法=新JButton(“扫描器方法”);
jbuttonmath=newjbutton(“java中的数学”);
JButton creation=新JButton(“方法创建”);
JButton数组=新JButton(“数组”);
JButton jframe=新JButton(“jframe”);
JButton stringtokenizer=新JButton(“字符串标记器”);
JButton extensing=新JButton(“类扩展”);
JButton fileio=新JButton(“文件I.O.”);
JButton quit=新JButton(“quit”);
公共JavaHelper(){
这个.setLayout(新的GridLayout(0,2));
挫折背景(颜色:灰色);
添加(打印);
print.addActionListener(this);
本节添加(类);
addActionListener(this);
添加(变量);
varibles.addActionListener(此);
本条增加(标志);
signs.addActionListener(此);
添加(typesv);
typesv.addActionListener(此);
添加(扫描仪);
scanners.addActionListener(this);
添加(循环);
loops.addActionListener(this);
本节添加(IFS声明);
ifstatements.addActionListener(this);
添加(图形);
graphics.addActionListener(此);
添加(对象);
addActionListener(this);
添加(进口状态);
importStatements.addActionListener(此);
这个。加(整数);
integers.addActionListener(this);
增加(方法);
方法addActionListener(this);
这个。加上(数学);
math.addActionListener(this);
添加(创建);
creation.addActionListener(this);
添加(数组);
addActionListener(this);
添加(jframe);
jframe.addActionListener(this);
添加(stringtokenizer);
stringtokenizer.addActionListener(此);
添加(fileio);
fileio.addActionListener(这个);
这个。添加(退出);
quit.addActionListener(this);
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(如getSource()==打印){
字符串标题=((JButton)e.getSource()).getText();
JLabel=新的JLabel(createPrintText());
JOptionPane.showMessageDialog(此、标签、标题、,
JOptionPane.INFORMATION(信息和消息);
}
如果(例如getSource()==类){
字符串标题=((JButton)e.getSource()).getText();
JLabel=newjLabel(createClassesText());
JOptionPane.showMessageDialog(此、标签、标题、,
JOptionPane.INFORMATION(信息和消息);
}
如果(如getSource()==退出){
系统出口(0);
}
}
私有字符串createPrintText(){
StringBuilder=新的StringBuilder();
builder.append(“
”);
builder.append(“System.out.print()”);
生成器。追加(“
”); append(“System.out.println()”); 生成器。追加(“
”); 返回builder.toString(); } 私有字符串CreateClasseText(){ StringBuilder=新的StringBuilder(); builder.append(“
”);
append(“主类:公共类ClassNameTester{
”; append(“publicstaticvoidmain(String[]args){
”; builder.append(“}
”); builder.append(“}

”); append(“其他类:公共类类名{
”); builder.append(“}”); 生成器。追加(“
”); 返回builder.toString(); } }
我认为
按钮[]
更合适。可能需要映射到
创建