gpt4 book ai didi

java - JLabel 未出现在 JFrame 上

转载 作者:行者123 更新时间:2023-12-02 11:53:29 24 4
gpt4 key购买 nike

我已经搜索了很多次来查找为什么我的 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);
}
}

}

最佳答案

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

  1. 用于按钮的 GridLayout
  2. 用于标签的 FlowLayout
  3. 一个 BorderLayout,用于将标签限制在顶部,并将按钮限制在其余可用空间中。

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 上显示。创建 Runnable 的代码与 SwingUtilities.invokeLater(..) 结合实现了这一点。
  4. 操作、图标和字体都与布局无关。将它们排除在示例之外。
  5. 应在添加所有组件后调用 setVisible(true);,并调用 pack() 来对它们进行布局。打包容器呈现设置大小已过时(pack() 将确定正确的大小)。
  6. 调整布局构造函数中使用的数字、用于按钮的 InsetsEmptyBorder 来调整 GUI 的大小。有关详细信息,请参阅每个 Java 文档。

关于java - JLabel 未出现在 JFrame 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47744731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com