JLabel没有出现在JFrame上

时间:2017-12-10 23:51:18

标签: java swing jframe jpanel layout-manager

我已多次搜索,以找出为什么我的JLabel没有出现,但我无法理解。我尝试使用setLayout(),但我无法弄明白。除了我的2个标签外,其他所有东西都出现了。带有图片的所有四个按钮都显示出来,但这些没有,我无法弄清楚为什么我需要设置它们的位置或界限?请帮忙! 感谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class summativeFile {

public static void main (String[] args){

    //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");
    JLabel hi = new JLabel("HI");
    JButton mathButton = new JButton("Math");
    JButton scienceButton = new JButton("Science");
    JButton englishButton = new JButton("English");
    JButton computersButton = new JButton("Computers");

    //Setting attributes of objects
    welcomeLabel.setFont(fontStyle1);
    mathButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//math.png"));
    scienceButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//science.png"));
    englishButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//english.png"));
    computersButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//computers.png"));

    //Setting bounds of objects
    mathButton.setBounds(150,150,300,200);
    scienceButton.setBounds(550,150,300,200);
    englishButton.setBounds(150,450,300,200);
    computersButton.setBounds(550,450,300,200);

    //Adding objects to panel
    panel.add(hi);
    panel.add(welcomeLabel);
    panel.add(mathButton);
    panel.add(scienceButton);
    panel.add(englishButton);
    panel.add(computersButton);

    mathButton.addActionListener (new mathAction());
    scienceButton.addActionListener (new scienceAction());
    englishButton.addActionListener (new englishAction());
    computersButton.addActionListener (new computersAction());

}
static class mathAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame mathFrame = new JFrame("Mathematics");
        mathFrame.setVisible(true);
        mathFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Mathematics");
        JPanel mathPanel = new JPanel();
        mathFrame.add(mathPanel);
        mathPanel.add(label);
    }
}
static class scienceAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame scienceFrame = new JFrame("Science");
        scienceFrame.setVisible(true);
        scienceFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Science");
        JPanel sciencePanel = new JPanel();
        scienceFrame.add(sciencePanel);
        sciencePanel.add(label);
    }
}
static class englishAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame englishFrame= new JFrame("English");
        englishFrame.setVisible(true);
        englishFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to English");
        JPanel englishPanel = new JPanel();
        englishFrame.add(englishPanel);
        englishPanel.add(label);
    }
}
static class computersAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame computersFrame = new JFrame("Computers");
        computersFrame.setVisible(true);
        computersFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Computers");
        JPanel computersPanel = new JPanel();
        computersFrame.add(computersPanel);
        computersPanel.add(label);
    }
}

}

3 个答案:

答案 0 :(得分:1)

您似乎想要的布局可以通过使用三种布局的组合来实现。

  1. 按钮GridLayout
  2. 标签为FlowLayout
  3. BorderLayout将标签限制在顶部,将剩余可用空间中的按钮限制在其中。
  4. enter image description here

    这是固定代码。请参阅下面的进一步提示。

    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);
        }
    }
    

    其他提示:

    1. 请学习常见的Java命名法(命名约定 - 例如EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是UPPER_CASE_CONSTANT)并一致地使用它。
    2. Java GUI必须使用不同语言环境中的不同PLAF来处理不同的操作系统,屏幕大小,屏幕分辨率等。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them以及white space的布局填充和边框。
    3. 为获得最佳效果,应构建GUI并在EDT上显示。创建RunnableSwingUtilities.invokeLater(..相结合的代码实现了这一目标。
    4. 动作,图标和字体都是布局的无关紧要。将它们排除在示例之外。
    5. 应在添加所有组件后调用setVisible(true);,并调用pack()进行布局。打包容器渲染设置大小过时(pack()将确定正确的大小)。
    6. 调整布局中控件中使用的数字,用于按钮的Insets和& EmptyBorder调整GUI的大小。有关详细信息,请参阅每个Java文档。

答案 1 :(得分:0)

您需要 setBounds()两个标签,即 welcomelabel hi ,类似于您对四个按钮所做的操作 您没有 setBounds()到单击按钮及其工作时创建的其他帧的标签,因为默认情况下JPanel具有flowlayout并且它正在相应地工作

答案 2 :(得分:0)

如果要查看在面板上创建的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设置mainFrame,您应该使用JLabel执行相同操作。

以下是一些有用的问题: Change jLabel Visibility