0

Trying get a fileViewer method that will allow user to choose photo from file and when Button is used will pull up the dialog box. My close button obviously works as code is simple. With the enlarge picture button I did wonder if I could declare a constant and use it to scale photo by the constant. Wasn't sure if that was even a thing.

Below is my code:

package masdfas.fd;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BetterWork extends Frame implements ActionListener {

    Button browseButton = new Button("Browse");
    Button enlargeButton = new Button("Enlarge");
    Button closeButton = new Button("Quit");
    Label  onlyLabel = new Label ("Welcome to my Program");

    public static void main(String[] args) {
         BetterWork BW = new BetterWork();
         BW.setVisible(true);
         BW.setSize(600, 500);
    }

    public BetterWork() {
         super ("program");
         setLayout(null);
         onlyLabel.setBounds(250, 50, 150, 50);
         add(onlyLabel);
         browseButton.setBounds(30, 50, 50, 50);

         add( browseButton);

         enlargeButton.setBounds(30, 150, 50, 50);

         add(enlargeButton);
         closeButton.setBounds(30, 250, 50, 50);
         add(closeButton);

         browseButton.addActionListener(this);
         enlargeButton.addActionListener(this);
         closeButton.addActionListener(this);
         addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                   System.exit(0);
              }
         });
   }

   public void actionPerformed(ActionEvent AE) {
    if (AE.getSource() == browseButton) {
        browse();
    } else if (AE.getSource() == enlargeButton) {
        enlarge();
    } else if (AE.getSource() == closeButton) {
        close();
      }
    }

   private void close() {
      System.exit(0);
   }

   private void enlarge() {
    // TODO Auto-generated method stub
   }

   private void browse() {
    // TODO Auto-generated method stub
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SamArm
  • 9
  • 2
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Jul 17 '18 at 23:17
  • .. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Jul 17 '18 at 23:17
  • *"With the enlarge picture button I did wonder if I could declare a constant and use it to scale photo by the constant. Wasn't sure if that was even a thing."* Yes, it's 'a thing'. Their are probably a dozen ways to scale an image, the details of which depend on how it is being loaded and displayed. Given the code is skeleton code which does not detail either part, it's hard to advise further. To expand on point (1) note that a Swing `JLabel` can display images, whereas the AWT based `Label` cannot. Most Swing text components also support HTML (makes it *very* easy to resize images!). – Andrew Thompson Jul 17 '18 at 23:20

0 Answers0