简体   繁体   English

摇摆在GUI中添加更多行,并且背景不显示

[英]Swing adding more lines in the GUI, and background not showing

I have created a GUI with the help of another thread to format it properly. 我已经在另一个线程的帮助下创建了一个GUI,以对其进行正确格式化。 Thing is now I want to add another line for a 'back button'. 现在,我想为“后退按钮”添加另一行。 When I create the panel and add it it doesn't show unless I remove another JPanel from the rootPanel . 创建面板并添加面板时,除非从rootPanel删除另一个JPanel,否则它不会显示。 If I change the parameters of the GridLayout for the rootPanel to 0, 2, 0, 0 rather than 0, 1, 0, 0 it becomes completely unformatted. 如果我将rootPanelGridLayout的参数更改为rootPanel而不是rootPanel ,则它会变得完全未格式化。 Any ideas? 有任何想法吗?

Another problem is the code frame.setContentPane(background); 另一个问题是代码frame.setContentPane(background); originally set the background for the GUI, however with this new code it no longer does. 最初设置了GUI的背景,但是使用此新代码,它将不再起作用。 Any ideas on how to fix this as well? 关于如何解决此问题的任何想法?

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

You can add background to your application by these ways 您可以通过以下方式为应用程序添加背景

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM