Java Label disappear - java

I want to make a simple agenda but somehow when I run the compiler it doesn't load the label's text and sometimes it does. How do i fix this?
example:
(can't show pictures)
13:00pm(this is what always shows up) Course A
13:30pm Course b
and sometimes it does this:
13:00pm(always shows up) (then nothing)
13:30pm
(please keep it simple cause I am a beginner and from The Netherlands).
CODE:
(I am a beginner so don't look at that copy and paste stuff)
import javax.swing.*;
import java.awt.*;
class P{
public static void main(String [] args){
JFrame frame = new JFrame(" Agenda 10/13/2014");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(620,620);
frame.setResizable(false);
frame.setVisible(true);
JLabel labeltime1 = new JLabel("1:00 - 1:30pm: ");
JLabel labeltime2 = new JLabel("1:30 - 2:00pm: ");
JLabel labeltime3 = new JLabel("2:00 - 2:30pm: ");
JLabel labeltime4 = new JLabel("2:30 - 3:00pm: ");
JLabel labeltime5 = new JLabel("3:00 - 3:30pm: ");
labeltime1.setForeground(Color.red);
labeltime2.setForeground(Color.red);
labeltime3.setForeground(Color.red);
labeltime4.setForeground(Color.red);
labeltime5.setForeground(Color.red);
JLabel space1 = new JLabel("\n");
JLabel space2 = new JLabel("\n");
JLabel space3 = new JLabel("\n");
JLabel space4 = new JLabel("\n");
JLabel space5 = new JLabel("\n");
JPanel timeP = new JPanel();
timeP.setBackground(Color.black);
timeP.setLayout(new BoxLayout(timeP, BoxLayout.Y_AXIS));
timeP.add(space5);
timeP.add(labeltime1);
timeP.add(space1);
timeP.add(labeltime2);
timeP.add(space2);
timeP.add(labeltime3);
timeP.add(space3);
timeP.add(labeltime4);
timeP.add(space4);
timeP.add(labeltime5);
frame.getContentPane().add(BorderLayout.WEST, timeP);
JPanel courses = new JPanel();
courses.setLayout(new BoxLayout(courses, BoxLayout.Y_AXIS));
courses.setBackground(Color.black);
frame.getContentPane().add(BorderLayout.CENTER,courses);
//Enter your course
JLabel course1 = new JLabel(" Course A");
JLabel course2 = new JLabel(" Course B");
JLabel course3 = new JLabel(" Course C");
JLabel course4 = new JLabel(" Course D");
JLabel course5 = new JLabel(" Course E");
course1.setForeground(Color.yellow);
course2.setForeground(Color.yellow);
course3.setForeground(Color.yellow);
course4.setForeground(Color.yellow);
course5.setForeground(Color.yellow);
JLabel space6 = new JLabel("\n");
JLabel space7 = new JLabel("\n");
JLabel space8 = new JLabel("\n");
JLabel space9 = new JLabel("\n");
JLabel space10 = new JLabel("\n");
courses.add(space6);
courses.add(course1);
courses.add(space7);
courses.add(course2);
courses.add(space8);
courses.add(course3);
courses.add(space9);
courses.add(course4);
courses.add(space10);
courses.add(course5);
}
}

moving frame.setVisible(true); to lastline will fix your problem.you need to call setvisible after you add component .or you can call repaint(),revalidate();
import javax.swing.*;
import java.awt.*;
class P{
public static void main(String [] args){
JFrame frame = new JFrame(" Agenda 10/13/2014");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(620,620);
frame.setResizable(false);
//frame.setVisible(true);//don't call this method here
JLabel labeltime1 = new JLabel("1:00 - 1:30pm: ");
JLabel labeltime2 = new JLabel("1:30 - 2:00pm: ");
JLabel labeltime3 = new JLabel("2:00 - 2:30pm: ");
JLabel labeltime4 = new JLabel("2:30 - 3:00pm: ");
JLabel labeltime5 = new JLabel("3:00 - 3:30pm: ");
labeltime1.setForeground(Color.red);
labeltime2.setForeground(Color.red);
labeltime3.setForeground(Color.red);
labeltime4.setForeground(Color.red);
labeltime5.setForeground(Color.red);
JLabel space1 = new JLabel("\n");
JLabel space2 = new JLabel("\n");
JLabel space3 = new JLabel("\n");
JLabel space4 = new JLabel("\n");
JLabel space5 = new JLabel("\n");
JPanel timeP = new JPanel();
timeP.setBackground(Color.black);
timeP.setLayout(new BoxLayout(timeP, BoxLayout.Y_AXIS));
timeP.add(space5);
timeP.add(labeltime1);
timeP.add(space1);
timeP.add(labeltime2);
timeP.add(space2);
timeP.add(labeltime3);
timeP.add(space3);
timeP.add(labeltime4);
timeP.add(space4);
timeP.add(labeltime5);
frame.getContentPane().add(BorderLayout.WEST, timeP);
JPanel courses = new JPanel();
courses.setLayout(new BoxLayout(courses, BoxLayout.Y_AXIS));
courses.setBackground(Color.black);
frame.getContentPane().add(BorderLayout.CENTER,courses);
//Enter your course
JLabel course1 = new JLabel(" Course A");
JLabel course2 = new JLabel(" Course B");
JLabel course3 = new JLabel(" Course C");
JLabel course4 = new JLabel(" Course D");
JLabel course5 = new JLabel(" Course E");
course1.setForeground(Color.yellow);
course2.setForeground(Color.yellow);
course3.setForeground(Color.yellow);
course4.setForeground(Color.yellow);
course5.setForeground(Color.yellow);
JLabel space6 = new JLabel("\n");
JLabel space7 = new JLabel("\n");
JLabel space8 = new JLabel("\n");
JLabel space9 = new JLabel("\n");
JLabel space10 = new JLabel("\n");
courses.add(space6);
courses.add(course1);
courses.add(space7);
courses.add(course2);
courses.add(space8);
courses.add(course3);
courses.add(space9);
courses.add(course4);
courses.add(space10);
courses.add(course5);
frame.setVisible(true);//call here
}
}

Related

How to use JButton in Java

I'm trying to create a hangman game using the Swing class and Graphics class for a APCS class I have.
I created a JButton that would take the guess typed in by the user and either add it to the "misses" or fill in all of the corresponding letters in the word. I have a separate txt file where I pick a random word from.
When I run the program, all of the components show up, but the button does not do anything. I have been working on this for a few days and don't know what I am doing wrong. I have only learnt how to use the Swing class recently, so it may be a stupid mistake for all I know. I have put all of the code down below.
public class Hangman extends JFrame {
DrawPanel panel = new DrawPanel();
Graphics g = panel.getGraphics();
JTextField guess;
JTextField outputWord;
JTextField missesString;
boolean play;
JButton button = new JButton();
String output = "";
String word;
int misses = 0;
public Hangman() {
int again = 0;
String[] dictionary = new String[1000];
In words = new In("words.txt");
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
app.setSize(500, 500);
for (int i = 0; i < 1000; i++) {
dictionary[i] = words.readLine();
}
do {
int num = (int) (Math.random() * 1000);
word = dictionary[num].toLowerCase();
String answer = word;
word = "";
for (int i = answer.length(); i > 0; i--) {
word = word + "*";
}
int length = answer.length();
app.setLayout(new BorderLayout());
JLabel letter = new JLabel();
letter.setFont(new Font("Arial", Font.BOLD, 20));
letter.setText("Enter Your guess");
guess = new JTextField(10);
JLabel output = new JLabel();
output.setFont(new Font("Arial", Font.BOLD, 20));
output.setText("The Word is");
outputWord = new JTextField(30);
outputWord.setText(word);
outputWord.setEditable(false);
JLabel misses = new JLabel();
misses.setFont(new Font("Arial", Font.BOLD, 20));
misses.setText("Misses:");
missesString = new JTextField(52);
missesString.setEditable(false);
button = new JButton("Guess");
button.addActionListener(new ButtonListener());
JPanel panels = new JPanel();
panels.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.add(letter);
panel1.add(guess);
panel1.add(button);
JPanel panel2 = new JPanel();
panel2.add(output);
panel2.add(outputWord);
JPanel panel3 = new JPanel();
panel3.add(misses);
panel3.add(missesString);
app.add(panel1, BorderLayout.NORTH);
app.add(panel2, BorderLayout.EAST);
app.add(panel3, BorderLayout.SOUTH);
app.add(panels);
app.setVisible(true);
again = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
} while (again == JOptionPane.YES_OPTION);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == guess) {
output = "";
}
for (int i = 0; i <= word.length() - 1; i++) {
if (guess.getText().equalsIgnoreCase(word.substring(i, i + 1))) {
output = output.substring(0, i) + guess + output.substring(i + 1, word.length() - 1);
outputWord.setText(output);
} else {
misses++;
missesString.setText(missesString.getText() + guess + " ");
}
}
}
}
}
The components show up perfectly on the screen, it is only the button that is giving me a hard time by not working as expected.

Small trouble with BorderLayout

I'm trying to set up the frame for a pizza order form, and I'm having trouble with the orderPanel and the buttonsPanel. I can either make one or the other show up, but not both? In this current code I'm posting, the buttons are showing, but the textbox/orderPanel isn't. And I've gotten it so the orderPanel DOES show, but then it hides the buttons, which is also not good. I want the buttons at the very bottom and the orderPanel right above it; how can I do that?
class PizzaOrderFrame extends JFrame
{
final private JPanel crustPanel, sizePanel, toppingsPanel, orderPanel, buttonsPanel;
final private TitledBorder crustBorder, sizeBorder, toppingsBorder, orderBorder;
final private JButton quitButton, clearButton, orderButton;
final private JTextArea orderTextArea;
final private JRadioButton thin, regular, deepDish;
final private JCheckBox pepperoni, sausage, bacon, extraCheese, olives, mushrooms;
double smallSizeCost = 8.0;
double mediumSizeCost = 12.0;
double largeSizeCost = 16.0;
double superSizeCost = 20.0;
double toppingsCost = 1.0;
double toppingsCount = 0;
double tax = 0.07;
double orderSubTotal = 0;
double orderTotal = 0;
public PizzaOrderFrame()
{
setTitle("Pizza Order Form");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
double setScreen = screenWidth * .80;
double setScreen3 = screenHeight * .80;
int setScreen2 = (int) setScreen;
int setScreen4 = (int) setScreen3;
setSize(setScreen2, setScreen4);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
crustPanel = new JPanel();
crustBorder = new TitledBorder("Select your crust");
crustBorder.setTitleJustification(TitledBorder.CENTER);
crustBorder.setTitlePosition(TitledBorder.TOP);
crustPanel.setBorder(crustBorder);
thin = new JRadioButton("Thin");
regular = new JRadioButton("Regular");
deepDish = new JRadioButton("Deep-Dish");
ButtonGroup group = new ButtonGroup();
group.add(thin);
group.add(regular);
group.add(deepDish);
crustPanel.add(thin);
crustPanel.add(regular);
crustPanel.add(deepDish);
add(crustPanel, BorderLayout.WEST);
sizePanel = new JPanel();
sizeBorder = new TitledBorder("Select your size");
sizeBorder.setTitleJustification(TitledBorder.CENTER);
sizeBorder.setTitlePosition(TitledBorder.TOP);
sizePanel.setBorder(sizeBorder);
String[] sizeOptions = new String [] {"Small", "Medium", "Large", "Super" };
JComboBox<String> size = new JComboBox<>(sizeOptions);
String selectedSize = (String) size.getSelectedItem();
sizePanel.add(size);
add(sizePanel, BorderLayout.CENTER);
toppingsPanel = new JPanel();
toppingsBorder = new TitledBorder("Select your toppings");
toppingsBorder.setTitleJustification(TitledBorder.CENTER);
toppingsBorder.setTitlePosition(TitledBorder.TOP);
toppingsPanel.setBorder(toppingsBorder);
pepperoni = new JCheckBox("Pepperoni");
sausage = new JCheckBox("Sausage");
extraCheese = new JCheckBox("Extra Cheese");
mushrooms = new JCheckBox("Mushrooms");
olives = new JCheckBox("Olives");
bacon = new JCheckBox("Bacon");
toppingsPanel.add(pepperoni);
toppingsPanel.add(sausage);
toppingsPanel.add(extraCheese);
toppingsPanel.add(mushrooms);
toppingsPanel.add(olives);
toppingsPanel.add(bacon);
add(toppingsPanel, BorderLayout.EAST);
orderPanel = new JPanel();
orderBorder = new TitledBorder("Your Order");
orderBorder.setTitleJustification(TitledBorder.CENTER);
orderBorder.setTitlePosition(TitledBorder.TOP);
orderPanel.setBorder(orderBorder);
orderTextArea = new JTextArea();
JScrollPane orderSP = new JScrollPane(orderTextArea);
orderSP.setPreferredSize( new Dimension( 300, 100 ) );
orderPanel.add(orderSP);
add(orderPanel, BorderLayout.SOUTH);
buttonsPanel = new JPanel();
quitButton = new JButton("Quit");
clearButton = new JButton("Clear");
orderButton = new JButton("Order");
buttonsPanel.add(quitButton);
buttonsPanel.add(clearButton);
buttonsPanel.add(orderButton);
add(buttonsPanel, BorderLayout.PAGE_END);
}
}
According to the JavaDoc for PAGE_END
For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH.
BorderLayout "areas" can only contain one component, so if you call add() multiple times using the same "area", only the last one is shown.
One way to get the layout you want is by creating another Panel, give it a BorderLayout, add orderPanel to the new Panel's NORTH, buttonsPanel to the new Panel's SOUTH, and then add the new Panel to the existing pizzaOrderFrame's SOUTH.

How to append a scrollbar for the window?

How to append a scroll pane for my window?
The program compiles properly, but the scroll pane for the window is not created. I really don't know why this is happening. I defined JScrollPane and even implemented it with scrollPane = new JScrollPane
Where is my mistake?
Below is my code:
import java.awt.*;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
public class RegForm extends JFrame implements ItemListener{
JLabel l0,li,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13;
JButton b1,b2,b3,b4,b5;
JTextField t1,t2,t3,t4,t5,t6,t7,t8;
JTextArea a1,a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton( " Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox (" C ");
JCheckBox cb2 = new JCheckBox (" C++ ");
JCheckBox cb3 = new JCheckBox (" Java ");
JCheckBox cb4 = new JCheckBox (" Oracle ");
JCheckBox cb5 = new JCheckBox (" Android ");
JCheckBox cb6 = new JCheckBox (" iOS ");
JCheckBox cb7 = new JCheckBox (" Web Designing ");
JCheckBox cb8 = new JCheckBox (" .Net ");
JCheckBox cb9 = new JCheckBox (" Same as Contact Address ");
JScrollPane scrollPane = new JScrollPane();
RegForm()
{
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian",Font.ITALIC,20);
l0.setFont(f0);
l0.setBounds(600,10,250,50);
scrollPane.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting",Font.PLAIN,17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10,50,150,30);
scrollPane.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style",Font.PLAIN,12);
l1.setFont(f1);
l1.setBounds(10,70,100,50);
scrollPane.add(l1);
t1 = new JTextField(20);
t1.setBounds(165,85,140,20);
scrollPane.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10,100,150,50);
scrollPane.add(l2);
t2 = new JTextField(20);
t2.setBounds(165,115,140,20);
scrollPane.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15,130,120,50);
scrollPane.add(l3);
t3 = new JTextField(20);
t3.setBounds(165,145,140,20);
scrollPane.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15,160,150,50);
scrollPane.add(l4);
t4 = new JTextField(20);
t4.setBounds(165,175,140,20);
scrollPane.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10,190,100,50);
scrollPane.add(l5);
t5 = new JTextField(20);
t5.setBounds(165,205,140,20);
scrollPane.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10,220,150,50);
scrollPane.add(l6);
t6 = new JTextField(20);
t6.setBounds(165,235,140,20);
scrollPane.add(t6);
l7 = new JLabel(" * DD / MM / YYYY" );
Font f2 = new Font(" Comic Sans MS ",Font.ITALIC,12);
l7.setFont(f2);
l7.setBounds(10,260,150,50);
scrollPane.add(l7);
for(int j=1;j<=31;j++)
dd.addItem(new Integer(j));
dd.setBounds(165,275,47,20);
scrollPane.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212,275,90,20);
scrollPane.add(mm);
mm.addItemListener(this);
for(int i=1990;i<=2016;i++)
yyyy.addItem(new Integer(i));
yyyy.setBounds(302,275,70,20);
scrollPane.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15,290,50,50);
scrollPane.add(l8);
t8 = new JTextField(10);
t8.setBounds(165,305,50,20);
scrollPane.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15,320,120,50);
scrollPane.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165,335,100,20);
scrollPane.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15,360,80,50);
scrollPane.add(l10);
rb1.setBounds(165,365,80,39);
rb2.setBounds(250,365,80,39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
scrollPane.add(rb1);
scrollPane.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15,450,150,50);
scrollPane.add(l11);
cb1.setBounds(165,390,100,50);
scrollPane.add(cb1);
cb2.setBounds(285,390,100,50);
scrollPane.add(cb2);
cb3.setBounds(165,425,100,50);
scrollPane.add(cb3);
cb4.setBounds(285,425,100,50);
scrollPane.add(cb4);
cb5.setBounds(165,460,100,50);
scrollPane.add(cb5);
cb6.setBounds(285,460,100,50);
scrollPane.add(cb6);
cb7.setBounds(165,495,100,50);
scrollPane.add(cb7);
cb8.setBounds(285,495,100,50);
scrollPane.add(cb8);
cb9.setBounds(15,630,200,50);
scrollPane.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15,550,150,50);
scrollPane.add(l12);
a1 = new JTextArea (5,20);
a1.setBounds(165,545,250,80);
scrollPane.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15,675,150,50);
scrollPane.add(l13);
a2 = new JTextArea (5,20);
a2.setBounds(165,680,250,80);
scrollPane.add(a2);
cb9.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource() == yyyy){
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016-y));
t8.setEditable(false);
}
if(cb9.isSelected()){
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae)
{
}
public static void main(String[] args)
{
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(10,10,100,100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(scrollPane);
rf.setContentPane(contentPane);
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
scrollPane.add(l11);
Never add components directly to a scroll pane.
l1.setBounds(10,70,100,50);
Don't use setBounds(...). It is the job of the layout manager to set the size/location of the component.
The basic logic would be:
JPanel panel = new JPanel(); // set your layout manager for the panel.
panel.add( someComponent );
panel.add( anotherComponent );
JScrollPane scrollPane = new JScrollPane( panel );
frame.add( scrollPane );
Read the Swing Tutorial for Swing basics. Every section in the tutorial has working examples. Maybe start with the section on How to Use Scroll Panes.
This works, but you'll need to layout the components better..
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegForm extends JFrame implements ItemListener {
JLabel l0, li, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13;
JButton b1, b2, b3, b4, b5;
JTextField t1, t2, t3, t4, t5, t6, t7, t8;
JTextArea a1, a2;
JComboBox<Integer> dd = new JComboBox<Integer>();
JComboBox<String> mm = new JComboBox<String>();
JComboBox<Integer> yyyy = new JComboBox<Integer>();
JComboBox<String> q = new JComboBox<String>();
JRadioButton rb1 = new JRadioButton(" Male ");
JRadioButton rb2 = new JRadioButton(" Female ");
JCheckBox cb1 = new JCheckBox(" C ");
JCheckBox cb2 = new JCheckBox(" C++ ");
JCheckBox cb3 = new JCheckBox(" Java ");
JCheckBox cb4 = new JCheckBox(" Oracle ");
JCheckBox cb5 = new JCheckBox(" Android ");
JCheckBox cb6 = new JCheckBox(" iOS ");
JCheckBox cb7 = new JCheckBox(" Web Designing ");
JCheckBox cb8 = new JCheckBox(" .Net ");
JCheckBox cb9 = new JCheckBox(" Same as Contact Address ");
JScrollPane scrollPane;
JPanel panel = new JPanel(new GridLayout(0,1));
RegForm() {
l0 = new JLabel("REGISTRATION FORM");
Font f0 = new Font("Algerian", Font.ITALIC, 20);
l0.setFont(f0);
l0.setBounds(600, 10, 250, 50);
panel.add(l0);
li = new JLabel(" * Fields are mandatory");
Font fi = new Font("Arabic TypeSetting", Font.PLAIN, 17);
li.setFont(fi);
li.setForeground(Color.RED);
li.setBounds(10, 50, 150, 30);
panel.add(li);
l1 = new JLabel(" * FirstName: ");
Font f1 = new Font("Bookman Old Style", Font.PLAIN, 12);
l1.setFont(f1);
l1.setBounds(10, 70, 100, 50);
panel.add(l1);
t1 = new JTextField(20);
t1.setBounds(165, 85, 140, 20);
panel.add(t1);
l2 = new JLabel("* Confirm FirstName: ");
l2.setFont(f1);
l2.setBounds(10, 100, 150, 50);
panel.add(l2);
t2 = new JTextField(20);
t2.setBounds(165, 115, 140, 20);
panel.add(t2);
l3 = new JLabel(" Middle Name: ");
l3.setFont(f1);
l3.setBounds(15, 130, 120, 50);
panel.add(l3);
t3 = new JTextField(20);
t3.setBounds(165, 145, 140, 20);
panel.add(t3);
l4 = new JLabel(" Confirm Middle Name: ");
l4.setFont(f1);
l4.setBounds(15, 160, 150, 50);
panel.add(l4);
t4 = new JTextField(20);
t4.setBounds(165, 175, 140, 20);
panel.add(t4);
l5 = new JLabel(" * Sur Name: ");
l5.setFont(f1);
l5.setBounds(10, 190, 100, 50);
panel.add(l5);
t5 = new JTextField(20);
t5.setBounds(165, 205, 140, 20);
panel.add(t5);
l6 = new JLabel(" * Confirm Sur Name: ");
l6.setFont(f1);
l6.setBounds(10, 220, 150, 50);
panel.add(l6);
t6 = new JTextField(20);
t6.setBounds(165, 235, 140, 20);
panel.add(t6);
l7 = new JLabel(" * DD / MM / YYYY");
Font f2 = new Font(" Comic Sans MS ", Font.ITALIC, 12);
l7.setFont(f2);
l7.setBounds(10, 260, 150, 50);
panel.add(l7);
for (int j = 1; j <= 31; j++) {
dd.addItem(new Integer(j));
}
dd.setBounds(165, 275, 47, 20);
panel.add(dd);
dd.addItemListener(this);
mm.addItem("January");
mm.addItem("February");
mm.addItem("March");
mm.addItem("April");
mm.addItem("May");
mm.addItem("June");
mm.addItem("July");
mm.addItem("August");
mm.addItem("September");
mm.addItem("October");
mm.addItem("November");
mm.addItem("December");
mm.setBounds(212, 275, 90, 20);
panel.add(mm);
mm.addItemListener(this);
for (int i = 1990; i <= 2016; i++) {
yyyy.addItem(new Integer(i));
}
yyyy.setBounds(302, 275, 70, 20);
panel.add(yyyy);
yyyy.addItemListener(this);
l8 = new JLabel(" Age: ");
l8.setFont(f1);
l8.setBounds(15, 290, 50, 50);
panel.add(l8);
t8 = new JTextField(10);
t8.setBounds(165, 305, 50, 20);
panel.add(t8);
l9 = new JLabel(" Qualification ");
l9.setFont(f1);
l9.setBounds(15, 320, 120, 50);
panel.add(l9);
q.addItem(" B.Tech ");
q.addItem(" M.Tech ");
q.addItem(" MBA ");
q.addItem(" MCA ");
q.addItem(" Intermediate ");
q.addItem(" SSC ");
q.addItem(" Others ");
q.setBounds(165, 335, 100, 20);
panel.add(q);
q.addItemListener(this);
l10 = new JLabel(" Gender ");
l10.setFont(f1);
l10.setBounds(15, 360, 80, 50);
panel.add(l10);
rb1.setBounds(165, 365, 80, 39);
rb2.setBounds(250, 365, 80, 39);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
panel.add(rb1);
panel.add(rb2);
l11 = new JLabel(" Courses Intrested: ");
l11.setFont(f1);
l11.setBounds(15, 450, 150, 50);
panel.add(l11);
cb1.setBounds(165, 390, 100, 50);
panel.add(cb1);
cb2.setBounds(285, 390, 100, 50);
panel.add(cb2);
cb3.setBounds(165, 425, 100, 50);
panel.add(cb3);
cb4.setBounds(285, 425, 100, 50);
panel.add(cb4);
cb5.setBounds(165, 460, 100, 50);
panel.add(cb5);
cb6.setBounds(285, 460, 100, 50);
panel.add(cb6);
cb7.setBounds(165, 495, 100, 50);
panel.add(cb7);
cb8.setBounds(285, 495, 100, 50);
panel.add(cb8);
cb9.setBounds(15, 630, 200, 50);
panel.add(cb9);
l12 = new JLabel(" Contact Address: ");
l12.setFont(f1);
l12.setBounds(15, 550, 150, 50);
panel.add(l12);
a1 = new JTextArea(5, 20);
a1.setBounds(165, 545, 250, 80);
panel.add(a1);
l13 = new JLabel(" Permenant Address: ");
l13.setFont(f1);
l13.setBounds(15, 675, 150, 50);
panel.add(l13);
a2 = new JTextArea(5, 20);
a2.setBounds(165, 680, 250, 80);
panel.add(a2);
cb9.addItemListener(this);
scrollPane = new JScrollPane(panel);
add(scrollPane);
//add(panel);
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == yyyy) {
int y = (Integer) ie.getItem();
t8.setText(Integer.toString(2016 - y));
t8.setEditable(false);
}
if (cb9.isSelected()) {
a2.setText(a1.getText());
a2.setEditable(false);
}
}
public void actionPerformed(ActionEvent ae) {
}
public static void main(String[] args) {
RegForm rf = new RegForm();
rf.setTitle("Hai Hello");
/*JScrollPane panel = new JScrollPane();
panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel.setBounds(10, 10, 100, 100);
JPanel contentPane = new JPanel();
contentPane.setPreferredSize(new Dimension(1500, 800));
contentPane.add(panel);
rf.setContentPane(contentPane);*/
rf.pack();
rf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rf.setVisible(true);
}
}
Notes:
As mentioned by #camickr, setting the bounds of the components won't work reliably. 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 along with layout padding and borders for white space.
Please try to solve programming errors before you've got to dozens of components! For simple testing of layouts with many components, just add dummy components in a loop. To put that another way: For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
Safer to use logical font names that specific one, e.g. Algerian will only work on systems with that font installed!

JButton and GUI freezes after click

When Eclipse compiles this code everything works fine except the GUI freezes after the user clicks the "add" button. The answer is displayed and the work is shown. Can anyone shine some light on this problem and maybe give me some advice for the layout as well?
import Aritmathic.MathEquation;
public class GUI extends JFrame implements ActionListener{
private JTextField field1;
private JTextField field2;
private JButton add, subtract, multiply, divide;
private JLabel lanswer, label1, label2;
private String input1, input2, sanswer;
private int answer = 0;
JPanel contentPanel, answerPanel;
public GUI(){
super("Calculator");
field1 = new JTextField(null, 15);
field2 = new JTextField(null, 15);
add = new JButton("add");
subtract = new JButton("subtract");
multiply = new JButton("multiply");
divide = new JButton("divide");
lanswer = new JLabel("", SwingConstants.CENTER);
label1 = new JLabel("Value 1:");
label2 = new JLabel("Value 2:");
add.addActionListener(this);
Dimension opSize = new Dimension(110, 20);
Dimension inSize = new Dimension(200, 20);
lanswer.setPreferredSize(new Dimension(200,255));
field1.setPreferredSize(inSize);
field2.setPreferredSize(inSize);
add.setPreferredSize(opSize);
subtract.setPreferredSize(opSize);
multiply.setPreferredSize(opSize);
divide.setPreferredSize(opSize);
contentPanel = new JPanel();
contentPanel.setBackground(Color.PINK);
contentPanel.setLayout(new FlowLayout());
answerPanel = new JPanel();
answerPanel.setPreferredSize(new Dimension(230, 260));
answerPanel.setBackground(Color.WHITE);
answerPanel.setLayout(new BoxLayout(answerPanel, BoxLayout.Y_AXIS));
contentPanel.add(answerPanel);
contentPanel.add(label1); contentPanel.add(field1);
contentPanel.add(label2); contentPanel.add(field2);
contentPanel.add(add); contentPanel.add(subtract); contentPanel.add(multiply); contentPanel.add(divide);
this.setContentPane(contentPanel);
}
public void actionPerformed(ActionEvent e) {
JButton src = (JButton)e.getSource();
if(src.equals(add)){
add();
}
}
private void add(){
input1 = field1.getText();
input2 = field2.getText();
MathEquation problem = new MathEquation(input1, input2);
Dimension lineSize = new Dimension(10, 10);
JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE);
JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE);
JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE);
JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE);
JPanel line5 = new JPanel(); line4.setBackground(Color.WHITE);
JLabel[] sumLabels = problem.getSumLabels();
JLabel[] addend1Labels = problem.getAddend1Labels();
JLabel[] addend2Labels = problem.getAddend2Labels();
JLabel[] carriedLabels = problem.getCarriedLabels();
for(int i = 0; i < carriedLabels.length; i++){
line1.add(carriedLabels[i]);
}
for(int i = 0; i < addend1Labels.length; i++){
line2.add(addend1Labels[i]);
}
for(int i = 0; i < addend2Labels.length; i++){
line3.add(addend2Labels[i]);
}
String answerLine = "__";
for(int i = 0; i < sumLabels.length; i++){
answerLine += "__";
}
line4.add(new JLabel(answerLine));
for(int i = 0; i < sumLabels.length; i++){
line5.add(sumLabels[i]);
}
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(line1);
answerPanel.add(line1);
answerPanel.add(line2);
answerPanel.add(line3);
answerPanel.add(line4);
answerPanel.add(line5);
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
this.setContentPane(answerPanel);
}
}
You have to revalidate your JFrame after you change the content pane.
After
this.setContentPane(answerPanel);
add
revalidate();
Java docs about revalidate()
Your add() method should now look like:
private void add(){
input1 = field1.getText();
input2 = field2.getText();
MathEquation problem = new MathEquation(input1, input2);
Dimension lineSize = new Dimension(10, 10);
JPanel line1 = new JPanel(); line1.setBackground(Color.WHITE);
JPanel line2 = new JPanel(); line2.setBackground(Color.WHITE);
JPanel line3 = new JPanel(); line3.setBackground(Color.WHITE);
JPanel line4 = new JPanel(); line4.setBackground(Color.WHITE);
JPanel line5 = new JPanel(); line4.setBackground(Color.WHITE);
JLabel[] sumLabels = problem.getSumLabels();
JLabel[] addend1Labels = problem.getAddend1Labels();
JLabel[] addend2Labels = problem.getAddend2Labels();
JLabel[] carriedLabels = problem.getCarriedLabels();
for(int i = 0; i < carriedLabels.length; i++){
line1.add(carriedLabels[i]);
}
for(int i = 0; i < addend1Labels.length; i++){
line2.add(addend1Labels[i]);
}
for(int i = 0; i < addend2Labels.length; i++){
line3.add(addend2Labels[i]);
}
String answerLine = "__";
for(int i = 0; i < sumLabels.length; i++){
answerLine += "__";
}
line4.add(new JLabel(answerLine));
for(int i = 0; i < sumLabels.length; i++){
line5.add(sumLabels[i]);
}
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(line1);
answerPanel.add(line1);
answerPanel.add(line2);
answerPanel.add(line3);
answerPanel.add(line4);
answerPanel.add(line5);
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
answerPanel.add(new JLabel(" "));
this.setContentPane(answerPanel);
this.revalidate();//
}
When I tested this it works. By works I mean did not freeze and set the content pane to the answerPanel.

How to get rid of spaces in the string of my Calc program

I was wondering how I would be able to account for spaces in my Calculator program. Right now it is set up to only work if there is a space after every value. I want it to be able to work if there is multiple spaces as well. I tried doing something (in my action performed, under the '=' sign test), but it didn't really work. So wanted to know how I could make it account for multiple spaces (like if there are more than one space before the next value, then it should recognize that that is the case, and delete the extra spaces). Thanks
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class GUI extends JFrame implements ActionListener
{
JPanel buttonPanel, topPanel, operationPanel;
JTextField display = new JTextField(20);
doMath math = new doMath();
String s = "";
String b= "";
//int counter;
JButton Num1;
JButton Num2;
JButton Num3;
JButton Num4;
JButton Num5;
JButton Num6;
JButton Num7;
JButton Num8;
JButton Num9;
JButton Num0;
JButton Add;
JButton Sub;
JButton Mult;
JButton Div;
JButton Eq;
JButton Clr;
JButton Space;
public GUI()
{
super("Calculator");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout (2,1));
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4));
buttonPanel.add(Num1 = new JButton("1"));
buttonPanel.add(Num2 = new JButton("2"));
buttonPanel.add(Num3 = new JButton("3"));
buttonPanel.add(Num4 = new JButton("4"));
buttonPanel.add(Num5 = new JButton("5"));
buttonPanel.add(Num6 = new JButton("6"));
buttonPanel.add(Num7 = new JButton("7"));
buttonPanel.add(Num8 = new JButton("8"));
buttonPanel.add(Num9 = new JButton("9"));
buttonPanel.add(Num0 = new JButton("0"));
buttonPanel.add(Clr = new JButton("C"));
buttonPanel.add(Eq = new JButton("="));
buttonPanel.add(Add = new JButton("+"));
buttonPanel.add(Sub = new JButton("-"));
buttonPanel.add(Mult = new JButton("*"));
buttonPanel.add(Div = new JButton("/"));
buttonPanel.add(Space = new JButton("Space"));
Num1.addActionListener(this);
Num2.addActionListener(this);
Num3.addActionListener(this);
Num4.addActionListener(this);
Num5.addActionListener(this);
Num6.addActionListener(this);
Num7.addActionListener(this);
Num8.addActionListener(this);
Num9.addActionListener(this);
Num0.addActionListener(this);
Clr.addActionListener(this);
Eq.addActionListener(this);
Add.addActionListener(this);
Sub.addActionListener(this);
Mult.addActionListener(this);
Div.addActionListener(this);
Space.addActionListener(this);
topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
topPanel.add(display);
add(mainPanel);
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
String text = source.getText();
int counter = 0;
if (text.equals("="))
{
doMath math = new doMath();
for(int i = 0; i <b.length(); i++)
{
String c = b.substring(i,(i+1));
String d = b.substring((i+1),(i+2));
String temp = "";
if( c.equals(" ") && c.equals(d))
{
temp = b.substring(0,i)+(b.substring(i+1));
b = temp;
}
}
int result = math.doMath1(b);
String answer = ""+result;
display.setText(answer);
}
else if(text.equals("Space"))
{
counter ++;
if(counter <2)
{
b+= " ";
display.setText(b);
}
else
{
b+="";
display.setText(b);
}
}
else if (text.equals("C"))
{
b = "";
display.setText(b);
}
else
{
b += (text);
display.setText(b);
}
counter = 0;
}
}
This method requires knowledge of regular expressions. For each string, simply replace any instance of multiple consecutive spaces with a single space. In other words, using regular expressions, you would replace "\\s+", which means "more than one space," with " ", which is just the space character.
Example:
String c = ...
c = c.replaceAll("\\s+", " ");
String str = "your string";
Pattern _pattern = Pattern.compile("\\s+");
Matcher matcher = _pattern.matcher(str);
str = matcher.replaceAll(" ");
Instead of using a substring, use a Scanner which takes a String as a parameter.
Or you can disable the "Space" button after it's pressed, enabling it back, when a different button is pressed.
If b is not empty you could try looking at the previous character using charAt and if it is a space do nothing.

Categories

Resources