摆动在GUI中添加更多行,并且背景不显示 [英] Swing adding more lines in the GUI, and background not showing

查看:76
本文介绍了摆动在GUI中添加更多行,并且背景不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在另一个线程的帮助下创建了一个GUI,以对其进行正确格式化.现在,我想为后退按钮"添加另一行.创建面板并添加面板时,除非从rootPanel中删除另一个JPanel,否则它不会显示.如果我将rootPanelGridLayout的参数更改为0、2、0、0而不是0、1、0、0,则它将变得完全未格式化.有什么想法吗?

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?

另一个问题是代码frame.setContentPane(background);最初为GUI设置了背景,但是对于此新代码,它不再起作用.关于如何解决此问题的任何想法?

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

推荐答案

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

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

这篇关于摆动在GUI中添加更多行,并且背景不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆