3 Jpanel in JFrame, my button is not visible - java

I have a JFrame with three JPanel objects. I have a problem with two of the panels. I can see in my frame, the panel JPanelProduit with object but for the panel, JPanelInformations and JPanelVentes, I see nothing. Where is my error?
My Code
package IHM;
import javax.swing.*;
import Donnees.Categories;
import Donnees.CategoriesCellRenderer;
import Donnees.CategoriesListModel;
import Donnees.Marques;
import Donnees.MarquesCellRenderer;
import Donnees.MarquesListModel;
import Donnees.Produits;
import Donnees.ProduitsCellRenderer;
import Donnees.ProduitsListModel;
import Fabriques.FabCategories;
import Fabriques.FabMarques;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Fenetre {
static Connection conn;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.hsqldb.jdbcDriver");
conn=DriverManager.getConnection("jdbc:hsqldb:file:BDD/bdd","sa","");
FabCategories.getInstance().demarrerConnexion(conn);
FabMarques.getInstance().demarrerConnexion(conn);
JFrame f = new JFrame("Gestion des Produits");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1,2,3, 3));
JPanelProduit jPanelProduit = new JPanelProduit();
JPanelInformations jPanelInformations = new JPanelInformations();
JPanelVentes jPanelVentes = new JPanelVentes();
jPanelProduit.setBackground(Color.GREEN);
jPanelProduit.setBackground(Color.YELLOW);
jPanelVentes.setBackground(Color.PINK);
f.add(jPanelProduit);
f.add(jPanelInformations);
f.add(jPanelVentes);
f.setSize(700,700);
f.pack();
f.setVisible(true);
}
}
class JPanelProduit extends JPanel {
public JPanelProduit() throws SQLException {
setLayout(new GridLayout(5,2,5,5));
String labelCat = "Categories";
String labelMark = "Marques";
String labelProd = "Produits";
JList<Categories> listCategories= new JList<Categories> ();
JList<Marques> listMarques= new JList<Marques> ();
JList<Produits> listProduits= new JList<Produits> ();
JScrollPane listCategoriesScrollPane = new JScrollPane (listCategories);
add(new JLabel(labelCat));
add(new JScrollPane(listCategoriesScrollPane));
listCategories.setCellRenderer(new CategoriesCellRenderer());;
listCategories.setModel(new CategoriesListModel());
add(new JLabel(labelMark));
JScrollPane listMarquesScrollPane = new JScrollPane (listMarques);
add(new JScrollPane(listMarquesScrollPane));
listMarques.setCellRenderer(new MarquesCellRenderer());
listMarques.setModel(new MarquesListModel());
add(new JLabel(labelProd));
JScrollPane listProduitScrollPane = new JScrollPane (listProduits);
add(new JScrollPane(listProduitScrollPane));
//listProduits.setCellRenderer(new ProduitsCellRenderer());
//listProduits.setModel(new ProduitsListModel());
}
}
class JPanelInformations extends JPanel {
public JPanelInformations() {
JPanel PanelInformation = new JPanel();
setLayout(new GridLayout(7,1,5,5));
JLabel labelInfo = new JLabel ("INFORMATION");
JLabel labelPrix = new JLabel ("Prix");
JLabel labelDesc = new JLabel ("Description");
JLabel labelQuant = new JLabel ("Quantite");
JTextField fieldPrix = new JTextField (20);
JTextArea fieldDesc = new JTextArea (20, 20);
JTextField fieldQuantite = new JTextField (20);
PanelInformation.add(labelInfo);
PanelInformation.add(labelPrix);
PanelInformation.add(fieldPrix);
PanelInformation.add(labelDesc);
PanelInformation.add(fieldDesc);
PanelInformation.add(labelQuant);
PanelInformation.add(fieldQuantite);
}
}
class JPanelVentes extends JPanel {
public JPanelVentes() {
JPanel PanelVentes = new JPanel();
setLayout(new GridLayout());
JLabel labelVendre = new JLabel ("VENDRE");
JLabel labelQte = new JLabel ("Quantite");
JLabel labelPromo = new JLabel ("Promotion");
JLabel labelTot = new JLabel ("Total");
JTextField fieldQte = new JTextField (20);
JTextField fieldPromoEuros = new JTextField (20);
JTextField fieldPromoPourcent = new JTextField (20);
JTextField fieldTotal = new JTextField (20);
PanelVentes.add (labelVendre);
PanelVentes.add (labelQte);
PanelVentes.add (fieldQte);
PanelVentes.add (labelPromo);
PanelVentes.add (fieldPromoEuros);
PanelVentes.add (fieldPromoPourcent);
PanelVentes.add (labelTot);
PanelVentes.add (fieldTotal);
}
}

The JPanelInformations in the constructor creates a local instance JPanel PanelInformation = new JPanel(); which is not added to main panel.
You should either add it to this or get rid of it at all and add all the labels to this directly.
The same with JPanelVentes

Inside JPanelInformations class you are creating new JPanel class PanelInformation and adding other elements in it. You should just call add() as JPanelInformations already extends JPanel and you are creating instance of JPanelVentes class.
So follow same JPanel logic you have used in JPanelProduit class.
Same goes for JPanelVentes as well.
Also take care of naming conventions. Makes life easy
JPanel panelVentes = new JPanel();

Try out this:
public JPanelInformations() {
//JPanel PanelInformation = new JPanel(); remove new instance of panel
setLayout(new GridLayout(7,1,5,5));
JLabel labelInfo = new JLabel ("INFORMATION");
JLabel labelPrix = new JLabel ("Prix");
JLabel labelDesc = new JLabel ("Description");
JLabel labelQuant = new JLabel ("Quantite");
JTextField fieldPrix = new JTextField (20);
JTextArea fieldDesc = new JTextArea (20, 20);
JTextField fieldQuantite = new JTextField (20);
add(labelInfo); //remove PanelInformation.
add(labelPrix);//remove PanelInformation.
add(fieldPrix);//remove PanelInformation.
add(labelDesc);//remove PanelInformation.
add(fieldDesc);//remove PanelInformation.
add(labelQuant);//remove PanelInformation.
add(fieldQuantite);//remove PanelInformation.
}
AND
public JPanelVentes() {
//JPanel PanelVentes = new JPanel(); remove the new instance of JPanel
setLayout(new GridLayout());
JLabel labelVendre = new JLabel ("VENDRE");
JLabel labelQte = new JLabel ("Quantite");
JLabel labelPromo = new JLabel ("Promotion");
JLabel labelTot = new JLabel ("Total");
JTextField fieldQte = new JTextField (20);
JTextField fieldPromoEuros = new JTextField (20);
JTextField fieldPromoPourcent = new JTextField (20);
JTextField fieldTotal = new JTextField (20);
add (labelVendre); //remove PanelVentes
add (labelQte);//remove PanelVentes
add (fieldQte);//remove PanelVentes
add (labelPromo);//remove PanelVentes
add (fieldPromoEuros);//remove PanelVentes
add (fieldPromoPourcent);//remove PanelVentes
add (labelTot);//remove PanelVentes
add (fieldTotal);//remove PanelVentes
}

Related

How to remove existing panel before adding new one

I am learning Java and working on a project. I want to add a new panel as the button is clicked. It is working, but if user clicks button again it adds the panel again. I want to remove the existing panel before adding new one. I have tried remove(westPanel); revalidate(); at the start of my actionPerformed method, but its not working.
Can someone please help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJpanel extends JPanel implements ActionListener
{ //declaring some buttons
private JButton newAccountButton, deleteAccountButton, searchButton, showButton, depositButton, withdrawButton;
protected JPanel westPanel, panel;
JLabel message = new JLabel("Please Use Buttons For Interation");
public MyJpanel()
{
newAccountButton = new JButton("Create Account");
deleteAccountButton = new JButton("Delete Account");
searchButton = new JButton("Search Account");
showButton = new JButton("Show Accounts");
depositButton = new JButton("Deposit");
withdrawButton = new JButton("Withdraw");
setBackground(Color.GRAY);
add(newAccountButton);
add(deleteAccountButton);
add(searchButton);
add(showButton);
add(depositButton);
add(withdrawButton);
newAccountButton.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e)
{ westPanel= new JPanel();
if (e.getSource()==newAccountButton)
{ getRootPane().remove(westPanel);
revalidate();
JLabel accountNumber = new JLabel("Account Number");
JTextField tfaccountnumber = new JTextField(10);
JLabel name = new JLabel("Owner Name");
JTextField tfName = new JTextField(10);
JLabel password = new JLabel("Password");
JTextField tfPassword = new JTextField(10);
JLabel deposit = new JLabel("Deposit Amount");
JTextField tfDeposit = new JTextField(10);
westPanel.setLayout(new GridLayout(4,2));
westPanel.setBackground(getBackground());
westPanel.add(accountNumber);
westPanel.add(tfaccountnumber);
westPanel.add(name);
westPanel.add(tfName);
westPanel.add(password);
westPanel.add(tfPassword);
westPanel.add(deposit);
westPanel.add(tfDeposit);
add(westPanel);
revalidate();
}
}
}

Nothing Displayed On Screen Java Swing Application

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimultaneousSolver extends JFrame implements ActionListener
{
JTextField tfEQ1X = new JTextField (20);
JTextField tfEQ1Y = new JTextField (20);
JTextField tfEQ1Num = new JTextField (20);
JTextField tfEQ2X = new JTextField (20);
JTextField tfEQ2Y = new JTextField (20);
JTextField tfEQ2Num = new JTextField (20);
JLabel lblX1 = new JLabel ("X₁");
JLabel lblY1 = new JLabel ("Y₁");
JLabel lblNum1 = new JLabel ("Number₁");
JLabel lblEqual1 = new JLabel ("=");
JLabel lblX2 = new JLabel ("X₂");
JLabel lblY2 = new JLabel ("Y₂");
JLabel lblNum2 = new JLabel ("Number₂");
JLabel lblEqual2 = new JLabel ("=");
JTextArea Empty = new JTextArea ("",1,20);
double X1, X2, Y1, Y2, Num1, Num2;
double SolX, SolY;
Font font = new Font("Comic Sans MS", Font.BOLD, 14);
SimultaneousSolver()
{
super ("Simultaneous Equation Solver");
setDesign();
setSize(700,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panelEquation1 = new JPanel(new GridLayout(1,7));
{
panelEquation1.add(lblX1);
panelEquation1.add(tfEQ1X);
panelEquation1.add(lblY1);
panelEquation1.add(tfEQ1Y);
panelEquation1.add(lblEqual1);
panelEquation1.add(lblNum1);
panelEquation1.add(tfEQ1Num);
}
JPanel panelEquation2 = new JPanel(new GridLayout(1,7));
{
panelEquation2.add(lblX2);
panelEquation2.add(tfEQ2X);
panelEquation2.add(lblY2);
panelEquation2.add(tfEQ2Y);
panelEquation2.add(lblEqual2);
panelEquation2.add(lblNum2);
panelEquation2.add(tfEQ2Num);
}
setVisible(true);
}
public final void setDesign()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
new SimultaneousSolver();
}
}
Ok, So I am starting a new application development using pure swing if possible, so i set up my objects and stuff and when I try to run the application to see if it looks good, Nothing Shows up except an empty container.
Add these two lines at the end of constructor.
setLayout(new GridLayout()); // sets layout for frame - you can choose any one suitable layout
add(panelEquation1); // adds 1st panel
add(panelEquation2); // adds 2nd panel

changing color for the square textfield

I'm trying to create a layout that would take 3 RGB colors and change the background of the textfield but I'm not able to do so.
package Quizzes_practice;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class quiz3 extends JFrame{
private JPanel up;
private JPanel down;
private JPanel left; // as mother container
private JPanel right;
private JLabel font_size;
private JLabel color_val;
private JLabel r;
private JLabel g;
private JLabel b;
private JTextField font_text;
private JTextField r_text;
private JTextField g_text;
private JTextField b_text;
private JLabel color_text;
private JTextField red; // the big square
private JCheckBox bold;
private JCheckBox italic;
private JRadioButton fg;
private JRadioButton bg;
private JRadioButton reset;
private ButtonGroup gorup;
//private JPanel container; // this will sit in the left corner
private JPanel panel1; // for each row one of these
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel center;
public quiz3(){
JFrame myframe = new JFrame();
setLayout(new BorderLayout()); // general layout
JPanel up = new JPanel();
JPanel down = new JPanel();
JPanel left = new JPanel();
JPanel right = new JPanel();
// adding components
// adding the main 4 panels to the their positions
// panel left will be the container
JLabel font_size = new JLabel("Font Size ");
JLabel r = new JLabel("R");
JLabel g = new JLabel("G");
JLabel b = new JLabel("B");
myhandler handler = new myhandler();
JTextField font_text = new JTextField(10);
JTextField r_text = new JTextField(4);
r_text.addActionListener(handler);
JTextField g_text = new JTextField(4);
g_text.addActionListener(handler);
JTextField b_text = new JTextField(4);
b_text.addActionListener(handler);
JCheckBox bold = new JCheckBox("Bold");
JCheckBox italic = new JCheckBox("Italic");
JRadioButton fg = new JRadioButton("Foreground");
JRadioButton bg = new JRadioButton("Background");
JRadioButton reset = new JRadioButton("Reset");
ButtonGroup group = new ButtonGroup();
//JPanel center = new JPanel();
red = new JTextField(); // text field that I want to change its color
//center.setLayout(new FlowLayout());
//center.add(red);
///red.setBackground(Color.RED);
add(up, BorderLayout.NORTH);
add(down, BorderLayout.SOUTH);
add(left, BorderLayout.WEST);
add(red, BorderLayout.CENTER);
JPanel panel1 = new JPanel ();
JPanel panel2 = new JPanel ();
JPanel panel3 = new JPanel ();
JPanel panel4 = new JPanel ();
// adding compnents to the frame
up.setLayout(new FlowLayout());
up.add(font_size);
up.add(font_text);
up.add(bold);
up.add(italic);
down.setLayout(new FlowLayout());
//down.add(group);
group.add(fg);
group.add(bg);
group.add(reset);
down.add(fg);
down.add(bg);
down.add(reset);
JLabel color_text = new JLabel("Font size");
panel1.add(color_text);
panel2.setLayout(new FlowLayout());
panel2.add(r);
panel2.add(r_text);
panel3.setLayout(new FlowLayout());
panel3.add(g);
panel3.add(g_text);
panel4.setLayout(new FlowLayout());
panel4.add(b);
panel4.add(b_text);
left.setLayout(new GridLayout(5,0));
left.add(panel1);
left.add(panel2);
left.add(panel3);
left.add(panel4);
}
private class myhandler implements ActionListener{
int red1;
int blue1;
int green1;
public void actionPerformed (ActionEvent event){
if(event.getSource()== r_text){
String a = event.getActionCommand();
int a2 = Integer.parseInt(a);
Color mycolor = new Color(a2);
red.setBackground(mycolor);
}
if(event.getSource()== b_text){
String a = event.getActionCommand();
int a2 = Integer.parseInt(a);
Color mycolor = new Color(a2);
red.setBackground(mycolor);
}
if(event.getSource()== g_text){
String a = event.getActionCommand();
int a2 = Integer.parseInt(a);
Color mycolor = new Color(a2);
red.setBackground(mycolor);
}
else {System.out.println("error!");}
}
}
}
I'm not getting an error, it's just not happening, the condition I believe is incorrect
You're shadowing your JTextField variables. Replace
JTextField r_text = new JTextField(4);
with
r_text = new JTextField(4);
The same applys to the other color components.
Aside: Consider using JColorChooser. Read How to Use Color Choosers

Java swing timer not decrementing the right way and not starting at the correct hour

I am part of team that is creating a test for students to get used to a certain format before they have to take a certification test. The test is four hours long and as I am trying to implement the timer I am seeing unusual patterns.
I expect the timer to start at 4 hours (in HH:MM:SS format) and count down to all zeros, unfortunately it is starting at 11:00:00 and counting down to 07:00:00.
The next problem is the timer has to be shown between two different pages. The actual taking of the exam and a review page. When I toggle back and fourth between the pages the counter starts decrementing by the multiple of times clicked between pages.
Below is my created timer class, take quiz and submit review. The code is not perfect and needs work but I needed a GUI mock up to present.
Any help would be appreciated. Thank you.
TAKE QUIZ
package edu.kings.pexam.student;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class TakeQuiz extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton submit;
private JButton show;
static QuizTimer timer;
/**
* #param args
*/
public static void main(String[] args){
TakeQuiz window = new TakeQuiz();
window.setVisible(true);
}
public TakeQuiz(){
setExtendedState(JFrame.MAXIMIZED_BOTH);
JPanel upperPanel = new JPanel(new GridLayout(2,8));
upperPanel.setPreferredSize(new Dimension(WIDTH,100));
upperPanel.setBackground(Color.lightGray);
this.add(upperPanel,BorderLayout.NORTH);
JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
lowerPanel.setBackground(Color.white);
this.add(lowerPanel, BorderLayout.CENTER);
Font font = new Font("Dialog",Font.PLAIN,17);
Font buttonFont = new Font("Dialog",Font.PLAIN,13);
Font textButtonFont = new Font("Dialog",Font.PLAIN+Font.BOLD,13);
Font submitButtonFont = new Font("Dialog", Font.PLAIN + Font.BOLD,15);
//adding the questions buttons to the upper panel
JPanel questionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
questionPanel.setBackground(Color.lightGray);
JButton firstQuestion = new JButton("<<");
firstQuestion.setFont(buttonFont);
questionPanel.add(firstQuestion);
//space to help button line up with text
JPanel spacer1 = new JPanel();
spacer1.setBackground(Color.lightGray);
questionPanel.add(spacer1);
JButton perviousQuestion = new JButton("<");
perviousQuestion.setFont(buttonFont);
questionPanel.add(perviousQuestion);
//space to help button line up with text
JPanel spacer2 = new JPanel();
spacer2.setBackground(Color.lightGray);
questionPanel.add(spacer2);
JButton nextQuestion = new JButton(">");
nextQuestion.setFont(buttonFont);
questionPanel.add(nextQuestion);
//space to help button line up with text
JPanel spacer3 = new JPanel();
spacer3.setBackground(Color.lightGray);
questionPanel.add(spacer3);
JButton lastQuestion = new JButton(">>");
lastQuestion.setFont(buttonFont);
questionPanel.add(lastQuestion);
upperPanel.add(questionPanel);
//adding the goto button to the upper panel
JPanel goToPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
goToPanel.setBackground(Color.lightGray);
JButton goTo = new JButton("Go To");
goTo.setFont(textButtonFont);
goToPanel.add(goTo);
upperPanel.add(goToPanel);
//adding the flag buttons to the upper panel
JPanel flagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
flagPanel.setBackground(Color.lightGray);
JButton flagP = new JButton("< Flag");
flagP.setFont(textButtonFont);
flagPanel.add(flagP);
JButton flag = new JButton("Flag");
flag.setFont(textButtonFont);
flagPanel.add(flag);
JButton flagN = new JButton("Flag >");
flagN.setFont(textButtonFont);
flagPanel.add(flagN);
upperPanel.add(flagPanel);
//adding help and hide/show timer buttons to the upper panel
JPanel timerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
timerPanel.setBackground(Color.lightGray);
JButton help = new JButton("Help");
help.setFont(textButtonFont);
timerPanel.add(help);
show = new JButton("Show/Hide Timer");
show.setFont(textButtonFont);
show.addActionListener(this);
timerPanel.add(show);
upperPanel.add(timerPanel);
//adding space panels
JPanel spacePanel1 = new JPanel();
JPanel spacePanel2 = new JPanel();
spacePanel1.setBackground(Color.lightGray);
spacePanel2.setBackground(Color.lightGray);
upperPanel.add(spacePanel1);
upperPanel.add(spacePanel2);
//adding the submit button to the upper panel
JPanel submitPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
submitPanel.setBackground(Color.lightGray);
submit = new JButton("Submit Quiz");
submit.setFont(submitButtonFont);
submit.addActionListener(this);
submitPanel.add(submit);
upperPanel.add(submitPanel);
//adding the question button text to the upper panel
JPanel questionText = new JPanel(new FlowLayout(FlowLayout.LEFT));
questionText.setBackground(Color.lightGray);
JLabel label2 = new JLabel("<html><center>First<br></br>Question</center></html>");
label2.setFont(textButtonFont);
questionText.add(label2);
JLabel label4 = new JLabel("<html><center>Perivous<br></br>Question</center></html>");
label4.setFont(textButtonFont);
questionText.add(label4);
JLabel label6 = new JLabel("<html><center>Next<br></br>Question</center></html>");
label6.setFont(textButtonFont);
questionText.add(label6);
JLabel label8 = new JLabel("<html><center>Last<br></br>Question</center></html>");
label8.setFont(textButtonFont);
questionText.add(label8);
upperPanel.add(questionText);
//adding text box for go to button
JPanel textGoTo = new JPanel(new FlowLayout(FlowLayout.CENTER));
textGoTo.setBackground(Color.lightGray);
JPanel upper10 = new JPanel();
upper10.setBackground(Color.lightGray);
JTextField goToText = new JTextField("1",2);
JLabel label10 = new JLabel("/25");
label10.setFont(font);
upper10.add(goToText,BorderLayout.CENTER);
upper10.add(label10,BorderLayout.CENTER);
textGoTo.add(upper10);
upperPanel.add(textGoTo);
//adding spacer to the upper panel
JPanel spacePanel3 = new JPanel();
spacePanel3.setBackground(Color.lightGray);
upperPanel.add(spacePanel3);
//adding the timer to the upper panel
JPanel timePanel = new JPanel();
timePanel.setBackground(Color.lightGray);
timer = new QuizTimer();
timer.start();
JPanel upper20 = new JPanel();
upper20.setBackground(Color.lightGray);
upper20.add(timer.getTimeLabel(),BorderLayout.CENTER);
timePanel.add(upper20);
upperPanel.add(timePanel);
//adding two more space panels
JPanel spacePanel4 = new JPanel();
JPanel spacePanel5 = new JPanel();
spacePanel4.setBackground(Color.lightGray);
spacePanel5.setBackground(Color.lightGray);
upperPanel.add(spacePanel4);
upperPanel.add(spacePanel5);
//adding the questions to the lower panel
JPanel lower1 = new JPanel(new GridLayout(4,1));
lower1.setBackground(Color.white);
JLabel question = new JLabel("<html>The parents of a 16-year-old swimmer contact an athletic trainer seeking nutritional advice for the athlete's pre-event<br><\bmeal. What recommendation should the athletic trainer share share with the parents regrading ideal pre-event meals?</html>");
question.setFont(new Font("Dialog", Font.PLAIN+Font.BOLD, 18));
JPanel answer = new JPanel(new GridLayout(6,1));
answer.setBackground(Color.white);
JLabel type = new JLabel("Choose all that apply.");
type.setFont(new Font("Dialog", Font.PLAIN+Font.BOLD+Font.ITALIC, 20));
JPanel answerA = new JPanel(new FlowLayout(FlowLayout.LEFT));
answerA.setBackground(Color.white);
JRadioButton a = new JRadioButton();
a.setBackground(Color.white);
a.setSize(25,25);
JLabel aFill = new JLabel("Include foods high in carbohydrates, high in proteins , and low in fats");
aFill.setFont(font);
answerA.add(a);
answerA.add(aFill);
JPanel answerB = new JPanel(new FlowLayout(FlowLayout.LEFT));
answerB.setBackground(Color.white);
JRadioButton b = new JRadioButton();
b.setBackground(Color.white);
b.setSize(25,25);
JLabel bFill = new JLabel("Prepare meals without diuretics foods");
bFill.setFont(font);
answerB.add(b);
answerB.add(bFill);
JPanel answerC = new JPanel(new FlowLayout(FlowLayout.LEFT));
answerC.setBackground(Color.white);
JRadioButton c = new JRadioButton();
c.setBackground(Color.white);
c.setSize(25,25);
JLabel cFill = new JLabel("Prepare meals for eating four hours prior to the competition");
cFill.setFont(font);
answerC.add(c);
answerC.add(cFill);
JPanel answerD = new JPanel(new FlowLayout(FlowLayout.LEFT));
answerD.setBackground(Color.white);
JRadioButton d = new JRadioButton();
d.setBackground(Color.white);
d.setSize(25,25);
JLabel dFill = new JLabel("Prepare meals with food that delay gastric emptying");
dFill.setFont(font);
answerD.add(d);
answerD.add(dFill);
JPanel record = new JPanel(new FlowLayout(FlowLayout.LEFT));
record.setBackground(Color.lightGray);
JLabel unanswered = new JLabel("Unanswered: ");
unanswered.setFont(font);
record.add(unanswered);
JLabel unansweredNumber = new JLabel("25");
unansweredNumber.setFont(font);
unansweredNumber.setForeground(Color.blue);
record.add(unansweredNumber);
JLabel space1 = new JLabel();
record.add(space1);
JLabel answered = new JLabel("Answered: ");
answered.setFont(font);
record.add(answered);
JLabel answeredNumber = new JLabel("0");
answeredNumber.setFont(font);
answeredNumber.setForeground(Color.blue);
record.add(answeredNumber);
JLabel space2 = new JLabel();
record.add(space2);
JLabel flagged = new JLabel("Flagged: ");
flagged.setFont(font);
record.add(flagged);
JLabel flaggedNumber = new JLabel("0");
flaggedNumber.setFont(font);
flaggedNumber.setForeground(Color.blue);
record.add(flaggedNumber);
answer.add(type);
answer.add(answerA);
answer.add(answerB);
answer.add(answerC);
answer.add(answerD);
answer.add(record);
lower1.add(question);
lower1.add(answer);
lowerPanel.add(lower1);
getContentPane().setBackground(Color.white);
upperPanel.setVisible(true);
lowerPanel.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit){
SubmitReview reviewScreen = new SubmitReview();
this.setVisible(false);
reviewScreen.setVisible(true);
}else if(e.getSource() == show){
if(timer.getTimeLabel().isVisible() == true){
timer.getTimeLabel().setVisible(false);
}else{
timer.getTimeLabel().setVisible(true);
}
}
}
}
FINAL SUBMIT
package edu.kings.pexam.student;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SubmitReview extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton hideTimerButton;
private JButton showTimerButton;
private JComboBox<String> languageBox;
private JButton returnToQuizButton;
private JButton endQuizButton;
private JTextField textEnter;
static QuizTimer timer = TakeQuiz.timer;
/**
* Runs the program to produce the screen for submission review.
* #param args
*/
public static void main(String[] args){
SubmitReview window = new SubmitReview();
//sets the window visible
window.setVisible(true);
}
public SubmitReview(){
//Extends the screen to maximum size
setExtendedState(JFrame.MAXIMIZED_BOTH);
//Creates a panel that will keep items pushed to the right.
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(200,HEIGHT));
leftPanel.setBackground(Color.white);
this.add(leftPanel,BorderLayout.WEST);
//Panel where everything on page will go.
JPanel rightPanel = new JPanel(new GridLayout(10,1));
rightPanel.setBackground(Color.white);
this.add(rightPanel, BorderLayout.CENTER);
//font for the text
Font textFont = new Font("Dialog",Font.PLAIN,15);
//First panel in the grid. Grid moves from top to bottom.
JPanel panel0 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel0.setBackground(Color.white);
//hide timer button, visible when timer is shown.
hideTimerButton = new JButton("Hide Timer");
hideTimerButton.setBackground(Color.lightGray);
Dimension hideTimerDimension = new Dimension(100,25);
hideTimerButton.setSize(hideTimerDimension);
hideTimerButton.setMinimumSize(hideTimerDimension);
hideTimerButton.setMaximumSize(hideTimerDimension);
hideTimerButton.setPreferredSize(hideTimerDimension);
hideTimerButton.setVisible(true);
//show timer button, visible when timer is not shown.
showTimerButton = new JButton("Show Timer");
showTimerButton.setBackground(Color.lightGray);
Dimension showTimerDimension = new Dimension(125, 25);
showTimerButton.setSize(showTimerDimension);
showTimerButton.setMinimumSize(showTimerDimension);
showTimerButton.setMaximumSize(showTimerDimension);
showTimerButton.setPreferredSize(showTimerDimension);
showTimerButton.setVisible(false);
//creates functionality for the show and hide timer buttons
hideTimerButton.addActionListener(this);
showTimerButton.addActionListener(this);
panel0.add(timer.getTimeLabel());
panel0.add(hideTimerButton);
panel0.add(showTimerButton);
rightPanel.add(panel0);
//Second panel in the grid.
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel1.setBackground(Color.white);
//Splits this panel into a grid
JPanel grid = new JPanel(new GridLayout(2,1));
//A Panel to hold the language drop down menu
JPanel languagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
languagePanel.setBackground(Color.white);
//Creates a combo box of languages (drop down menu)
String[] languages = {"English", "Spanish", "French", "Portuguese" };
languageBox = new JComboBox<String>(languages);
languageBox.setBackground(Color.white);
languageBox.addActionListener(this);
languagePanel.add(languageBox);
//Text under the combo box
JPanel textPanel = new JPanel();
JLabel text = new JLabel("Do you want to end your exam now?");
Font font = new Font("Dialog",Font.PLAIN,17);
text.setFont(font);
textPanel.setBackground(Color.white);
textPanel.add(text);
grid.add(languagePanel);
grid.add(textPanel);
//Stop sign picture
File stopSign = new File("resources/stop_sign.png");
ImageIcon stopSignIcon = null;
try {
stopSignIcon = new ImageIcon(ImageIO.read(stopSign));
}
catch (IOException e) {
System.out.println("Caught exception:" + e);
}
JLabel stopLabel = new JLabel();
stopLabel.setIcon(stopSignIcon);
stopLabel.setBackground(Color.white);
stopLabel.setBorder(null);
panel1.add(grid);
panel1.add(stopLabel);
rightPanel.add(panel1);
//third panel in the grid
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2.setBackground(Color.white);
//splits the panel into a grid
JPanel textArea2 = new JPanel(new GridLayout(2,1));
textArea2.setBackground(Color.white);
JPanel warningText1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
warningText1.setBackground(Color.white);
JLabel warningText1Point = new JLabel("<html><li>You left the following questions unanswered. If you end your exam now,<b> you lose the chance to answer these questions.</b></html>");
warningText1Point.setFont(textFont);
warningText1.add(warningText1Point);
JPanel breakPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
breakPanel.setBackground(Color.white);
JLabel space = new JLabel("<html><t> </t></html>");
breakPanel.add(space);
//adds FAKE question buttons to the panel
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setBackground(Color.white);
JButton one = new JButton("1");
JButton two = new JButton("5");
JButton three = new JButton("12");
one.setBackground(Color.lightGray);
two.setBackground(Color.lightGray);
three.setBackground(Color.lightGray);
buttonPanel.add(one);
buttonPanel.add(two);
buttonPanel.add(three);
breakPanel.add(buttonPanel);
textArea2.add(warningText1);
textArea2.add(breakPanel);
panel2.add(textArea2);
rightPanel.add(panel2);
//fourth panel in the grid
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel3.setBackground(Color.white);
JPanel textArea3 = new JPanel(new GridLayout(2,1));
textArea3.setBackground(Color.white);
JPanel warningText3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
warningText3.setBackground(Color.white);
JLabel warningText3Point = new JLabel("<html><li>You marked the following questions for later review. If you end your exam now, <b>you lose the chance to review these marked questions.</b></html>");
textArea3.setBackground(Color.white);
warningText3Point.setFont(textFont);
JPanel breakPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
breakPanel3.setBackground(Color.white);
JLabel space3 = new JLabel("<html><t> </t></html>");
breakPanel3.add(space3);
JPanel buttonPanel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel3.setBackground(Color.white);
JButton four = new JButton("4");
JButton five = new JButton("9");
JButton six = new JButton("20");
four.setBackground(Color.lightGray);
five.setBackground(Color.lightGray);
six.setBackground(Color.lightGray);
buttonPanel3.add(four);
buttonPanel3.add(five);
buttonPanel3.add(six);
breakPanel3.add(buttonPanel3);
textArea3.add(warningText3Point);
textArea3.add(warningText3);
textArea3.add(breakPanel3);
panel3.add(textArea3);
rightPanel.add(panel3);
//fifth panel in the grid
JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel4.setBackground(Color.white);
JPanel grid4 = new JPanel(new GridLayout(2,1));
grid4.setBackground(Color.white);
JPanel border4 = new JPanel(new GridLayout(1,2));
border4.setBackground(Color.white);
JPanel spacer4 = new JPanel();
spacer4.setBackground(Color.white);
JPanel button4 = new JPanel();
button4.setBackground(Color.white);
returnToQuizButton = new JButton("No. Return to the Quiz" );
returnToQuizButton.setBackground(Color.lightGray);
returnToQuizButton.addActionListener(this);
button4.add(returnToQuizButton,BorderLayout.SOUTH);
JPanel textPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
textPanel4.setBackground(Color.white);
JLabel label4 = new JLabel("<html><li>You still have time remaining.</b></html>");
label4.setFont(textFont);
textPanel4.add(label4);
border4.add(spacer4);
border4.add(button4);
grid4.add(textPanel4);
grid4.add(border4);
panel4.add(grid4);
rightPanel.add(panel4);
//sixth panel in the grid
JPanel panel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel5.setBackground(Color.white);
JPanel textPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
textPanel5.setBackground(Color.white);
JLabel text5 = new JLabel("<html><li>If you end your exam now,<b> you cannot return to the exam.</b></html>");
text5.setFont(textFont);
textPanel5.add(text5);
panel5.add(textPanel5);
rightPanel.add(panel5);
//seventh panel in the grid
JPanel panel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel6.setBackground(Color.white);
JPanel textPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
textPanel6.setBackground(Color.white);
JLabel text6 = new JLabel("If you are ready to end the multiple-choice exam now, type the words 'I understand' in the box below.");
text6.setFont(textFont);
textPanel6.add(text6);
panel6.add(textPanel6);
rightPanel.add(panel6);
//eight panel in the grid
JPanel panel7 = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel7.setBackground(Color.white);
textEnter = new JTextField("Type 'I understand' here.");
textEnter.setFont(textFont);
textEnter.setColumns(13);
//clears box on click of mouse
textEnter.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
textEnter.setText("");
}
});
textEnter.addActionListener(this);
JPanel textHolder = new JPanel();
textHolder.setBackground(Color.white);
textHolder.add(textEnter,BorderLayout.CENTER);
panel7.add(textHolder);
rightPanel.add(panel7);
//ninth panel in the grid
JPanel panel8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel8.setBackground(Color.white);
endQuizButton = new JButton("Yes. End the Quiz Now");
endQuizButton.setBackground(Color.lightGray);
endQuizButton.addActionListener(this);
endQuizButton.setEnabled(false);
JPanel button8 = new JPanel();
button8.setBackground(Color.white);
button8.add(endQuizButton, BorderLayout.CENTER);
panel8.add(button8);
rightPanel.add(panel8);
getContentPane().setBackground(Color.white);
leftPanel.setVisible(true);
rightPanel.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e) {
String text = textEnter.getText();
if(text.equals("I understand")){
endQuizButton.setEnabled(true);
}
if(e.getSource() == hideTimerButton){
hideTimerButton.setVisible(false);
timer.getTimeLabel().setVisible(false);
showTimerButton.setVisible(true);
}else if(e.getSource() == showTimerButton){
showTimerButton.setVisible(false);
hideTimerButton.setVisible(true);
timer.getTimeLabel().setVisible(true);
}else if(e.getSource() == returnToQuizButton){
TakeQuiz quizScreen = new TakeQuiz();
this.setVisible(false);
quizScreen.setVisible(true);
}else if(e.getSource() == endQuizButton){
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to submit your quiz for grading?","Select an Option", JOptionPane.YES_NO_OPTION);
if(response == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Your grade on this quiz is: 85");
System.exit(0);
}
}
}
}
QUIZ TIMER
package edu.kings.pexam.student;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class QuizTimer {
private static double time = 1.44*Math.pow(10,7);
private SimpleDateFormat setTime = new SimpleDateFormat("hh:mm:ss");
private JLabel timeLabel;
private Timer countDown;
public QuizTimer(){
countDown = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (time >= 0) {
timeLabel.setText(setTime.format(time));
time = time-1000;
}else{
JOptionPane.showMessageDialog(null, "Your quiz has been automatically submitted for grading.", "Out of Time", JOptionPane.OK_OPTION);
System.exit(0);
}
}
});
timeLabel = new JLabel();
timeLabel.setFont( new Font("Dialog", Font.PLAIN + Font.BOLD,24));
timeLabel.setVisible(true);
}
public JLabel getTimeLabel(){
return timeLabel;
}
public void start(){
countDown.start();
}
}
The first problem (11:00:00 to 7:00:00) probably has to do with your timezone.
The second one may (from the top of my head) have to do with time field being static.
In any way, I'd be curious why it is static. Seems that this logic would break if you have two timers.
(P.S. Please vote.)

Java GUI (SWING/AWT) - Empty Frame - Components not showing

I'm trying to create (hand-coded) a GUI similair to the GUI shown below, however, only an empty frame shows.
Mock GUI:
I've used various layouts and SWING/AWT components to create the GUI and 4 JPanels which contain:
mainPanel: Contains all the panels in it.
listPanel: Contains the JTables, JLabels and the two JButtons
infoPanel: Contains the JLabels, JCheckBox and JTextBoxes.
addPanel: Contains the JLists and JButton
This is what I coded so far:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
public class GUI extends JFrame {
public void buildGui() {
JFrame frame = new JFrame("Hotel TV Scheduler");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(3,3));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(2,2));
JPanel addPanel = new JPanel();
addPanel.setLayout(new FlowLayout());
mainPanel.add(listPanel, BorderLayout.LINE_START);
mainPanel.add(infoPanel, BorderLayout.LINE_END);
mainPanel.add(addPanel, BorderLayout.PAGE_END);
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
JLabel ch1Label = new JLabel("Channel 1");
JLabel ch2Label = new JLabel("Channel 2");
JLabel listLabel = new JLabel("List");
JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");
listPanel.add(ch1Label);
listPanel.add(ch2Label);
listPanel.add(listLabel);
listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
listPanel.add(rmvChOneButton);
listPanel.add(rmvChTwoButton);
JLabel titleLabel = new JLabel("Title");
JLabel genreLabel = new JLabel("Genre");
JLabel durationLabel = new JLabel("Duration");
JLabel actorLabel = new JLabel("Actor");
JLabel directorLabel = new JLabel("Director");
JLabel rentableLabel = new JLabel("Rentable");
JLabel synLabel = new JLabel("Synopsis");
JTextField txtTitle = new JTextField();
JTextField txtGenre = new JTextField();
JTextField txtDuration = new JTextField();
JTextField txtActor = new JTextField();
JTextField txtDirector = new JTextField();
JTextField txtSynopsis = new JTextField();
JCheckBox rentCB = new JCheckBox();
infoPanel.add(titleLabel);
infoPanel.add(txtTitle);
infoPanel.add(genreLabel);
infoPanel.add(txtGenre);
infoPanel.add(durationLabel);
infoPanel.add(txtDuration);
infoPanel.add(actorLabel);
infoPanel.add(txtActor);
infoPanel.add(directorLabel);
infoPanel.add(txtDirector);
infoPanel.add(rentableLabel);
infoPanel.add(rentCB);
infoPanel.add(synLabel);
infoPanel.add(txtSynopsis);
JButton btnAddProg = new JButton("Add Program");
JList channelList = new JList();
JList timeList = new JList();
addPanel.add(btnAddProg);
addPanel.add(channelList);
addPanel.add(timeList);
frame.setVisible(true);
}
}
Anyone can tell me why only an empty frame is showing up ?
Thanks and Regards,
Brian
Yep just checked, you'll see something if you actually add the mainPanel to the frame, (looks nothing like the mock though!)
frame.setContentPane(mainPanel);
frame.pack();
You've not added mainPanel to the frame

Categories

Resources