JDialog is not showing up - java

I have this problem over and over again when i call my JDialog class is not showing anything this is the code for the JDialog interface:
public JMovie() {
JFrame f = new JFrame();
JDialog jmovie = new JDialog(f,"JMovie Dialog");
jmovie.setLayout(new BorderLayout());
okbutton = new JButton("Ok");
cancelbutton = new JButton("Cancel");
title = new JLabel("Title", SwingConstants.RIGHT);
date = new JLabel("Date made on ", SwingConstants.RIGHT);
price = new JLabel("Price", SwingConstants.RIGHT);
inputdate = new JLabel("dd/mm/yyyy");
Ttitle = new JTextField(null, 15);
TMadeon = new JTextField(null, 10);
TPrice = new JTextField(null, 6);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jb = new JPanel();
jl = new JPanel();
okbutton.addActionListener(this);
cancelbutton.addActionListener(this);
Ttitle.setName("Title");
TMadeon.setName("Date Made on");
TPrice.setName("Price");
jb.add(okbutton);
jb.add(cancelbutton);
title.setAlignmentX(Component.RIGHT_ALIGNMENT);
date.setAlignmentX(Component.RIGHT_ALIGNMENT);
price.setAlignmentX(Component.RIGHT_ALIGNMENT);
JPanel south = new JPanel(new FlowLayout());
south.add(jb, BorderLayout.SOUTH);
JPanel west = new JPanel(new GridLayout(3,1));
west.add(title,BorderLayout.WEST);
west.add(date,BorderLayout.WEST);
west.add(price,BorderLayout.WEST);
JPanel center = new JPanel(new GridLayout(3,1));
center.add(jp3,FlowLayout.LEFT);
center.add(jp2,FlowLayout.LEFT);
center.add(jp1,FlowLayout.LEFT);
inputdate.setEnabled(false);
jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);
jmovie.add(south);
jmovie.add(west);
jmovie.add(center);
}
This is the code of the interface JMovie DIalog and i call it here in another class.
public void actionPerformed(ActionEvent event) {
if(event.getSource().equals(btnMAdd))
{
JMovie ne = new JMovie();
ne.setVisible(true);
}
}
when i run it is showing this:

First of all, variable names should NOT start with an upper case character. Some variables are correct, others are not. Follow Java conventions and be consistent!
jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);
jmovie.add(south);
jmovie.add(west);
jmovie.add(center);
Component should be added to the dialog BEFORE the dialog is made visible.
So the code should be:
jmovie.add(south);
jmovie.add(west);
jmovie.add(center);
jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);
You should be using pack() instead of setVisible(...). This will allow all the components to be displayed at their preferred size.
JFrame f = new JFrame();
Also, you should not be create a JFrame. The parameter you want to pass to the JDialog, is the current visible JFrame.
So in your ActionListener you can get the current frame with logic like:
Window window = SwingUtilities.windowForComponent( btnMAdd );
Now when you create your dialog you pass the window as a parameter:
JMovie ne = new JMovie(window);
and you use this value to specify the parent window of your dialog.

Related

access input that get from user in UI in java and show them in console

i want save input that i get from user and save them in element .
i want to access elements that user write in my UI.
and if i want save the elements in array list which kind of array list i should build.
in my UI i have text field name and text field middle name and combo box city has got 3 city name and and a radio box that it depend sex.
in final show them in console what should i do ?
this all of my code:
package ui;
import java.awt.*;
import javax.swing.*;
public class UI extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500, 600);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
JRadioButton rb1 = new JRadioButton("man");
JRadioButton rb2 = new JRadioButton("weman");
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel,BorderLayout.CENTER);
frame.add(spanel,BorderLayout.SOUTH);
}
}
You need to use listeners to create code that runs when ui component is pressed. I added listener to every component. try it:
public class UI extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout blayout = new BorderLayout();
JButton center = new JButton();
JButton north = new JButton();
JButton south = new JButton();
south.addActionListener(e->{System.out.println("Save button is pressed");});
JComboBox combo = new JComboBox();
combo.addItem("-");
combo.addItem("Tehran");
combo.addItem("Tabriz");
combo.addItem("Shiraz");
combo.addActionListener(e -> {
System.out.println(((JComboBox<String>) e.getSource()).getSelectedItem());
});
JRadioButton rb1 = new JRadioButton("man");
rb1.addActionListener(e -> {
System.out.println("man: " + ((JRadioButton) e.getSource()).isSelected());
});
JRadioButton rb2 = new JRadioButton("weman");
rb2.addActionListener(e -> {
System.out.println("weman: " + ((JRadioButton) e.getSource()).isSelected());
});
frame.setLayout(blayout);
FlowLayout fLoyout = new FlowLayout(FlowLayout.CENTER);
center.setLayout(fLoyout);
south.setLayout(fLoyout);
JLabel jb1 = new JLabel("Name :");
JTextField name = new JTextField(20);
center.add(jb1);
center.add(name);
name.addActionListener(e -> {
System.out.println("name: " + ((JTextField) e.getSource()).getText());
});
JLabel jb2 = new JLabel("Family :");
JTextField family = new JTextField(20);
center.add(jb2);
center.add(family);
family.addActionListener(e -> {
System.out.println("family: " + ((JTextField) e.getSource()).getText());
});
JLabel jb4 = new JLabel("City :");
center.add(jb4);
center.add(combo);
JLabel jb5 = new JLabel("Sex :");
center.add(jb5);
center.add(rb1);
center.add(rb2);
JLabel jb6 = new JLabel("Comment :");
JTextField comment = new JTextField(50);
comment.addActionListener(e -> {
System.out.println("comment: " + ((JTextField) e.getSource()).getText());
});
JLabel jb7 = new JLabel("Save");
south.add(jb7);
JPanel cpanel = new JPanel();
cpanel.add(center);
JPanel spanel = new JPanel();
spanel.add(south);
cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
cpanel.add(jb6);
cpanel.add(comment);
frame.add(cpanel, BorderLayout.CENTER);
frame.add(spanel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
You will have to put JRadioButton into RadioGroup to select one of them.
At first, you need to check your GUI, because from skimming it works not good. Later you can get data from the different component using an appropriate listener for each component or you can use any General button to reading data from all components and print it in a console or anywhere else.
To get data from combo, for example, will be here:
String data = combo.getSelectedIndex();
More information you can get here:
How can I get the user input in Java?

JFrame Window not showing any content unless resized

I have an Interface class that extends JFrame. I am creating 3 JPanels and adding them to a Container (Border Layout) in different areas of the layout.
When I run the application, nothing shows but a blank window and a title. If I resize the application it will then show all the content and work as intended.
I'm not sure what I did differently this time, I've used this method before and it works in previous programs.
Any help is appreciated.
Here is my Interface Class Constructor code: http://pastebin.com/4UyEXsBr
Code:
public class Interface extends JFrame implements ActionListener {
private Container contentPane;
private JPanel buttonPanel, userPanel;
private JButton loginButton, createUserButton, logoutButton, withdrawButton, depositButton, switchToRegisterButton, switchToLoginButton;
private JLabel headerLabel, inputTopJTFLabel, inputPW1JPFLabel, toastLabel, inputPW2JPFLabel;
public JTextField inputTopJTF;
public JPasswordField inputPW1JPF, inputPW2JPF;
JRootPane rootPane;
public Interface(int width, int height, String title) {
//Setting up Interface
setTitle(title);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,100);
setVisible(true);
Font header = new Font("TimesRoman", Font.PLAIN, 50);
///////////////////////BUTTON PANEL///////////////////////
//Button Panel
buttonPanel = new JPanel();
//Buttons
//loginButton
loginButton = new JButton("Login");
loginButton.addActionListener(this);
loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
//switchToRegisterButton
switchToRegisterButton = new JButton("New User?");
switchToRegisterButton.addActionListener(this);
switchToRegisterButton.setAlignmentX(Component.CENTER_ALIGNMENT);
//switchToLoginButton
switchToLoginButton = new JButton("Switch to Login");
switchToLoginButton.addActionListener(this);
switchToLoginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
switchToLoginButton.setVisible(false);
//createUserButton
createUserButton = new JButton("Register");
createUserButton.addActionListener(this);
createUserButton.setAlignmentX(Component.CENTER_ALIGNMENT);
createUserButton.setVisible(false);
//logoutButton
logoutButton = new JButton("Logout");
logoutButton.addActionListener(this);
logoutButton.setAlignmentX(Component.CENTER_ALIGNMENT);
logoutButton.setVisible(false);
//withdrawButton
withdrawButton = new JButton("Withdraw");
withdrawButton.addActionListener(this);
withdrawButton.setAlignmentX(Component.CENTER_ALIGNMENT);
withdrawButton.setVisible(false);
//depositButton
depositButton = new JButton("Deposit");
depositButton.addActionListener(this);
depositButton.setAlignmentX(Component.CENTER_ALIGNMENT);
depositButton.setVisible(false);
//Adding items to buttonPanel
buttonPanel.add(loginButton);
buttonPanel.add(switchToRegisterButton);
buttonPanel.add(switchToLoginButton);
buttonPanel.add(createUserButton);
buttonPanel.add(logoutButton);
buttonPanel.add(withdrawButton);
buttonPanel.add(depositButton);
///////////////BODY PANEL//////////////////////
//Body Panel
userPanel = new JPanel();
userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.PAGE_AXIS));
//JTextFields
//inputTopJTF
Dimension inputTopJTFDimension = new Dimension(100, 20);
inputTopJTF = new JTextField();
inputTopJTF.setMaximumSize(inputTopJTFDimension);
//inputPW1JPF
Dimension inputPW1JPFDimension = new Dimension(100, 20);
inputPW1JPF = new JPasswordField();
inputPW1JPF.setMaximumSize(inputPW1JPFDimension);
//inputPW2JPF
Dimension inputPW2JPFDimension = new Dimension(100, 20);
inputPW2JPF = new JPasswordField();
inputPW2JPF.setMaximumSize(inputPW2JPFDimension);
inputPW2JPF.setVisible(false);
//JLabels
//toastLabel
toastLabel = new JLabel("Please sign in or create new account.");
toastLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputTopJTFLabel
inputTopJTFLabel = new JLabel("Username:");
inputTopJTFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputPW1JPFLabel
inputPW1JPFLabel = new JLabel("Password:");
inputPW1JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
//inputPW2JPFLabel
inputPW2JPFLabel = new JLabel("Confirm Password:");
inputPW2JPFLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
inputPW2JPFLabel.setVisible(false);
//Adding items to userPanel
userPanel.add(toastLabel);
userPanel.add(inputTopJTFLabel);
userPanel.add(inputTopJTF);
userPanel.add(inputPW1JPFLabel);
userPanel.add(inputPW1JPF);
userPanel.add(inputPW2JPFLabel);
userPanel.add(inputPW2JPF);
///////////////CONTENT PANE/////////////////////
//Content Pane
contentPane = getContentPane();
//JLabels
//headerLabel
headerLabel = new JLabel("Bank", SwingConstants.CENTER);
headerLabel.setFont(header);
//PAGE_START
contentPane.add(headerLabel, BorderLayout.PAGE_START);
//LINE_START
//CENTER
contentPane.add(userPanel, BorderLayout.CENTER);
//LINE_END
//PAGE_END
contentPane.add(buttonPanel, BorderLayout.PAGE_END);
userPanel.setFocusable(true);
userPanel.requestFocus();
//Default Button
rootPane = getRootPane();
rootPane.setDefaultButton(loginButton);
}
call
setVisible(true);
in last line .because component added after line setvisible will not be shown until you call repaint(),revalidate().when you resize ,repaint() method get called and frame will visible correctly .so call setvisible after add all component
after line rootPane.setDefaultButton(loginButton); call setvisible
rootPane.setDefaultButton(loginButton);
setVisible(true);//after add all component to frame call setvisible method
this is full working code

How to add multiple JPanels to JFrame with different sizes

I tried to do this from stackoverflow:
adding multiple jPanels to jFrame
But that didn't seem to work out like in the example, could anyone tell me what im doing wrong?
Im trying to add multiple JPanels with each their own sizes to the JFrame. I was also hoping it was possible to give each JPanel specific sizes and ability to put them on the exact spot i want.
Picture of what i try to make:
This is my code so far:
public ReserveringenGUI(ReserveringController controller) {
this.controller = new ReserveringController();
makeFrame();
}
public void makeFrame() {
JFrame frame1 = new JFrame();
frame1.setTitle("Reserveringen");
frame1.setSize(800, 500);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel willekeurigPanel = new JPanel();
willekeurigPanel.setSize(400, 500);
willekeurigPanel.setBackground(Color.YELLOW);
willekeurigPanel.setVisible(true);
JPanel overzichtPanel = new JPanel();
overzichtPanel.setSize(400, 500);
overzichtPanel.setBackground(Color.red);
overzichtPanel.setVisible(true);
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
DateFormatter df = new DateFormatter(format);
JFormattedTextField dateBeginField = new JFormattedTextField(df);
dateBeginField.setPreferredSize(new Dimension(250, 20));
dateBeginField.setValue(new Date());
JFormattedTextField dateEndField = new JFormattedTextField(df);
dateEndField.setPreferredSize(new Dimension(250, 20));
dateEndField.setValue(new Date());
JTextField klantnummer = new JTextField();
klantnummer.setPreferredSize(new Dimension(250, 20));
JTextField artikelnummer = new JTextField();
artikelnummer.setPreferredSize(new Dimension(250, 20));
JLabel dateBeginLabel = new JLabel("Begin Datum ");
JLabel dateEndLabel = new JLabel("Eind datum: ");
JLabel klantID = new JLabel("Klant nummer: ");
JLabel artikelID = new JLabel("Artikel nummer: ");
JButton voegReserveringToe = new JButton("Voeg toe");
voegReserveringToe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
voegReserveringToeActionPerformed(evt);
}
});
willekeurigPanel.add(dateBeginLabel);
willekeurigPanel.add(dateBeginField);
willekeurigPanel.add(dateEndLabel);
willekeurigPanel.add(dateEndField);
willekeurigPanel.add(klantID);
willekeurigPanel.add(klantnummer);
willekeurigPanel.add(artikelID);
willekeurigPanel.add(artikelnummer);
willekeurigPanel.add(voegReserveringToe);
container.add(willekeurigPanel);
container.add(overzichtPanel);
frame1.add(container);
frame1.setVisible(true);
}
As discussed here, don't set the size and position of components arbitrarily. Instead, let the layout do the work, nesting as required. Use the GroupLayout shown here for the labeled input fields. Add each to the CENTER of a panel having BorderLayout, with a button in the SOUTH on the left. Finally, add both panels to an enclosing panel having GridLayout(1, 0).

My GUI window doesn't show anything

I'm trying to use a grid layout to make a GUI window. I add all my components and it compiles but when it runs it doesn't show anything. I'm trying to make a simple layout grouped and stacked like this.
{introduction message}
{time label
time input text}
{gravity label
gravity input text}
{answer label
answer text box}
{calculate button clear button}
Here is my code
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
setTitle("Falling Distance Calculator");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 5));
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setVisible(true);
}
public static void main(String[] args)
{
new TurnerRandyFallingGUI();
}
}
You've added nothing to the JFrame that your class above extends. You need to add your components to containers whose hierarchy eventually leads to the top level window, to the this if you will. In other words, you have no add(someComponent) or the functionally similar this.add(someComponent)method call in your code above.
Consider adding all of your JPanels to a single JPanel
Consider adding that JPanel to the JFrame instance that is your class by calling add(thatJPanel).
Even better would be to not extend JFrame and just to create one when needed, but that will likely be the subject of another discussion at another time.
Before setVisible (true) statement add following statements:
add (introP);
add (timeP);
add (gravityP);
add (answerP);
add (buttonsP);
There is nothing in your JFrame. That is the reason
import javax.swing.*;
import java.awt.*;
public class TurnerRandyFallingGUI extends JFrame
{
final int WINDOW_HEIGHT=500;
final int WINDOW_WIDTH=500;
public TurnerRandyFallingGUI()
{
//labels
JLabel introMessage = new JLabel("Welcome to the Falling distance"+
"calculator");
JLabel timeLabel = new JLabel("Please enter the amount of time "+
"in seconds the object was falling.");
JLabel gravityLabel = new JLabel("Enter the amount of gravity being "+
"forced onto the object");
JLabel answerLabel = new JLabel("Answer");
//text fields
JTextField fTime = new JTextField(10);
JTextField gForce = new JTextField(10);
JTextField answerT = new JTextField(10);
//buttons
JButton calculate = new JButton("Calculate");
JButton clr = new JButton("clear");
//panels
JPanel introP = new JPanel();
JPanel timeP = new JPanel();
JPanel gravityP = new JPanel();
JPanel answerP = new JPanel();
JPanel buttonsP = new JPanel();
//adding to the panels
//intro panel
introP.add(introMessage);
//time panel
timeP.add(timeLabel);
timeP.add(fTime);
//gravity panel
gravityP.add(gravityLabel);
gravityP.add(gForce);
//answer panel
answerP.add(answerLabel);
answerP.add(answerT);
//button panel
buttonsP.add(calculate);
buttonsP.add(clr);
setLayout(new GridLayout(5, 1));
this.add(introP);
this.add(timeP);
this.add(gravityP);
this.add(answerP);
this.add(buttonsP);
setTitle("Falling Distance Calculator");
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.validate();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TurnerRandyFallingGUI();
}
});
}
}
Consider the following
In GridLayout, the first parameter is Rows, Second is columns
Never set the size of JFrame manually. Use pack() method to decide
the size
Use SwingUtilities.InvokeLater() to run the GUI in another thread.

How will I change the contents of the panel with a click of a button in GUI?

Phew. I've been stuck for so long in this question. I'm doing a GUI program simulating a Pop Quiz. But I don't know what the codes to put when I want my program to be like this...
And when I click the start button, the panel should be like this...
So far, this is what I have for the start up menu...
public static void main (String []args){
JFrame f = new JFrame("Pop Quiz");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setResizable(false);
JPanel p1 = new JPanel();
p1.setSize(400,100);
p1.setLocation(0,0);
p1.setLayout(new GridLayout(3,1));
f.add(p1);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
p1.add(l1);
JLabel l2 = new JLabel("Enter your name:");
p1.add(l2);
final JTextField name = new JTextField ();
p1.add(name);
JPanel p2 = new JPanel();
p2.setSize(400,50);
p2.setLocation(0,225);
f.add(p2);
JButton start = new JButton ("Start");
p2.add(start);
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String player = name.getText();
//what should be added here to change the contents of the panel?
}
});
f.show();
}
And for the questions...
public static void main(String[] args){
JFrame f = new JFrame("Pop Quiz");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setResizable(false);
JPanel p1 = new JPanel();
p1.setSize(400,100);
p1.setBackground(Color.PINK);
f.add(p1);
JLabel question = new JLabel();
question.setText("In computers, what is the smallest and basic unit of information storage?");
p1.add(question);
JPanel p2 = new JPanel();
p2.setSize(400,175);
p2.setLocation(0,100);
p2.setLayout(new GridLayout(2,4));
f.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
f.show();
}
I anyone could help, I would really appreciate it. Thanks in advance! Have a nice day! :)
Use a CardLayout. As shown here.
Tips
Layouts
f.setLayout(null);
Use Layouts! I cannot stress this enough. Layouts might seem complicated, but they are the only workable solution to laying out complex groups of components in an GUI intended to be used on different platforms (PLAFs, screen resolutions..).
Controls
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
// ..
Given the nature of the strings used for the buttons, it seems they might best be a JComboBox, a JList or buttons in a ButtonGroup.
Deprecated methods
f.show();
This method was deprecated, your compiler should be warning you that it is deprecated or that there are further warnings that are being ignored. Look into such warnings, fix them. Methods are deprecated for a reason.
Try f.getContentPane().add(panel);
Try this..
public class test {
public static void main(String[] args) {
final JFrame f = new JFrame("Pop Quiz");
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.setResizable(false);
final JPanel p1 = new JPanel();
p1.setSize(400, 100);
p1.setLocation(0, 0);
p1.setLayout(new GridLayout(3, 1));
f.add(p1);
JLabel l1 = new JLabel("Welcome to POP Quiz!");
p1.add(l1);
JLabel l2 = new JLabel("Enter your name:");
p1.add(l2);
final JTextField name = new JTextField();
p1.add(name);
final JPanel p2 = new JPanel();
p2.setSize(400, 50);
p2.setLocation(0, 225);
f.add(p2);
JButton start = new JButton("Start");
p2.add(start);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String player = name.getText();
p1.setVisible(false);
p2.setVisible(false);
JPanel testPanel = new JPanel();
testPanel.setSize(400, 100);
testPanel.setBackground(Color.PINK);
f.add(testPanel);
JLabel question = new JLabel();
question.setText("<html>In computers, what is the smallest and basic unit<br/> of information storage?</html>");
testPanel.add(question);
JPanel p2 = new JPanel();
p2.setSize(400, 175);
p2.setLocation(0, 100);
p2.setLayout(new GridLayout(2, 4));
f.add(p2);
JButton a = new JButton("a. Bit");
p2.add(a);
JButton b = new JButton("b. Byte");
p2.add(b);
JButton c = new JButton("c. Data");
p2.add(c);
JButton d = new JButton("d. Newton");
p2.add(d);
f.show();
}
});
f.show();
}
}
Steps to achive this:
1.Create a main panel and add to JFrame
2. Add the contents to a welcomePanel in a main JPanel.
3. Once the user presses "start" button in the welcomePanel. call removeAll() on the main Container.
4.Add new contents in a contentPanel in main JPanel and call revalidate() which will update main.
Note: You do not need a seperate instance of the jframe for this

Categories