Итак, теперь, когда я запускаю программу, открываются две разные панели. Один из них - JPanel, а другой - JFrame. Мне было интересно, как объединить эти два или просто взять JLabel на JPanel и поместить его на JFrame, который у меня уже есть?
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
}
}
Я нахожу много ошибок в вашем коде. Для запуска:
JFrame frame=new JFrame();
setLayout(new BorderLayout());
Должно быть:
JFrame frame=new JFrame();
frame.setLayout(new BorderLayout());
Кроме того, вы можете переместить весь свой код в конструктор. Таким образом, ваш основной может выглядеть так:
public static void main (String args[]){
new MainQuestions();
}
Затем внутри конструктора переместите весь свой код:
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);
}
}
Я еще не запускал этот отредактированный код. Попробуйте это, набрав его самостоятельно и отлаживая.
Вы можете расширить JPanel. Что-то вроде этого:
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
}
}