Java JLabel不会出现在JFrame上
我已经搜索了很多次,想知道为什么我的JLabel没有出现,但我无法找到答案。我试着玩弄setLayout(),但我想不出来。除我的2个标签外,其他所有标签都显示出来了。所有带有图片的四个按钮都会显示,但这些按钮不会显示,我无法理解为什么需要设置它们的位置或边界??请帮忙! 谢谢 您需要为两个标签(即welcomelabel和hi)设置setBounds(),类似于对四个按钮所做的操作Java JLabel不会出现在JFrame上,java,swing,jframe,jpanel,layout-manager,Java,Swing,Jframe,Jpanel,Layout Manager,我已经搜索了很多次,想知道为什么我的JLabel没有出现,但我无法找到答案。我试着玩弄setLayout(),但我想不出来。除我的2个标签外,其他所有标签都显示出来了。所有带有图片的四个按钮都会显示,但这些按钮不会显示,我无法理解为什么需要设置它们的位置或边界??请帮忙! 谢谢 您需要为两个标签(即welcomelabel和hi)设置setBounds(),类似于对四个按钮所做的操作 您尚未将setBounds()设置为在单击按钮时创建的其他帧的标签及其工作方式,因为默认情况下,JPanel具有
您尚未将setBounds()设置为在单击按钮时创建的其他帧的标签及其工作方式,因为默认情况下,JPanel具有flowlayout,并且它正在相应地工作如果您希望查看在面板上创建的JLabel,则需要将标签设置为可见 因此,在您的代码中,我将编辑以下内容:
...
//Declaring and setting properties of the JFrame
JFrame mainFrame = new JFrame("iLearn");
mainFrame.setVisible(true);
mainFrame.setSize(1000,800);
mainFrame.setBackground(Color.white);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Declaring and setting properties of the JPanel
JPanel panel = new JPanel();
mainFrame.add(panel);
//Declaring objects
Font fontStyle1 = new Font("Arial",Font.PLAIN,25);
JLabel welcomeLabel = new JLabel("Welcome to iLearn");
welcomeLabel.setVisible(true)
JLabel hi = new JLabel("HI");
hi.setVisible(true);
JButton mathButton = new JButton("Math");
JButton scienceButton = new JButton("Science");
JButton englishButton = new JButton("English");
JButton computersButton = new JButton("Computers");
...
至于如何使用true
设置大型机可见,您应该使用JLabel执行相同的操作
以下是过去提出的一些有用的问题:
您想要的布局可以通过使用三种布局的组合来实现
GridLayout
FlowLayout
BorderLayout
,用于将标签约束到顶部,并将按钮约束到剩余可用空间import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SummativeFile {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
//Declaring and setting properties of the JFrame
JFrame mainFrame = new JFrame("iLearn");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout(5, 20));
contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
mainFrame.setContentPane(contentPane);
//Declaring and setting properties of the JPanel
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.PAGE_START);
//Declaring labels
JLabel welcomeLabel = new JLabel("Welcome to iLearn");
JLabel hi = new JLabel("HI");
//Adding objects to panel
panel.add(hi);
panel.add(welcomeLabel);
//Declaring buttons and their container
JPanel buttonMenuPanel = new JPanel(new GridLayout(2, 0, 20, 20));
JButton mathButton = new JButton("Math");
JButton scienceButton = new JButton("Science");
JButton englishButton = new JButton("English");
JButton computersButton = new JButton("Computers");
// make the buttons larger
Insets insets = new Insets(15, 5, 15, 5);
mathButton.setMargin(insets);
scienceButton.setMargin(insets);
englishButton.setMargin(insets);
computersButton.setMargin(insets);
buttonMenuPanel.add(mathButton);
buttonMenuPanel.add(scienceButton);
buttonMenuPanel.add(englishButton);
buttonMenuPanel.add(computersButton);
contentPane.add(buttonMenuPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
}
其他提示:
EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是大写常量
),并一致使用它李>
Runnable
的代码与SwingUtilities.invokeLater(..
实现了这一点setVisible(true);
应在添加所有组件后完成,并调用pack()
对其进行布局。打包容器会导致设置大小过时(pack()
将确定正确的大小)Insets
,以及调整GUI大小的EmptyBorder
。有关详细信息,请参阅Java文档通常,当不在事件调度线程(EDT)上时,您不能修改Swing UI组件。因此,您的主函数实际上应该以
SwingUtilities.invokeLater(…)开始
,它切换到EDT,然后您将创建主UI。至少,您可以创建未实现的整个UI,并调用mainFrame.setVisible(true)
作为最后一步。为了获得最佳兼容性,请同时执行这两项操作。在以后的问题中,将问题代码简化到绝对最小。如果问题出在大型机布局上,请删除所有的ActionListener
代码。是否与问题字体相关?否?删除所有字体代码。您的问题越简单,您越有可能成为一名re的目的是(a)自己发现问题,(b)吸引有兴趣的眼球。如果你提供了一堆不必要的代码,一个本来可以提供帮助的人可能会失去兴趣——或者写下这些评论,而不是寻找问题。获取mainFrame.setVisible(true)并使它成为您最后调用的对象,考虑减少使用的帧的数量,并考虑使用A<代码>卡布局< /代码>。instead@AJNeufeld,您是否尝试设置它们的边界?
-您不应该设置任何组件的边界,这是布局管理器的工作。“如果要查看在面板上创建的JLabel,需要将标签设置为可见。”不正确。组件添加到自身可见的容器后,默认情况下是可见的。“需要为两个标签设置setBounds()。。“在您理解为什么设置组件边界是不可取的之前,请提供关于布局跨平台GUI的建议。请参阅我回答中“其他提示”下的第(2)点,了解这种理解的开始。如果这有助于解决问题,请咨询。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SummativeFile {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
//Declaring and setting properties of the JFrame
JFrame mainFrame = new JFrame("iLearn");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout(5, 20));
contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));
mainFrame.setContentPane(contentPane);
//Declaring and setting properties of the JPanel
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.PAGE_START);
//Declaring labels
JLabel welcomeLabel = new JLabel("Welcome to iLearn");
JLabel hi = new JLabel("HI");
//Adding objects to panel
panel.add(hi);
panel.add(welcomeLabel);
//Declaring buttons and their container
JPanel buttonMenuPanel = new JPanel(new GridLayout(2, 0, 20, 20));
JButton mathButton = new JButton("Math");
JButton scienceButton = new JButton("Science");
JButton englishButton = new JButton("English");
JButton computersButton = new JButton("Computers");
// make the buttons larger
Insets insets = new Insets(15, 5, 15, 5);
mathButton.setMargin(insets);
scienceButton.setMargin(insets);
englishButton.setMargin(insets);
computersButton.setMargin(insets);
buttonMenuPanel.add(mathButton);
buttonMenuPanel.add(scienceButton);
buttonMenuPanel.add(englishButton);
buttonMenuPanel.add(computersButton);
contentPane.add(buttonMenuPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
}