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

时间:2015-05-22 18:52:24

标签: java swing joptionpane

我的程序应该有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);
  }
}

1 个答案:

答案 0 :(得分:0)

我进行了更改以清理Java Helper GUI并允许您格式化JOptionPane对话框中的信息。

这是Java Helper GUI。

Java Helper GUI

这是Classes JOptionPane。

JOptionPane Classes

我修改了 JavaHelperTester 类,以包含对SwingUtilities invokeLater方法的调用。此方法将您的Swing组件的创建和使用放在Event Dispatch thread (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();
    }

}