我是编程和学习的新手。在GUI界面顶部放置JLabel
“欢迎回来”时,我需要帮助。尽管我付出了很大的努力,但这个标签一直卡在中间。随时纠正我可以做得更好的代码中显示的任何其他错误。
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
label.setLocation(240, 20);
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
frame.add(panel);
frame.add(label);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
看看代码。面板在中间布局管理器(边框)上重新布置还有其他基于Layout的解决方案,但主要思想是相同的
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
//define a new layout
frame.setLayout(new BorderLayout());
//panel for label
JPanel topPanel = new JPanel();
JPanel panel = new JPanel();
panel.setBounds(30, 80, 400, 570);
panel.setLayout(new GridLayout(3, 2, 15, 15));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JButton meals = new JButton("Meals");
meals.setFont(new Font("Helvetica", Font.BOLD, 24));
meals.setOpaque(true);
JButton reminder = new JButton ("Reminders");
reminder.setFont(new Font("Helvetica", Font.BOLD, 24));
reminder.setOpaque(true);
JButton shop = new JButton ("Shop");
shop.setFont(new Font("Helvetica", Font.BOLD, 24));
shop.setOpaque(true);
JButton sleep = new JButton ("Sleep Timer");
sleep.setFont(new Font("Helvetica", Font.BOLD, 24));
sleep.setOpaque(true);
JButton account = new JButton ("My Account");
account.setFont(new Font("Helvetica", Font.BOLD, 24));
account.setOpaque(true);
JButton aboutus = new JButton ("About Us");
aboutus.setFont(new Font("Helvetica", Font.BOLD, 24));
aboutus.setOpaque(true);
JLabel label = new JLabel("Welcome back!");
panel.add(meals);
panel.add(reminder);
panel.add(shop);
panel.add(sleep);
panel.add(account);
panel.add(aboutus);
topPanel.add(label);
topPanel.setSize(200, 30);
//rearrange panels on frame
frame.add(topPanel, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}