This is a Java GUI sample. Regarding the Java GUI, there are some samples that are completely described in the main class, and I remember having difficulty finding a practical usage, so I implemented a group of GUI dialogs separately from the main class. Share here.
1.swing.JOptionPane InfoDialog
This is a general-purpose dialog.
main.java
public class Main {
public static void main(String[] args) {
InfoDialog.showDialog("Please select a file(.xlsx)。");
}
}
InfoDialog.java
import javax.swing.JOptionPane;
class InfoDialog {
public static void showDialog(String msg) {
JOptionPane.showMessageDialog(null, msg,"Take it",JOptionPane.INFORMATION_MESSAGE);
}
}
When using this method, specify the character string you want to display in the dialog for the argument String. Here, "Please select a file (.xlsx)." I think the 2nd-4th arguments of the showMessageDialog method are understandable, but the first one takes parentComponent as an argument. I couldn't figure out what the ~~ parentComponent was, but ~~ basic null should be fine.
If you want to select "Yes" or "No", use the showConfirmDialog method instead of showMessageDialog.
ErrorDialog Just change the 4th argument of ShowMessageDialog from INFORMATION_MESSAGE, to ERROR_MESSAGE, and nothing else will change.
main.java
public class Main {
public static void main(String[] args) {
InfoDialog.ErrorDialog(".xlsx is unspecified. I'm done.");
}
}
ErrorDialog.java
import javax.swing.JOptionPane;
class ErrorDialog {
public static void showDialog(String msg) {
JOptionPane.showMessageDialog(null, msg,"Take it",JOptionPane.ERROR_MESSAGE);
}
}
2.awt.FileDialog
A dialog that lets the user select a file.
Main.java
public class Main {
public static void main(String[] args) {
new WindowTest();
File file = new File(WindowTest.dir + WindowTest.fileName);
}
}
WindowTest.java
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.WindowListener;
class WindowTest extends Frame implements WindowListener {
static String dir;
static String fileName;
WindowTest() {
FileDialog fileDialog = new FileDialog(this);
fileDialog.setVisible(true);
this.dir = fileDialog.getDirectory();
this.fileName = fileDialog.getFile();
if (fileName == null) {
ErrorDialog.showDialog(".xlsx is unspecified. I'm done.");
System.exit(0);
}
}
public void windowActivated(java.awt.event.WindowEvent e) {
}
public void windowClosed(java.awt.event.WindowEvent e) {
}
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(1);
}
public void windowDeactivated(java.awt.event.WindowEvent e) {
}
public void windowDeiconified(java.awt.event.WindowEvent e) {
}
public void windowIconified(java.awt.event.WindowEvent e) {
}
public void windowOpened(java.awt.event.WindowEvent e) {
}
}
that's all.
Recommended Posts