繁体   English   中英

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

[英]Resize JOptionPane Dialog and lines in a dialogs

我的程序应该具有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 Helper GUI。

Java Helper GUI

这是类JOptionPane。

JOptionPane类

我修改了JavaHelperTester类,以包含对SwingUtilities invokeLater方法的调用。 此方法将创建和使用Swing组件放在事件调度线程(EDT)上 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字符串。 您只能使用HTML 3.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();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM