Reputation: 25
Hi am new to java and have been trying to create a form for a college project. I am in early development stages and i have my text fields working but my label is not visable. Also my text areas are far to large stretching the whole of my Jframe. Here is my currant Code This Jframe opens automaticly when the User Loggs in the the previous class named (java.java.) This class is called (AddProperty.java)
package Main;
/**
*
* @author Graeme
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
public class AddProperty
{
public void gui()
{
JFrame frame = new JFrame("AddPropertyFrame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel HouseNumber = new JLabel();
panel.add(HouseNumber);
JTextField HouseNumber1 = new JTextField();
panel.add(HouseNumber1);
JLabel HousePrice = new JLabel();
panel.add(HousePrice);
JTextField HousePrice1 = new JTextField();
panel.add(HousePrice1);
JLabel HouseType = new JLabel();
panel.add(HouseType);
JTextField HouseType1 = new JTextField();
panel.add(HouseType1);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new Action());
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e)
{
JFrame frame2 = new JFrame("Submitted");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You Have Submitted a New Property");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
}
Thanks, any advice will be much appreciated.
Upvotes: 1
Views: 702
Reputation: 4312
The reason your JLabels are not being shown is because it is given no value to show.
try adding a value when calling its constructor, for example;
new JLabel("House Nummber");
Upvotes: 1
Reputation: 168825
See the comments in the code.
/**
*
* @author Graeme
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;
public class AddProperty
{
public void gui()
{
JFrame frame = new JFrame("AddPropertyFrame");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// having to set sizes of components is rare, and often a sign
// of problems with layouts.
//frame.setSize(800,600);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
// make it big like the original
panel.setBorder(new EmptyBorder(100,20,100,20));
frame.add(panel);
//panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel HouseNumber = new JLabel("A");
panel.add(HouseNumber);
JTextField HouseNumber1 = new JTextField(10);
panel.add(HouseNumber1);
JLabel HousePrice = new JLabel("B");
panel.add(HousePrice);
JTextField HousePrice1 = new JTextField(10);
panel.add(HousePrice1);
JLabel HouseType = new JLabel("C");
panel.add(HouseType);
JTextField HouseType1 = new JTextField(5);
panel.add(HouseType1);
JButton submit = new JButton("Submit");
panel.add(submit);
submit.addActionListener(new Action());
// tell the GUI to assume its natural (minimum) size.
frame.pack();
}
static class Action implements ActionListener{
public void actionPerformed (ActionEvent e)
{
// this should probably be a modal JDialog or JOptionPane.
JFrame frame2 = new JFrame("Submitted");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("You Have Submitted a New Property");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
AddProperty addProperty = new AddProperty();
addProperty.gui();
}
});
}
}
Upvotes: 1
Reputation: 2597
I believe the JLabels are not visible because they don't have something to display in them...
try putting a value for the JLabel when you call its constructor... this is just my guess though...
new JLabel("House Nummber");
Upvotes: 1