Swing adding more lines in the GUI, and background

2019-09-03 10:56发布

问题:

I have created a GUI with the help of another thread to format it properly. 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. 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. Any ideas?

Another problem is the code frame.setContentPane(background); originally set the background for the GUI, however with this new code it no longer does. 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);
    }
}

回答1:

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