[英]How to combine a JLabel and JFrame?
So as of now when I run the program two different panels open up. 因此,到目前为止,当我运行该程序时,将打开两个不同的面板。 One is a JPanel and one is a JFrame. 一个是JPanel,另一个是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? 我想知道如何将两者结合或仅将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
}
}
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. 自己输入并调试即可尝试。
You may extend JPanel. 您可以扩展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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.