Java Swing在GUI中添加更多行,背景不显示

Java Swing在GUI中添加更多行,背景不显示,java,swing,jframe,grid-layout,border-layout,Java,Swing,Jframe,Grid Layout,Border Layout,我已经在另一个线程的帮助下创建了一个GUI来正确格式化它。现在的问题是,我想为“后退按钮”添加另一行。当我创建并添加面板时,它不会显示,除非我从rootPanel中删除另一个JPanel。如果我将rootPanel的GridLayout的参数更改为0,2,0,0,而不是0,1,0,0,它将完全未格式化。有什么想法吗 另一个问题是代码frame.setContentPane(后台)最初为GUI设置背景,但是使用此新代码,它不再设置背景。关于如何解决这个问题有什么想法吗 public class T

我已经在另一个线程的帮助下创建了一个GUI来正确格式化它。现在的问题是,我想为“后退按钮”添加另一行。当我创建并添加面板时,它不会显示,除非我从
rootPanel
中删除另一个JPanel。如果我将
rootPanel
GridLayout
的参数更改为0,2,0,0,而不是0,1,0,0,它将完全未格式化。有什么想法吗

另一个问题是代码
frame.setContentPane(后台)
最初为GUI设置背景,但是使用此新代码,它不再设置背景。关于如何解决这个问题有什么想法吗

public class TimerMenu {

    private JFrame frame;
    private JPanel rootPanel, logoPanel, mainPanel, backButtonPanel; // JPanel = 'parts' that build up the JFrame
    private JLabel background, logo, timeText;
    private JButton startTimerButton, backButton;
    private JComboBox timeUnitChoice;

    public TimerMenu() {
        frame = new JFrame("Timer");
        startTimerButton = new JButton("Start Timer");
        startTimerButton.setPreferredSize(new Dimension(135, 30));
        startTimerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!", JOptionPane.ERROR_MESSAGE);
            }
        });

        backButton = new JButton("Back to Main Menu");
        backButton.setPreferredSize(new Dimension(135, 30));
        backButton.setForeground(Color.RED);
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Opening main menu.
                frame.dispose();
                new MainMenu();
            }
        });

        // Creating drop down menu.
        String[] timeChoices = {"Nanoseconds", "Microseconds", "Milliseconds", "Seconds", "Minutes", "Hours", "Days"};

        // Giving the choices from the array of 'timeChoices'
        timeUnitChoice = new JComboBox(timeChoices);

        // Setting the default option to 'Minutes' (4th choice, starting at 0 as its an array!)
        timeUnitChoice.setSelectedIndex(4);

        try {
            background = new JLabel(new ImageIcon(ImageIO.read(getClass()
                .getResourceAsStream("/me/devy/alarm/clock/resources/background.jpg"))));
            logo = new JLabel(new ImageIcon(ImageIO.read(getClass()
                .getResourceAsStream("/me/devy/alarm/clock/resources/timer.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Creating simple text
        background.setLayout(new BorderLayout());
        frame.setContentPane(background);

        // Creating the root panel (will combined all the panels)
        rootPanel = new JPanel();
        frame.getContentPane().add(rootPanel, BorderLayout.NORTH);

        // Giving it a GridLayout of (0, 1, 0, 0), this makes it that every panel
        // has their own row in the GUI
        rootPanel.setLayout(new GridLayout(0, 1, 0, 0));

        // Creating the logo panel with a flow layout (keeps all components on one line goes onto the next if needed)
        logoPanel = new JPanel(new FlowLayout());
        rootPanel.add(logoPanel);
        logoPanel.add(logo);

        // Creating the main panel, same as above.
        mainPanel = new JPanel(new FlowLayout());
        rootPanel.add(mainPanel);
        timeText = new JLabel("Length:"); // Creating text on the GUI.
        mainPanel.add(timeUnitChoice);
        mainPanel.add(timeText);
        mainPanel.add(startTimerButton);

        // Creating the back button panel, same as above (logoPanel).
        backButtonPanel = new JPanel(new FlowLayout());
        rootPanel.add(backButtonPanel);
        backButtonPanel.add(backButton);

        // Setting some frame properties.
        frame.setVisible(true);
        frame.setSize(550, 250);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

您可以通过以下方式向应用程序添加背景

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class BackgroundImageJFrame extends JFrame
    {
    JButton b1;
    JLabel l1;
        public BackgroundImageJFrame()
        {
        setTitle("Background Color for JFrame");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    /*
        One way
        -----------------
        setLayout(new BorderLayout());
        JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
        add(background);
        background.setLayout(new FlowLayout());
        l1=new JLabel("Here is a button");
        b1=new JButton("I am a button");
        background.add(l1);
        background.add(b1);
    */
    // Another way
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
        setLayout(new FlowLayout());
        l1=new JLabel("Here is a button");
        b1=new JButton("I am a button");
        add(l1);
        add(b1);
        // Just for refresh :) Not optional!
        setSize(399,399);
        setSize(400,400);
        }
        public static void main(String args[])
        {
        new BackgroundImageJFrame();
        }
    }

“有什么想法吗?”使用更灵活的布局,如
GridBagLayout
,例如“另一个问题是代码”frame.setContentPane(background);“最初为GUI设置背景,但使用新代码它不再这样做。关于如何解决这个问题的想法呢?”-
JPanel
默认情况下是不透明的,您需要使
rootPane
透明,
JPanel#setOpaque
我“认为”您的问题“可能”是由于使用
JLabel
作为背景
JLabel
只从
图标和
文本属性计算它的首选大小,而不是像
JPanel
那样从布局管理器计算,但当我运行代码时,我可以很好地看到“后退”按钮,但我的背景图像相当大。@MadProgrammer我尝试过使用set不透明(true)然而,这没有起到任何作用。那么除了JLabel之外,你还推荐什么背景呢?我还将尝试GridBagLayout的想法,谢谢!想想看,如果将
true
提供给
setopue
,您希望发生什么?既然
不透明
是不透明的,那不就是不透明吗?尝试传递它
false
。另外请注意,您还需要为添加到
rootPanel
的每个面板执行此操作