import java.text.*;
import java.util.Date;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.applet.*;
import java.util.*;
public class FinalProject extends JFrame
{
private JFrame mainFrame;
private JButton calculateButton;
private JButton exitButton;
private JButton saveButton;
private JTextField lengthField;
private JTextField widthField;
private JTextField depthField;
private JTextField volumeField;
private JLabel lengthLabel;
private JLabel widthLabel;
private JLabel depthLabel;
private JLabel volumeLabel;
private JLabel nameLabel;
Image splashimage;
class SplashScreen extends JWindow
{
public SplashScreen(String FinalProject, Frame f)
{
super(f);
JLabel l = new JLabel(new ImageIcon("splashimage.jpg"));
getContentPane().add(l, BorderLayout.CENTER);
pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSiz...
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width/2 - (labelSize.width/2),
screenSize.height/2 - (labelSize.height/2));
setVisible(true);
screenSize = null;
labelSize = null;
}
}
public FinalProject()
{
mainFrame = new JFrame("Swimming Pool Volume Calculator");
calculateButton = new JButton("Calculate volume");
exitButton = new JButton("Exit");
saveButton = new JButton("Save File");
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
depthLabel = new JLabel("Enter the average depth of the pool: ");
volumeLabel = new JLabel("Swimming pool volume is:");
nameLabel = new JLabel("Enter Contractor Name: ");
lengthField = new JTextField(5);
widthField = new JTextField(5);
depthField = new JTextField(5);
volumeField = new JTextField(5);
Container c = mainFrame.getContentPane();
c.setLayout(new FlowLayout());
c.add(lengthLabel);
c.add(lengthField);
c.add(widthLabel);
c.add(widthField);
c.add(depthLabel);
c.add(depthField);
c.add(volumeLabel);
c.add(volumeField);
c.add(calculateButton);
c.add(exitButton);
c.add(saveButton);
c.add(nameLabel);
calculateButton.setMnemonic('C');
exitButton.setMnemonic('x');
saveButton.setMnemonic('s');
mainFrame.setSize(300,200);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
calculateButtonHandler chandler = new calculateButtonHandler();
calculateButton.addActionListener(ch...
ExitButtonHandler ehandler = new ExitButtonHandler();
exitButton.addActionListener(ehandle...
FocusHandler fhandler = new FocusHandler();
lengthField.addFocusListener(fhandle...
widthField.addFocusListener(fhandler...
depthField.addFocusListener(fhandler...
volumeField.addFocusListener(fhandle...
mainFrame.show();
}
class calculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat num = new DecimalFormat(",###.##");
double length, width, AverageDepth, volume;
String intString;
intString = lengthField.getText();
if (intString.equals(""))
{
intString = "0";
lengthField.setText("0");
}
length = Double.parseDouble(intString);
intString = widthField.getText();
if (intString.equals(""))
{
intString = "0";
widthField.setText("0");
}
width = Double.parseDouble(intString);
intString = depthField.getText();
if (intString.equals(""))
{
intString = "0";
depthField.setText("0");
}
AverageDepth = Double.parseDouble(intString);
volume = length * width * AverageDepth;
volumeField.setText(num.format(volu...
setLayout(new BorderLayout());
setBackground(Color.white);
}
}
class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class nameLabel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (checkFields())
{
try
{
output.writeUTF(saveButton.getText())...
ouput.writeUTF(nameLabel.getText());
JOptionPane.showMessageDialog(null, "The file has been saved.",
"Submission Successful", JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException c)
{
System.exit(1);
}
clearFields();
}
}
}
class FocusHandler implements FocusListener
{
public void focusGained(FocusEvent e)
{
if(e.getSource() == lengthField || e.getSource() == widthField || e.getSource() == depthField)
{
volumeField.setText("");
}
else if(e.getSource() == volumeField)
{
volumeField.setNextFocusableCompon...
calculateButton.grabFocus();
}
}
public void focusLost(FocusEvent e)
{
if(e.getSource() == depthField)
{
widthField.setNextFocusableCompone...
}
}
}
public static void main(String args[])
{
new FinalProject();
FileOutputStream fout;
try
{
fout = new FileOutputStream ("myfile.txt");
new PrintStream(fout).println ("results");
fout.close();
}
catch (IOException e)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}
Help with Java Application. My splash screen will not display and I can't get my save button to work.?
A simple version with splash screen %26amp; working save button (you will need Java SE 6. To run the program, use this command: "java -splash:splash.jpg MeasurementsPanel", where "splash.jpg" is your picture for the splash screen. If you don't see the splash screen: make sure it's in the right folder) Mail me at anthony_ve@hotmail.com, if you have any more questions about this code or your program.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.Locale;
import javax.swing.*;
public class MeasurementsPanel extends JPanel implements ActionListener {
private static final long TIME_TO_SHOW_SPLASH_SCREEN = 2000;
private static final String SAVE_FILE = "save.txt";
private static final String CALCULATE_CMD = "Calculate";
private static final String SAVE_CMD = "Save";
private JFormattedTextField lengthTxt;
private JFormattedTextField widthTxt;
private JFormattedTextField depthTxt;
private JFormattedTextField volumeTxt;
public MeasurementsPanel() {
createPanelGUI();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals( CALCULATE_CMD))
calculate();
else if(e.getActionCommand().equals( SAVE_CMD))
save();
}
protected void calculate() {
double volume = getValue(lengthTxt)* getValue(widthTxt)* getValue(depthTxt);
volumeTxt.setValue(volume);
}
protected double getValue(JFormattedTextField txtField) {
return ((Number) txtField.getValue()).doubleValue();
}
protected void save() {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(SAVE_FILE));
out.write("Length: "+getValue(lengthTxt));
out.newLine();
out.write("Width: "+getValue(widthTxt));
out.newLine();
out.write("Depth: "+getValue(depthTxt));
out.newLine();
out.write("Volume: "+getValue(volumeTxt));
out.newLine();
JOptionPane.showMessageDialog(this, "Successfully saved to "+SAVE_FILE, "Information", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e) { e.printStackTrace(); }
finally {
if(out != null)
try { out.close(); } catch(IOException e) { e.printStackTrace(); }
}
}
private void createPanelGUI() {
setLayout(new GridLayout(0,2,5,5));
add(new JLabel("Length: "));
lengthTxt = getNumberTxtFld();
add(lengthTxt);
add(new JLabel("Width: "));
widthTxt = getNumberTxtFld();
add(widthTxt);
add(new JLabel("Depth: "));
depthTxt = getNumberTxtFld();
add(depthTxt);
add(new JLabel("Volume: "));
volumeTxt = getNumberTxtFld();
volumeTxt.setEditable(false);
add(volumeTxt);
addBtn(CALCULATE_CMD,'c');
addBtn(SAVE_CMD,'s');
}
private JFormattedTextField getNumberTxtFld() {
JFormattedTextField txt = new JFormattedTextField( NumberFormat.getInstance());
txt.setValue(0);
return txt;
}
private void addBtn(String cmd, char c) {
JButton btn = new JButton(cmd);
btn.setMnemonic(c);
btn.addActionListener(this);
add(btn);
}
public static void main(String[] args) {
Locale.setDefault(Locale.US);
waitAndCloseSplashScreen();
showMainFrame();
}
private static void waitAndCloseSplashScreen() {
if(SplashScreen.getSplashScreen() != null) {
try{
Thread.sleep( TIME_TO_SHOW_SPLASH_SCREEN);
}
catch(InterruptedException e) {}
SplashScreen.getSplashScreen().close() ;
}
}
private static void showMainFrame() {
JFrame mainFrame = new JFrame("Volume Calculator");
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
mainFrame.setContentPane(new MeasurementsPanel());
main.pack();
mainFrame.setVisible(true);
mainFrame.toFront();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment