Reputation: 57
So as of now when I run the program two different panels open up. One is a JPanel and one is a JFrame. I was wondering how to either combine the two or just take the JLabel on the JPanel and put it on the JFrame I already have?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class MainQuestions {
public static void main (String args[]){
JFrame frame=new JFrame();
setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel bottomRtLabel = new JLabel("BOTTOM RIGHT LABEL");
labelPanel.add(bottomRtLabel);
frame.add(labelPanel,BorderLayout.SOUTH);
frame.setVisible(true);
Object ARRAY[]={"French","English","Portugese","Spanish"};
String answer=(String)JOptionPane.showInputDialog(frame, "What language predominately spoken in Latin American countries?","World Geography Review", JOptionPane.PLAIN_MESSAGE, null, ARRAY, null);
if (answer==null)
{
//System.exit(0);
}
else if (answer.equals("Spanish"))
{
JOptionPane.showMessageDialog(null, "Correct!", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
//System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, wrong answer.", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
//System.exit(0);
}
}
private static void setLayout(BorderLayout borderLayout) {
// TODO Auto-generated method stub
}
}
Upvotes: 1
Views: 507
Reputation: 379
You may extend JPanel. Something like this:
public class MainQuestions {
public static void main (String args[]){
JFrame frame=new JFrame();
YourClass labelPanel = new YourClass();
frame.setLayout(new BorderLayout());
frame.add(labelPanel,BorderLayout.SOUTH);
setVisible(true);
}
class YourClass extends JPanel {
YourClass(){
//add label there
}
}
Upvotes: 1
Reputation: 804
I find many errors in your code. For starts:
JFrame frame=new JFrame();
setLayout(new BorderLayout());
Should be:
JFrame frame=new JFrame();
frame.setLayout(new BorderLayout());
Also, you might want to move all your code to a Constructor. So your main may look like this:
public static void main (String args[]){
new MainQuestions();
}
Then inside the constructor, move all your code:
public MainQuestions(){
JFrame frame=new JFrame();
frame.setLayout(new BorderLayout());
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); // Read up on GridBagLayout
JLabel bottomRtLabel = new JLabel("BOTTOM RIGHT LABEL");
labelPanel.add(bottomRtLabel);
frame.add(labelPanel,BorderLayout.SOUTH);
frame.setVisible(true);
String ARRAY[]={"French","English","Portugese","Spanish"}; // Notice how I changed the type to String
String answer=(String)JOptionPane.showInputDialog(frame, "What language predominately spoken in Latin American countries?","World Geography Review", JOptionPane.PLAIN_MESSAGE, null, ARRAY, null);
if (answer==null)
{
//code
}
else if (answer.equals("Spanish"))
{
JOptionPane.showMessageDialog(null, "Correct!", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
}
else
{
JOptionPane.showMessageDialog(null, "Sorry, wrong answer.", "World Geography Review", JOptionPane.PLAIN_MESSAGE,null);
}
System.exit(0);
}
}
I haven't run this edited code yet. Try it out by typing it yourself and debugging.
Upvotes: 1