so im kinda new to programming and I'm having a weird problem. So I'm trying to add my 'Info' panel with labels in them to my 'JFrame' but when I run the program the panel doesn't show up. Also how would I make it to where the panel would appear in the top Left of the JFrame.
private Lilac player;
private int[] xPos;
private JLabel lblShowLives, lblShowScore, lblTimer;
private int lives, score, seconds, minutes;
private ImageIcon[] imgBackground;
private Timer backgroundTimer, cutsceneTimer, Timer;
private AudioStream auStream;
private AudioPlayer auPlayer;
private InputStream in;
private DecimalFormat df, df2;
public FreedomPlanet() {
df = new DecimalFormat("00");
df2 = new DecimalFormat("0000");
auPlayer = AudioPlayer.player;
// creates the player
player = new Lilac();
//
lives = 3;
score = 0;
seconds = 0;
minutes = 0;
imgBackground = new ImageIcon[2];
// gets the image for the background inside of a for loop
for (int i = 0; i < imgBackground.length; i++)
{
imgBackground[i] = new ImageIcon("Stage\\Forest.gif");
}
xPos = new int[] {0, imgBackground[0].getIconWidth()};
// sets the amount of time for each timer
backgroundTimer = new Timer(50, this);
Timer = new Timer(1000, this);
// creates a label that displays "TIME"
JLabel lblTime = new JLabel("TIME");
lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));
// label that displays the time
lblTimer = new JLabel();
lblTimer.setPreferredSize(new Dimension(100, 30));
lblTimer.setFont(new Font("Britannic Bold", Font.BOLD, 18));
lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
lblTimer.setText(df.format(minutes) + ":" + df.format(seconds));
// creates a label that says "SCORE"
JLabel lblScore = new JLabel("SCORE");
lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));
// label that displays the users score
lblShowScore = new JLabel();
lblShowScore.setPreferredSize(new Dimension(100, 30));
lblShowScore.setFont(new Font("Britannic Bold", Font.BOLD, 18));
lblShowScore.setHorizontalAlignment(SwingConstants.CENTER);
lblShowScore.setText(df.format(score));
// creates a panel called Info and and adds components to them
JPanel Info = new JPanel();
Info.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
Info.setPreferredSize(new Dimension(270, 180));
Info.add(lblTime);
Info.add(lblTimer);
Info.add(lblScore);
Info.add(lblShowScore);
addKeyListener(this);
setFocusable(true);
setLayout(null);
JFrame frame = new JFrame();
frame.add(Info);
frame.add(this, BorderLayout.CENTER);
frame.setContentPane(this);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// starts the background timer
backgroundTimer.start();
//Timer.start();
// sets the location of the player
player.setLocation(170, 395);
Timer.start();
}
Related
When i created my gui in java, i set the background color to blackish and there seems to be a pixel line of white at the right most and bottom most sections of my gui. However when i resize this gui, that like goes away and the gui is completely black. Does anyone know why this is happening? I need my gui to set resizeable to false so resizing the gui to fix this problem will not work.
package JavaQuizGameTut;
import java.awt.ActiveEvent.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class Quiz implements ActionListener{
String[] questions = {
"Which company created Java?",
"What year was Java created?",
"What was java originally called?",
"Who was credited for making java?"
};
String[][] options = {{"Sun Microsystems", "Starbucks", "Microsoft", "Alphabet"},
{"1989", "1996", "1972", "1492"},
{"Apple", "Latte", "Oak", "Koffing"},
{"Steve Jobs", "Bill Gates", "James Gosling", "Mark Zuckerburg"}
};
char[] answers = {'A', 'B', 'C', 'C'};
char guess;
char answer;
int index;
int correct_guesses = 0;
int total_questions = questions.length;
int result;
int seconds;
JFrame frame = new JFrame();
JTextField textfield = new JTextField();
JTextArea textarea = new JTextArea();
JButton buttonA = new JButton();
JButton buttonB = new JButton();
JButton buttonC = new JButton();
JButton buttonD = new JButton();
JLabel answer_labelA = new JLabel();
JLabel answer_labelB = new JLabel();
JLabel answer_labelC = new JLabel();
JLabel answer_labelD = new JLabel();
JLabel time_label = new JLabel();
JLabel seconds_left = new JLabel();
JTextField number_right = new JTextField();
JTextField percentage = new JTextField();
public Quiz() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(650, 650);
frame.getContentPane().setBackground(new Color(50, 50, 50));
frame.setLayout(null);
// frame.setResizable(false);
textfield.setBounds(0, 0, 650, 50);
textfield.setBackground(new Color(25,25, 25));
textfield.setForeground(new Color(25, 255, 0));
textfield.setFont(new Font("Ink Free", Font.PLAIN, 30));
textfield.setBorder(BorderFactory.createBevelBorder(1));
textfield.setHorizontalAlignment(JTextField.CENTER);
textfield.setEditable(false);
frame.add(textfield);
frame.setVisible(true);
}
public void nextQuestion() {
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public void displayAnswer() {
}
public void results() {
}
}
The background color does cover the entire frame only the reason you are seeing a grey bar above it is that you have placed a textfield on top of it and you have set its background colour to be grey.
As in here:
textfield.setBounds(0, 0, 650, 50);
textfield.setBackground(new Color(25,25, 25));
Try changing textfield's color or temporarily commenting it and you'll see whole frame black.
If nothing fixes it then there's something wrong with your system as it is running fine in my system.
You can try running it in another system.
I am currently working on the user interface for a quiz game. It currently looks like this:
I would like it to look like this:
However, I'm not sure about how to structure the screen accordingly, particularly the following things:
Splitting the screen into s several ones (header, where the the timer is... positioned to to the right), ensuring timer is on the right hand side
Creating a sidebar for scores
Creating a main area for questions
Centering all the elements slightly so that there is some padding around it
Not losing the elements drawn in paintComponent
Which type of layouts would work best from the outset?
My code is as follows (note that the bulk of the work is done in createWindow and paintComponent is how I draw the responses on screen):
final class Gui extends JFrame {
static String message;
static String answer;
private String alertMessage;
private String guesses;
private Display display;
private JTextArea textArea;
private JButton startButton;
private JLabel timerLabel;
private JButton nextButton;
private int badGuesses;
private boolean gameOver;
private Timer timer;
private ArrayList<JButton> alphabetButtons = new ArrayList<>();
Gui() {
createWindow();
}
public enum GuiText {
START("Start"),
QUIT("Quit"),
SUBMIT("Submit"),
RESET("Reset"),
SEND("Send"),
NEXT(">"),
PREVIOUS("<"),
PAUSE("Pause");
private String guiText;
GuiText(String guiText) {
this.guiText = guiText;
}
#Override
public String toString() {
return guiText;
}
}
/**
* This class defines the panel that occupies the large central area in the
* main panel. The paintComponent() method in this class is responsible for
* drawing the content of that panel. It shows everything that that the user
* is supposed to see, based on the current values of all the instance variables.
*/
private class Display extends JPanel {
Display() {
setPreferredSize(new Dimension(1000, 250));
setBackground(new Color(0x00bcda));
setFont(new Font("Tahoma", Font.BOLD, 20));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D) g).setStroke(new BasicStroke(3));
if (message != null) {
g.setColor(Color.DARK_GRAY);
g.drawString(message, 30, 120);
}
if (alertMessage != null) {
g.setColor(Color.DARK_GRAY);
g.drawString(alertMessage, 30, 150);
}
if (gameOver) {
alertMessage = "Click on \"Next\" to play again.";
} else {
g.drawString("Guesses remaining: " + (3 - badGuesses), 770, 40);
}
g.setColor(Color.DARK_GRAY);
if (answer != null) {
for (int i = 0; i < answer.length(); i++) {
if (String.valueOf(answer.charAt(i)).trim().length() > 0) {
g.drawLine(30 + i * 70, 210, 70 + i * 70, 210);
if (guesses.indexOf(answer.charAt(i)) >= 0) {
g.drawString(String.valueOf(answer.charAt(i)), 45 + i * 70, 195);
}
}
}
}
}
}
/**
* The constructor that creates the main panel, which is represented
* by this class. It makes all the buttons and subpanels and adds
* them to the main panel.
*/
private void createWindow() {
setJMenuBar(menuBarCreator());
// The ActionListener that will respond to button clicks.
ButtonHandler buttonHandler = new ButtonHandler();
// Create the subpanels and add them to the main panel.
display = new Display();
setLayout(new BorderLayout(3, 3));
add(display, BorderLayout.CENTER);
// Add timer panel
JPanel timerPanel = new JPanel();
timerPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
timerPanel.setBackground(new Color(0x00bcda));
add(timerPanel, BorderLayout.PAGE_START);
// Add timer label
timerLabel = new JLabel("01:00", SwingConstants.RIGHT);
timerLabel.setFont(new Font("Arial", Font.BOLD, 20));
timerLabel.setHorizontalAlignment(JLabel.RIGHT);
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = 0;
timerLabel.setForeground(Color.black);
timerPanel.add(timerLabel, c);
// Add left panel
JPanel leftPanel = new JPanel(new GridBagLayout());
c = new GridBagConstraints();
leftPanel.setBorder(new EmptyBorder(25, 25, 25, 5));
leftPanel.setBackground(new Color(0x00bcda));
add(leftPanel, BorderLayout.WEST);
// Add previous button
JButton previousButton = new JButton(String.valueOf(GuiText.PREVIOUS));
previousButton.setFont(new Font("Arial", Font.PLAIN, 60));
previousButton.addActionListener(buttonHandler);
c.gridx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 0;
c.gridy = 0;
leftPanel.add(previousButton, c);
// Add right panel
JPanel rightPanel = new JPanel(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
rightPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
rightPanel.setBackground(new Color(0x00bcda));
add(rightPanel, BorderLayout.EAST);
// Add next button
nextButton = new JButton(String.valueOf(GuiText.NEXT));
nextButton.setFont(new Font("Arial", Font.PLAIN, 60));
nextButton.addActionListener(buttonHandler);
c.gridx = 1;
c.gridy = 0;
rightPanel.add(nextButton, c);
// Add actual timer
initialiseTimer();
// Add bottom panel
JPanel bottomPanel = new JPanel(new GridBagLayout());
GridBagConstraints bottomPanelConstraints = new GridBagConstraints();
bottomPanelConstraints.fill = GridBagConstraints.HORIZONTAL;
add(bottomPanel, BorderLayout.PAGE_END);
setBackground(new Color(100, 0, 0));
// Add primary button panel to bottom panel
JPanel primaryButtonPanel = new JPanel();
primaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
primaryButtonPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(primaryButtonPanel, c);
// Add text area
textArea = new JTextArea(1, 10);
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
textArea.setFont(new Font("Arial", Font.PLAIN, 24));
textArea.setBackground(new Color(0xCCCCCC));
textArea.setEditable(false);
primaryButtonPanel.add(textArea, c);
// Add buttons
JButton sendButton = new JButton(String.valueOf(GuiText.SEND));
sendButton.addActionListener(buttonHandler);
sendButton.setFont(new Font("Arial", Font.PLAIN, 24));
primaryButtonPanel.add(sendButton);
// Add secondary button panel
JPanel secondaryButtonPanel = new JPanel();
secondaryButtonPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
secondaryButtonPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(secondaryButtonPanel, c);
// Add secondary buttons
startButton = new JButton(GuiText.START.toString());
startButton.addActionListener(buttonHandler);
startButton.setFont(new Font("Arial", Font.PLAIN, 24));
secondaryButtonPanel.add(startButton);
JButton pauseButton = new JButton(GuiText.PAUSE.toString());
pauseButton.setFont(new Font("Arial", Font.PLAIN, 24));
pauseButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(pauseButton);
JButton quitButton = new JButton(GuiText.QUIT.toString());
quitButton.setFont(new Font("Arial", Font.PLAIN, 24));
quitButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(quitButton);
JButton submitButton = new JButton(GuiText.SUBMIT.toString());
submitButton.setFont(new Font("Arial", Font.PLAIN, 24));
submitButton.addActionListener(buttonHandler);
secondaryButtonPanel.add(submitButton);
// Add keyboard panel
JPanel keyboardPanel = new JPanel();
keyboardPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
keyboardPanel.setBackground(new Color(0xFFFFFF));
c.gridx = 0;
c.gridy = 5;
c.fill = GridBagConstraints.HORIZONTAL;
bottomPanel.add(keyboardPanel, c);
keyboardPanel.setLayout(new GridLayout(3, 10, 5, 5));
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {
JButton button = new JButton(String.valueOf(alphabet));
button.addActionListener(buttonHandler);
button.setFont(new Font("Arial", Font.PLAIN, 20));
keyboardPanel.add(button);
alphabetButtons.add(button);
}
}
For this particular set of requirements, a SpringLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html) is perfect, though it requires a lot of lines of code, and is hard to read. However it it is one of the most flexibility 'out of the box' layout managers.
SpringLayout works by applying constraints to elements, bringing them in relative position to each other (including relative to the container). This is flexible, because you are able to achieve almost all configurations where the main focus is the relative position of elements (including some padding and thelike).
Incidentally, you should also be able to achieve what you need with a GridBagLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html), but that might restrict you later on.
So, I want to paint something on a Panel, using PaintComponent. But I want to do it in a Constructor. So, for example:
public class Interfata extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawOval(500, 500, 50, 40);
}
public Interfata()
{
// code here
}
public static void main(String[] args)
{
Interfata i = new Interfata();
}
The code should be like this model. The problem is that I don't know how to call the paintComponent method on a panel that create in the Constructor. ( Even though it may seem inefficient, I create the frame, panel and all the functions in the constructor, so, that I just have to instantiate that object in the main method). If I weren't clear enough, please do ask for any detail you may find suitable.
Edit: So to be more specific, this is how my Constructor looks like:
public Interfata()
{
// main interface
JFrame frame = new JFrame("Queue");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 450);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((d.getWidth() - frame.getWidth()) / 2);
int y = (int) ((d.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
//simulation interface
JFrame f = new JFrame("Simulare");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(900, 800);
int x1 = (int) ((d.getWidth() - frame.getWidth()) / 2);
int y1 = (int) ((d.getHeight() - frame.getHeight()) / 2);
f.setLocation(x1, y1);
JPanel panel = new JPanel();
panel.setLayout(null);
JPanel panel1 = new JPanel();
panel1.setLayout(null);
JLabel l1 = new JLabel("Intervalul minim de asteptare: ");
JTextArea tf1 = new JTextArea();
panel.add(l1);
l1.setBounds(5,10,200,25);
panel.add(tf1);
tf1.setBounds(200,10,300,20);
JLabel l2 = new JLabel("Intervalul maxim de asteptare: ");
JTextArea tf2 = new JTextArea();
panel.add(l2);
l2.setBounds(5,50,200,25);
panel.add(tf2);
tf2.setBounds(200,50,300,20);
JLabel l3 = new JLabel("Duarata minima pentru serviciu: ");
JTextArea tf3 = new JTextArea();
panel.add(l3);
l3.setBounds(5,90,200,25);
panel.add(tf3);
tf3.setBounds(200,90,300,20);
JLabel l4 = new JLabel("Durata maxima pentru serviciu: ");
JTextArea tf4 = new JTextArea();
panel.add(l4);
l4.setBounds(5,130,200,25);
panel.add(tf4);
tf4.setBounds(200,130,300,20);
JLabel l5 = new JLabel("Numarul de cozi: ");
JTextArea tf5 = new JTextArea();
panel.add(l5);
l5.setBounds(5,170,100,25);
panel.add(tf5);
tf5.setBounds(200,170,300,20);
JLabel l6 = new JLabel("Intervalul de simulare: ");
JTextArea tf6 = new JTextArea();
panel.add(l6);
l6.setBounds(5,210,150,25);
panel.add(tf6);
tf6.setBounds(200,210,300,20);
JButton b1 = new JButton("Simulare");
panel.add(b1);
b1.setBounds(700,100,100,35);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
f.setVisible(true);
}
});
frame.setContentPane(panel);
frame.setVisible(true);
f.setContentPane(panel1);
}
What I want to do exactly, is paint something in the panel of the NEW FRAME, or more specificaly, in the frame f.
I am trying to make the text field for quiz_7 set up, however, it is not showning up like the remaning six, i tried putting an parameter as 360, no text field. Changed it 340, but i only see half a textfield.
import java.awt.Container;
import javax.swing.*;
public class QuizEntry {
JPanel textPanel,panelForTextFields;
JLabel Entry_for_Quizes, quiz1, quiz2, quiz3, quiz4, quiz5, quiz6;
JTextField quiz_1, quiz_2, quiz_3, quiz_4, quiz_5, quiz_6;
JTextField quiz_7;
private JLabel quiz7;
public JPanel createContentPane() {
// We create a bottom JPanel to place everything on.
JPanel totalGUI1 = new JPanel();
totalGUI1.setLayout(null);
Entry_for_Quizes = new JLabel("Quiz Entry ");
Entry_for_Quizes.setLocation(0, 0);
Entry_for_Quizes.setSize(400, 400);
Entry_for_Quizes.setHorizontalAlignment(4);
totalGUI1.add(Entry_for_Quizes);
// Creation of a Panel to contain the JLabels
textPanel = new JPanel();
textPanel.setLayout(null);
textPanel.setLocation(10, 35);
textPanel.setSize(100, 600);
totalGUI1.add(textPanel);
// Username Label
quiz1 = new JLabel("Quiz 1");
quiz1.setLocation(-20, 0);
quiz1.setSize(70, 40);
quiz1.setHorizontalAlignment(4);
textPanel.add(quiz1);
// Login Label
quiz2 = new JLabel("Quiz 2");
quiz2.setLocation(-20, 60);
quiz2.setSize(70, 40);
quiz2.setHorizontalAlignment(4);
textPanel.add(quiz2);
// Username Label
quiz3 = new JLabel("Quiz 3");
quiz3.setLocation(-20, 120);
quiz3.setSize(70, 40);
quiz3.setHorizontalAlignment(4);
textPanel.add(quiz3);
// Login Label
quiz4 = new JLabel("Quiz 4");
quiz4.setLocation(-20, 180);
quiz4.setSize(70, 40);
quiz4.setHorizontalAlignment(4);
textPanel.add(quiz4);
// Username Label
quiz5 = new JLabel("Quiz 5");
quiz5.setLocation(-20, 240);
quiz5.setSize(70, 40);
quiz5.setHorizontalAlignment(4);
textPanel.add(quiz5);
// L
quiz6 = new JLabel("Quiz 6");
quiz6.setLocation(-20, 300);
quiz6.setSize(70, 40);
quiz6.setHorizontalAlignment(4);
textPanel.add(quiz6);
quiz7 = new JLabel("Quiz 7");
quiz7.setLocation(-20, 350);
quiz7.setSize(70, 40);
quiz7.setHorizontalAlignment(4);
textPanel.add(quiz7);
//////////////////////////////////////////////////////////////
panelForTextFields = new JPanel();
panelForTextFields.setLayout(null);
panelForTextFields.setLocation(110, 40);
panelForTextFields.setSize(100, 350);
totalGUI1.add(panelForTextFields);
// quiz Textfield
quiz_1 = new JTextField(8);
quiz_1.setLocation(0, 0);
quiz_1.setSize(100, 30);
panelForTextFields.add(quiz_1);
quiz_2 = new JTextField(8);
quiz_2.setLocation(0, 60);
quiz_2.setSize(100, 30);
panelForTextFields.add(quiz_2);
quiz_3 = new JTextField(8);
quiz_3.setLocation(0, 120);
quiz_3.setSize(100, 30);
panelForTextFields.add(quiz_3);
quiz_4 = new JTextField(8);
quiz_4.setLocation(0, 180);
quiz_4.setSize(100, 30);
panelForTextFields.add(quiz_4);
quiz_5 = new JTextField(8);
quiz_5.setLocation(0, 240);
quiz_5.setSize(100, 30);
panelForTextFields.add(quiz_5);
quiz_6 = new JTextField(8);
quiz_6.setLocation(0, 300);
quiz_6.setSize(100, 30);
panelForTextFields.add(quiz_6);
quiz_7 = new JTextField(8);
quiz_7.setLocation(0, 340);
quiz_7.setSize(100, 30);
panelForTextFields.add(quiz_7);
return totalGUI1;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Quiz Entry");
QuizEntry demo = new QuizEntry();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 700);
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Basically change size: for panelForTextFields. From 350 to 500 for example.
To make code a bit generic I would write something like:
panelForTextFields.setSize(100, 6*80);
where 6 is count of JTextFields.
If we will go further, I would create list of JTextField like:
List<JTextField> list = new ArrayList<JTextField>();
list.add(quiz_1);
list.add(quiz_2);
list.add(quiz_3);
list.add(quiz_4);
list.add(quiz_5);
list.add(quiz_6);
and after would write:
final int GAP = 40;
panelForTextFields.setSize(100, 6*( quiz_1.getHeight() + GAP));
Since you used the same logic to configure JTextfields you can use now list like:
quiz_1 = new JTextField(8);
panelForTextFields.add(quiz_1);
quiz_2 = new JTextField(8);
panelForTextFields.add(quiz_2);
quiz_3 = new JTextField(8);
panelForTextFields.add(quiz_3);
quiz_4 = new JTextField(8);
panelForTextFields.add(quiz_4);
quiz_5 = new JTextField(8);
panelForTextFields.add(quiz_5);
quiz_6 = new JTextField(8);
panelForTextFields.add(quiz_6);
quiz_7 = new JTextField(8);
panelForTextFields.add(quiz_7);
list.add(quiz_1);
list.add(quiz_2);
list.add(quiz_3);
list.add(quiz_4);
list.add(quiz_5);
list.add(quiz_6);
list.add(quiz_7);
for(int k = 0; k<list.size(); k++){
JTextField f = list.get(k);
f.setLocation(0, k*60);
f.setSize(100, 30);
}
final int GAP = 40;
panelForTextFields.setSize(100, 6*( quiz_1.getHeight() + GAP));
I'm having an issue where my JRadioButtons will not appear until moused over. I've looked up this issue before and it seems most people have solved it using layouts. However, I am not supposed to use them for my assignment, being completely new to graphics with Java I'm really struggling. Any ideas? Anything is appreciated.
import java.awt.GridLayout;
import javax.swing.*;
public class InsuarancePolicyApp {
public static void main(String[] args) {
//JFrame
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(800, 600);
f.setLayout(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Insurance Policy Application");
//JButtons
JButton addClient = new JButton("Add Client");
JButton openPolicy = new JButton("Open Policy");
f.getContentPane().add(addClient);
f.getContentPane().add(openPolicy);
openPolicy.setSize(300, 60);
addClient.setSize(380,60);
addClient.setLocation(30, 180);
openPolicy.setLocation(450, 180);
//JTextField
JTextField name = new JTextField("Name: ");
f.getContentPane().add(name);
name.setLocation(30,30);
name.setSize(380, 30);
//JList
String[] clientarray = {"Mark Mywords", "Jim Class", "Stan Dupp", "Mel Onhead", "Bob's Yoyo Shop", "Toys Aren't Us", "The Fish Rack"};
JList clients = new JList(clientarray);
f.getContentPane().add(clients);
clients.setLocation(30, 280);
clients.setSize(380, 250);
String[] policyarray = {"Policy 002354","Policy 005345", "Depreciable Policy 0789423", "Expirable Policy 009724"};
JList policies = new JList(policyarray);
f.getContentPane().add(policies);
policies.setLocation(450, 280);
policies.setSize(300, 250);
//JScrollPane
JScrollPane clientScroll = new JScrollPane(clients, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(clientScroll);
clientScroll.setLocation(30,280);
clientScroll.setSize(380,250);
JScrollPane policiesScroll = new JScrollPane(policies, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add(policiesScroll);
policiesScroll.setLocation(450,280);
policiesScroll.setSize(300,250);
//JradioButons
JRadioButton b1 = new JRadioButton("Company",false);
JRadioButton b2 = new JRadioButton("Individual",false);
JRadioButton b3 = new JRadioButton("Standard",false);
JRadioButton b4 = new JRadioButton("Depreciable",false);
JRadioButton b5 = new JRadioButton("Expirable",false);
//ButtonGroups
ButtonGroup clientsGroup = new ButtonGroup();
b1.setLocation(120, 70);
b1.setSize(100,30);
b2.setLocation(260, 70);
b2.setSize(100,30);
f.getContentPane().add(b2);
f.getContentPane().add(b1);
clientsGroup.add(b1);
clientsGroup.add(b2);
ButtonGroup policiesGroup = new ButtonGroup();
b3.setLocation(600, 10);
b4.setLocation(600, 60);
b5.setLocation(600, 110);
b3.setSize(100,60);
b4.setSize(100,60);
b5.setSize(100,60);
f.getContentPane().add(b3);
f.getContentPane().add(b4);
f.getContentPane().add(b5);
policiesGroup.add(b3);
policiesGroup.add(b4);
policiesGroup.add(b5);
//Jlabel
JLabel label = new JLabel("Type:");
f.getContentPane().add(label);
label.setLocation(30, 55);
label.setSize(60,60);
JLabel label2 = new JLabel("Type:");
f.getContentPane().add(label2);
label2.setLocation(545, 10);
label2.setSize(60,60);
}
}
The problem is that the JRadioButtons are not being painted as the JFrame has already been displayed when they are added.
You need to call JFrame#setVisible after you add all the components to the container:
f.setVisible(true);
Aside: Don't use absolute positioning (null layout), use a layout manager instead. Using a layout manager removes the need for the setting component sizes & positions.