dimanche 19 avril 2015

java Fields not aligning right with GridBagLayout, unable to implement background image

So I've been working away, learning to write in java, and I'm getting close to a usable interface, but I'm having 2 problems. When I try to implement the CloudPanel that I overrode the paint component in, it won't let me set a gridbaglayout in that panel. To try to get everything else working while I figure out why I can't get that done, I created a new basic JPanel to fill the form with. It's almost right, but its forcing the Price JTextField off the side of the screen or just not showing it, while the label for it is in the center of it's line instead of at the left as it should be. Any clue what I'm doing wrong here?



package newprovider;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;

/*@Michael Christopher, author/owner */
public class NewProvider {
/** @param args the command line arguments*/

public static class CloudPanel extends JPanel {
//paint background
String backgroundPath="/images/CloudBack.png";
@Override
public void paintComponent(Graphics cloud){
super.paintComponent(cloud);
Image back = new ImageIcon(getClass().getResource(backgroundPath)).getImage();
cloud.drawImage(back, 0,0, this);
}
}
public static void createGUI() {
//build main frame
JFrame mainFrame = new JFrame();
mainFrame.setTitle("New Provider Interface");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create the MenuBar - needs
JMenuBar maintenanceMenuBar = new JMenuBar ();
maintenanceMenuBar.setOpaque(true);
maintenanceMenuBar.setBackground(new Color(176,224,230));
maintenanceMenuBar.setPreferredSize(new Dimension(400, 20));

//create variables
int addProviderCheck;
String[] options = {"Yes","No"};
String[] newExit = {"Exit", "New"};

//create labels and JTextFields
JLabel labelID = new JLabel ("New ProviderID");
final JTextField textID = new JTextField("providerID ", 10);
JLabel labelName = new JLabel ("New Provider Name");
final JTextField textName = new JTextField("Provider Name ", 20);
JLabel labelPrice = new JLabel ("New Provider Price");
final JTextField textPrice = new JTextField ("Price ", 5);
//make Submit, Clear, & Exit buttons
JButton submit = new JButton("Submit");
JButton clear = new JButton("Clear");
JButton exit = new JButton("Exit");

//build main Panel
CloudPanel fieldBox = new CloudPanel();
JPanel buttonBar = new JPanel(new GridBagLayout());
buttonBar.setBackground(Color.red);
buttonBar.setSize(40, 300);
//GridBag constraints
GridBagConstraints bGUI = new GridBagConstraints();
//Adding buttons to Panel
bGUI.insets = new Insets(5,20,5,20);
bGUI.gridx = 0;
bGUI.gridy = 1;
buttonBar.add(submit,bGUI);
bGUI.insets = new Insets(5,20,5,20);
bGUI.gridx = 1;
bGUI.gridy = 1;
buttonBar.add(clear,bGUI);
bGUI.insets = new Insets(5,20,5,20);
bGUI.gridx = 2;
bGUI.gridy = 1;
buttonBar.add(exit,bGUI);

//Creation of Login Panel
JPanel Login = new JPanel(new GridBagLayout());
//Login.setBackground(Color.white);
//GridBag Adding Labels and Text to Login with Constraints
GridBagConstraints lGUI = new GridBagConstraints();

lGUI.insets = new Insets(20,05,20,05);
lGUI.gridx = 0;
lGUI.gridy = 1;
Login.add(labelID,lGUI);
lGUI.insets = new Insets(20,05,20,05);
lGUI.gridx = 1;
lGUI.gridy = 1;
Login.add(textID,lGUI);
lGUI.insets = new Insets(20,05,20,05);
lGUI.gridx = 0;
lGUI.gridy = 2;
Login.add(labelName,lGUI);
lGUI.gridx = 1;
lGUI.gridy = 2;
Login.add(textName,lGUI);

lGUI.gridx = 0;
lGUI.gridy = 3;
Login.add(labelPrice,lGUI);
lGUI.gridx = 1;
lGUI.gridy = 3;
Login.add(labelPrice,lGUI);
//add panels to frame
mainFrame.add(Login, BorderLayout.CENTER);
mainFrame.add(buttonBar, BorderLayout.SOUTH);

//focusListeners for ID Field
textID.addFocusListener(new FocusListener(){

@Override
public void focusGained(FocusEvent e) {
textID.setText("");
}

@Override
public void focusLost(FocusEvent e) {
if (!e.isTemporary()) {
String checkID = textID.getText();
Boolean validID = false;
if (checkID.isEmpty()){
JOptionPane.showMessageDialog(mainFrame, "Please enter a valid ID.");
SwingUtilities.invokeLater(new FocusGrabber(textID));
}
else while (validID = false){

}
}
}
});

//ActionListener for Submit button
submit.addActionListener((ActionEvent e) -> {
String Name;
Name = textName.getText();
int ID = Integer.parseInt(textID.getText());
double Price = Double.parseDouble(textPrice.getText());
int submitPress = JOptionPane.showOptionDialog(null,
"ProviderID: " + ID + "\n" + "Provider Name: " + Name +"\n"
+ "Provider Price: " + Price, "Please Verify Content",
JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,
null, options,options[0]);
if (submitPress <= 0) {
//Store Displayed data
int providerID = ID;
String providerName = Name;
Double providerPrice = Price;
System.out.println(providerID);
System.out.println(providerName);
System.out.println(providerPrice);//add method to store println()s to database
//Popup Confirm dialog to reset fields or exit
int confirmNew = JOptionPane.showOptionDialog(null, "New Provider Confirmed\n" + "Would you like to exit the module or add another provider?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, newExit, newExit[0]);
if (confirmNew <=0) {
mainFrame.setVisible(false);
mainFrame.dispose();
} else if (confirmNew > 0) {
textID.setText("providerID");
textName.setText("Provider Name");
textPrice.setText("Price");
}
}
else if (submitPress > 0) {
textID.setText("providerID");
textName.setText("Provider Name");
textPrice.setText("Price"); }
});
//ActionListener for clear button
clear.addActionListener((ActionEvent e) -> {
textID.setText("providerID");
textName.setText("Provider Name");
textPrice.setText("Price");
});
//ActionListener for Exit Button
exit.addActionListener((ActionEvent e) -> {
mainFrame.setVisible(false);
mainFrame.dispose();
});
//verify intent to add new provider to system before continuing
addProviderCheck = JOptionPane.showOptionDialog(null,
"This will add a new service provider to the database.\n"
+ "Are you sure you wish to continue?","Please Verify Intent", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, options,options[0]);
if (addProviderCheck <= 0) {
//Display Window
mainFrame.pack();
mainFrame.setSize(400, 400);
mainFrame.setVisible(true);}
else { //else close app
mainFrame.setVisible(false);
mainFrame.dispose();
}

// mainFrame.setVisible(true);
}
public static void main(String[] args){
//draw and show the GUI
javax.swing.SwingUtilities.invokeLater(() -> {
createGUI();
});
//store new provider data
}
}

Aucun commentaire:

Enregistrer un commentaire