I'm trying to learn swing and work through the task we have been given. I can't see why the code doesn't display the JButtons in the SOUTH section as there is no issue when displaying the textfields, combobox and labels in the CENTER section.
I used the same format to add components to my CENTER section as I did in the SOUTH and EAST but only the center displays anything.
public class ProductListGUI{
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel p1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel p3 = new JPanel();
p1.setLayout(null);
p1.setBounds(0,0,600,500);
p1.setBackground(new Color(230,230,230));
scrollPane.setLayout(null);
scrollPane.setBounds(600,0,200,500);
p3.setLayout(null);
p3.setBounds(0,500,800,100);
p3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
p1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
p1.add(t1);p1.add(l1);p1.add(t2);p1.add(l2);p1.add(t3);p1.add(l3);p1.add(l4);p1.add(dropdown);p1.add(checkBox);
JButton b1 = new JButton("New Item");
b1.setBounds(200,550,80,20);
JButton b2 = new JButton("Save");
b2.setBounds(300,550,80,20);
JButton b3 = new JButton("Delete Selected");
b3.setBounds(600,550,80,20);
b3.setEnabled(false);
p3.add(b1);p3.add(b2);p3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);menu.add(importData);menu.add(inventory);menu.add(export);
mb.add(menu);
f.getContentPane().add(p1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(p3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setLayout(new BorderLayout());
f.setVisible(true);
}
These code changes should help at least a little bit.
Things that had to change:
Each JPanel should have a layout. Setting the layout to null (as per TG's comment) is not good.
The setBounds() methods for the buttons were removed.
The f.setLayout(new BorderLayout()); was moved to the top of the code where Components were being added to the JFrame before the layout was set.
import javax.swing.*;
import java.awt.*;
public class ProductListGUI {
JMenu menu;
JMenuItem about,importData,inventory,export;
ProductListGUI(){
JFrame f = new JFrame("Assignment 2");
JPanel panel1 = new JPanel();
JList<String> list = new JList<>();
list.setBounds(600,0,200,600);
JScrollPane scrollPane = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panel3 = new JPanel();
panel1.setBounds(0,0,600,500);
panel1.setBackground(new Color(230,230,230));
scrollPane.setBounds(600,0,200,500);
panel3.setBounds(0,0,800,100);
panel3.setBackground(new Color(230,230,230));
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3;
l1=new JLabel("ProductID");
l1.setBounds(10,100,200,30);
t1=new JTextField();
t1.setBounds(100,100,200,30);
l2=new JLabel("Name");
l2.setBounds(10,150,200,30);
t2=new JTextField();
t2.setBounds(100,150,200,30);
l3=new JLabel("Quantity");
l3.setBounds(10,250,200,30);
t3=new JTextField();
t3.setBounds(100,250,200,30);
panel1.setBorder(BorderFactory.createTitledBorder("Product Details"));
JCheckBox checkBox = new JCheckBox("Available for Next Day Delivery");
checkBox.setBounds(10,300,250,50);
l4 = new JLabel("Item Type");
l4.setBounds(10,200,200,30);
String[] itemType = {"Select type","Homeware","Hobby","Garden"};
JComboBox dropdown = new JComboBox(itemType);
dropdown.setBounds(100,200,120,20);
panel1.add(t1);
panel1.add(l1);
panel1.add(t2);
panel1.add(l2);
panel1.add(t3);
panel1.add(l3);
panel1.add(l4);
panel1.add(dropdown);
panel1.add(checkBox);
JButton b1 = new JButton("New Item");
JButton b2 = new JButton("Save");
JButton b3 = new JButton("Delete Selected");
b3.setEnabled(false);
panel3.add(b1);
panel3.add(b2);
panel3.add(b3);
JMenuBar mb = new JMenuBar();
menu = new JMenu("Actions");
about = new JMenuItem("About");
importData = new JMenuItem("Import Data");
inventory = new JMenuItem("Inventory");
export = new JMenuItem("Export to CSV");
menu.add(about);
menu.add(importData);
menu.add(inventory);
menu.add(export);
mb.add(menu);
f.setLayout(new BorderLayout());
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(scrollPane,BorderLayout.EAST);
f.getContentPane().add(panel3,BorderLayout.SOUTH);
f.setJMenuBar(mb);
f.setSize(800,600);
f.setVisible(true);
}
public static void main(String[] args) {
ProductListGUI gui = new ProductListGUI();
}
}
Here is a very simple BorderLayout example that might help in the future, or not.
import javax.swing.*;
import java.awt.*;
public class TestGui {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Frame");
frame.setLayout(new BorderLayout());
frame.setSize(800,600);
frame.getContentPane().add(createJPanel("CENTER", Color.RED), BorderLayout.CENTER);
frame.getContentPane().add(createJPanel("NORTH", Color.CYAN), BorderLayout.NORTH);
frame.getContentPane().add(createJPanel("EAST", Color.LIGHT_GRAY), BorderLayout.EAST);
frame.getContentPane().add(createJPanel("SOUTH", Color.GREEN), BorderLayout.SOUTH);
frame.getContentPane().add(createJPanel("WEST", Color.YELLOW), BorderLayout.WEST);
frame.setVisible(true);
}
private static JPanel createJPanel(String title, Color color) {
JPanel jPanel = new JPanel();
jPanel.setLayout(new BorderLayout());
jPanel.add(new JLabel(title), BorderLayout.CENTER);
jPanel.setBackground(color);
return jPanel;
}
}
Related
I'm creating a simple UI using GUI objects to input data but whenever I add the center panel to the contentPane, my JLabels disappear. Additionally, the JTextFields, JComboBoxs and JButtons don't respond to clicking or entering keystrokes. If I don't add the centerPanel, or add it and start the applet with width and height parameters of 1 and 1, everything works perfectly.
When I stretch out the screen, after adding the center panel to a normal run configuration, the objects will appear outside of the initial window. I've defined all of the objects as private instance variables before the listed code, so that isn't the issue. Please help, I'm baffled!
Here's my code:
public void begin() {
// creates the GUI Objects for the northPanel
northPanel = new JPanel();
northPanel.setLayout(new GridLayout(1, 4));
searchBox = new JTextField();
searchBoxLabel = new JLabel("Search ID #:");
search = new JButton("Search");
search.addActionListener(this);
northPanel.add(searchBoxLabel);
northPanel.add(searchBox);
northPanel.add(search);
// creates the GUI OBjects for the southPanel
southDivider = new JPanel();
southDivider.setLayout(new GridLayout(2, 1));
southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 3));
enter = new JButton("Enter");
incrementInfo = new JButton("Increment ID");
setCurrentTimeDate = new JButton("Current Time/Date");
findRate = new JButton("Find Yield Rate");
findRate.addActionListener(this);
enter.addActionListener(this);
incrementInfo.addActionListener(this);
setCurrentTimeDate.addActionListener(this);
southPanel.add(findRate);
southPanel.add(setCurrentTimeDate);
southPanel.add(incrementInfo);
southPanel.add(enter);
messageLabel = new JLabel("Welcome to the Stringer Application");
southDivider.add(southPanel);
southDivider.add(messageLabel);
// create the GUI objects on the eastPanel
eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(5, 2));
cellType = new JComboBox();
cellType.addItem("Rect");
cellType.addItem("Cham");
cellTypeLabel = new JLabel("Cell Type:");
ecaCode = new JComboBox();
ecaCode.addItem("A");
ecaCode.addItem("B");
ecaCodeLabel = new JLabel("ECA Code:");
ecaSyringeNum = new JTextField();
ecaSyringeNumLabel = new JLabel("Eca Syringe #:");
passFail = new JComboBox();
passFail.addItem("Pass");
passFail.addItem("Fail");
passFailLabel = new JLabel("Pass/Fail:");
operator = new JTextField();
operatorLabel = new JLabel("Operator:");
cellType.addActionListener(this);
ecaCode.addActionListener(this);
passFail.addActionListener(this);
eastPanel.add(operatorLabel);
eastPanel.add(operator);
eastPanel.add(cellTypeLabel);
eastPanel.add(cellType);
eastPanel.add(ecaCodeLabel);
eastPanel.add(ecaCode);
eastPanel.add(ecaSyringeNumLabel);
eastPanel.add(ecaSyringeNum);
eastPanel.add(passFailLabel);
eastPanel.add(passFail);
// create the GUI objects on the westPanel
westPanel = new JPanel();
westPanel.setLayout(new GridLayout(6, 2));
yieldLabel = new JLabel("Current Yield:");
yieldValueLabel = new JLabel("Select Date/Times");
yieldAfterDate = new JTextField();
yieldAfterTime = new JTextField();
yieldBeforeDate = new JTextField();
yieldBeforeTime = new JTextField();
yieldAfterDateLabel = new JLabel("After Date:");
yieldAfterTimeLabel = new JLabel("After Time:");
yieldBeforeDateLabel = new JLabel("Before Date:");
yieldBeforeTimeLabel = new JLabel("Before Time:");
setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>");
fillBeforeWithCurrent = new JButton("Set to Current");
fillBeforeWithCurrent.addActionListener(this);
westPanel.add(yieldLabel);
westPanel.add(yieldValueLabel);
westPanel.add(yieldAfterDateLabel);
westPanel.add(yieldAfterDate);
westPanel.add(yieldAfterTimeLabel);
westPanel.add(yieldAfterTime);
westPanel.add(yieldBeforeDateLabel);
westPanel.add(yieldBeforeDate);
westPanel.add(yieldBeforeTimeLabel);
westPanel.add(yieldBeforeTime);
westPanel.add(setBeforeToCurrentLabel);
westPanel.add(fillBeforeWithCurrent);
// create the GUI objects for the centerPanel
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 4));
date = new JTextField(getCurrentDate());
dateLabel = new JLabel("Date:");
time = new JTextField(getCurrentTime());
timeLabel = new JLabel("Time:");
stringID = new JTextField();
stringIDLabel = new JLabel("String ID:");
cellLot = new JTextField();
cellLotLabel = new JLabel("Cell Lot #:");
cellEff = new JTextField();
cellEffLabel = new JLabel("Cell Eff:");
comments = new JTextField();
commentsLabel = new JLabel("Comments:");
centerPanel.add(dateLabel);
centerPanel.add(date);
centerPanel.add(timeLabel);
centerPanel.add(time);
centerPanel.add(stringIDLabel);
centerPanel.add(stringID);
centerPanel.add(cellLotLabel);
centerPanel.add(cellLot);
centerPanel.add(cellEffLabel);
centerPanel.add(cellEff);
centerPanel.add(commentsLabel);
centerPanel.add(comments);
// add the panel's to the contentPane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
contentPane.add(southDivider, BorderLayout.SOUTH);
contentPane.add(eastPanel, BorderLayout.EAST);
contentPane.add(westPanel, BorderLayout.WEST);
contentPane.validate();
}
I have tested your code and it is working with no problems.
Most likely you are adding components some where else to the container, and by default any added components goes to center, which cause the old center panel to be removed , and the the swing frame to draw dirty area.
EDIT:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test2 extends JFrame {
public Test2() {
begin();
}
public void begin() {
// creates the GUI Objects for the northPanel
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(1, 4));
JTextField searchBox = new JTextField();
JLabel searchBoxLabel = new JLabel("Search ID #:");
JButton search = new JButton("Search");
northPanel.add(searchBoxLabel);
northPanel.add(searchBox);
northPanel.add(search);
// creates the GUI OBjects for the southPanel
JPanel southDivider = new JPanel();
southDivider.setLayout(new GridLayout(2, 1));
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 3));
JButton enter = new JButton("Enter");
JButton incrementInfo = new JButton("Increment ID");
JButton setCurrentTimeDate = new JButton("Current Time/Date");
JButton findRate = new JButton("Find Yield Rate");
southPanel.add(findRate);
southPanel.add(setCurrentTimeDate);
southPanel.add(incrementInfo);
southPanel.add(enter);
JLabel messageLabel = new JLabel("Welcome to the Stringer Application");
southDivider.add(southPanel);
southDivider.add(messageLabel);
// create the GUI objects on the eastPanel
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(5, 2));
JComboBox cellType = new JComboBox();
cellType.addItem("Rect");
cellType.addItem("Cham");
JLabel cellTypeLabel = new JLabel("Cell Type:");
JComboBox ecaCode = new JComboBox();
ecaCode.addItem("A");
ecaCode.addItem("B");
JLabel ecaCodeLabel = new JLabel("ECA Code:");
JTextField ecaSyringeNum = new JTextField();
JLabel ecaSyringeNumLabel = new JLabel("Eca Syringe #:");
JComboBox passFail = new JComboBox();
passFail.addItem("Pass");
passFail.addItem("Fail");
JLabel passFailLabel = new JLabel("Pass/Fail:");
JTextField operator = new JTextField();
JLabel operatorLabel = new JLabel("Operator:");
eastPanel.add(operatorLabel);
eastPanel.add(operator);
eastPanel.add(cellTypeLabel);
eastPanel.add(cellType);
eastPanel.add(ecaCodeLabel);
eastPanel.add(ecaCode);
eastPanel.add(ecaSyringeNumLabel);
eastPanel.add(ecaSyringeNum);
eastPanel.add(passFailLabel);
eastPanel.add(passFail);
// create the GUI objects on the westPanel
JPanel westPanel = new JPanel();
westPanel.setLayout(new GridLayout(6, 2));
JLabel yieldLabel = new JLabel("Current Yield:");
JLabel yieldValueLabel = new JLabel("Select Date/Times");
JTextField yieldAfterDate = new JTextField();
JTextField yieldAfterTime = new JTextField();
JTextField yieldBeforeDate = new JTextField();
JTextField yieldBeforeTime = new JTextField();
JLabel yieldAfterDateLabel = new JLabel("After Date:");
JLabel yieldAfterTimeLabel = new JLabel("After Time:");
JLabel yieldBeforeDateLabel = new JLabel("Before Date:");
JLabel yieldBeforeTimeLabel = new JLabel("Before Time:");
JLabel setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>");
JButton fillBeforeWithCurrent = new JButton("Set to Current");
westPanel.add(yieldLabel);
westPanel.add(yieldValueLabel);
westPanel.add(yieldAfterDateLabel);
westPanel.add(yieldAfterDate);
westPanel.add(yieldAfterTimeLabel);
westPanel.add(yieldAfterTime);
westPanel.add(yieldBeforeDateLabel);
westPanel.add(yieldBeforeDate);
westPanel.add(yieldBeforeTimeLabel);
westPanel.add(yieldBeforeTime);
westPanel.add(setBeforeToCurrentLabel);
westPanel.add(fillBeforeWithCurrent);
// create the GUI objects for the centerPanel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 4));
JTextField date = new JTextField();
JLabel dateLabel = new JLabel("Date:");
JTextField time = new JTextField();
JLabel timeLabel = new JLabel("Time:");
JTextField stringID = new JTextField();
JLabel stringIDLabel = new JLabel("String ID:");
JTextField cellLot = new JTextField();
JLabel cellLotLabel = new JLabel("Cell Lot #:");
JTextField cellEff = new JTextField();
JLabel cellEffLabel = new JLabel("Cell Eff:");
JTextField comments = new JTextField();
JLabel commentsLabel = new JLabel("Comments:");
centerPanel.add(dateLabel);
centerPanel.add(date);
centerPanel.add(timeLabel);
centerPanel.add(time);
centerPanel.add(stringIDLabel);
centerPanel.add(stringID);
centerPanel.add(cellLotLabel);
centerPanel.add(cellLot);
centerPanel.add(cellEffLabel);
centerPanel.add(cellEff);
centerPanel.add(commentsLabel);
centerPanel.add(comments);
// add the panel's to the contentPane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
contentPane.add(southDivider, BorderLayout.SOUTH);
contentPane.add(eastPanel, BorderLayout.EAST);
contentPane.add(westPanel, BorderLayout.WEST);
// contentPane.validate();
setSize(812, 514);
}
public static void main(String[] args) {
Test2 t=new Test2();
// t.begin();
t.setVisible(true);
}
}
This is my first post so please forgive me if I am not following the rules correctly.
I am trying to do something relatively simple in Java. I want to make a JTextArea scrollable. I am aware this has been asked before (Java :Add scroll into text area). However, when I follow this example by adding JTextArea to JScrollPane my JTextArea does not become scrollable. What is it that I am missing?
Here is my code:
import ...;
public class MyControlPanel extends JPanel {
//Declare variables
private JComboBox accountsBox;
private String[] accType = {"Current Account", "Savings Account"};
private JLabel selAccType, initDeposit, logLabel, simLabel;
private JTextField depositText;
private JTextArea log;
private JScrollPane scroll;
private JButton createAccount, start, stop;
private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
private Timer timer;
private MyAccount theAccount;
private Random randNum1, randNum2;
private DecimalFormat df;
//Constructor
public MyControlPanel() {
//Create instances:
selAccType = new JLabel("Please select account type: "); //JLabel
initDeposit = new JLabel("Input initial deposit: ");
logLabel = new JLabel("Log:");
simLabel = new JLabel();
accountsBox = new JComboBox(accType); //JComboBox
depositText = new JTextField("0"); // JTextField
log = new JTextArea(); //JTextArea
scroll = new JScrollPane(log); //JScrollPane
createAccount = new JButton("Create Account"); //JButton
start = new JButton("Start");
stop = new JButton("Stop");
panel1 = new JPanel(); //JPanel
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
timer = new Timer(); //Timer
df = new DecimalFormat("#.00");
//Add ActionListeners
createAccount.addActionListener(new ActionListener() {...});
start.addActionListener(new ActionListener() {...});
stop.addActionListener(new ActionListener() {...});
//Set JTextField size
depositText.setColumns(5);
//Set JTextArea size
log.setPreferredSize(new Dimension(780, 150));
//Set panel size
panel1.setPreferredSize(new Dimension(500, 500));
panel2.setPreferredSize(new Dimension(800, 50));
panel3.setPreferredSize(new Dimension(500, 50));
panel4.setPreferredSize(new Dimension(500, 50));
panel5.setPreferredSize(new Dimension(800, 25));
panel6.setPreferredSize(new Dimension(800, 200));
//Set layout in panel5 to align left
panel6.setLayout(new FlowLayout(FlowLayout.LEFT));
//Add components to each panel
addPanels();
//Place objects in the framed window
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.add(panel5);
this.add(panel6);
}
public void addPanels() {...}
public void removePanels() {...}
}
You need to change:
log.setPreferredSize(new Dimension(780, 150));
to:
scroll.setPreferredSize(new Dimension(780, 150));
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.)
I'm trying to set the size of a gridlayout jpanel. Here is the code:
JFrame myFrame = new JFrame();
myFrame.setLayout(new FlowLayout());
myFrame.setLocation(400, 100);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel jlMins = new JLabel("Number of minutes for tutoring session (should be a positive decimal number): 0.0");
JLabel jlEarnings = new JLabel("Earnings in dollars and cents received (should be positive decimal number): 0.0");
jtfMins = new JTextField(20);
jtfEarnings = new JTextField(20);
JPanel jpMins = new JPanel(new BorderLayout());
JPanel jpEarnings = new JPanel(new BorderLayout());
jpMins.setPreferredSize(new Dimension(300,50));
jpEarnings.setPreferredSize(new Dimension(300,50));
jpMins.add(jlMins,BorderLayout.NORTH);
jpMins.add(jtfMins,BorderLayout.CENTER);
jpEarnings.add(jlEarnings,BorderLayout.NORTH);
jpEarnings.add(jtfEarnings,BorderLayout.CENTER);
JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20));
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);
jpButtons.add(jbReport);
jpButtons.add(jbQuit);
JPanel jpNorth = new JPanel(new BorderLayout());
jpNorth.add(jpMins,BorderLayout.NORTH);
jpNorth.add(jpEarnings,BorderLayout.CENTER);
jpNorth.add(jpButtons,BorderLayout.SOUTH);
jtaReports = new JTextArea();
jtaReports.setColumns(40);
jtaReports.setRows(10);
jtaReports.setLineWrap(true);
JScrollPane jspReports = new JScrollPane(jtaReports);
jspReports.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel jpSouth = new JPanel();
jpSouth.setPreferredSize(new Dimension(350,200));
jpSouth.add(jspReports);
JPanel jpMain = new JPanel(new BorderLayout());
jpMain.add(jpNorth,BorderLayout.NORTH);
jpMain.add(jpSouth,BorderLayout.SOUTH);
jpMain.setPreferredSize(new Dimension(500,500));
myFrame.setContentPane(jpMain);
myFrame.pack();
myFrame.setVisible(true);
The panel name is jpButtons. Of the above code I'm talking mainly about this section:
JButton jbQuit = new JButton("Quit");
JButton jbEnter = new JButton("Enter");
JButton jbReport = new JButton("Run Report");
jbQuit.setActionCommand("quit");
jbEnter.setActionCommand("enter");
jbReport.setActionCommand("report");
jbQuit.addActionListener(this);
jbEnter.addActionListener(this);
jbReport.addActionListener(this);
JPanel jpButtons = new JPanel(new GridLayout(4,1,0,20));
jpButtons.setSize(new Dimension(50,150));
jpButtons.add(jbEnter);
jpButtons.add(jbReport);
jpButtons.add(jbQuit);
How exactly does setSize, and setPreferredSize work or how to get them to work properly on jpanel, components, etc.
scaling and positioning is handled by the layout manager; let it do its job.
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